goweb/cqrs/service.go

35 lines
812 B
Go
Raw Normal View History

2020-04-16 16:32:02 +02:00
package cqrs
import (
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/service"
)
// ServiceName defines the cqrs service name
const ServiceName service.Name = "cqrs"
// From retrieves the cqrs service in the given container
2020-07-07 08:19:15 +02:00
func From(container *service.Container) (*Dispatcher, error) {
2020-04-16 16:32:02 +02:00
raw, err := container.Service(ServiceName)
if err != nil {
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
}
2020-07-07 08:19:15 +02:00
srv, ok := raw.(*Dispatcher)
2020-04-16 16:32:02 +02:00
if !ok {
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
}
return srv, nil
}
// Must retrieves the cqrs service in the given container or panic otherwise
2020-07-07 08:19:15 +02:00
func Must(container *service.Container) *Dispatcher {
2020-04-16 16:32:02 +02:00
service, err := From(container)
if err != nil {
panic(err)
}
return service
}