Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PostInject ¶
type PostInject interface {
PostInject() error
}
PostInject is a marker interface for injectable objects which should perform some action after injection of services.
type ServiceContainer ¶
type ServiceContainer interface {
// Get retrieves the service registered to the given key. It is an
// error for a service not to be registered to this key.
Get(key string) (interface{}, error)
// MustGet calls Get and panics on error.
MustGet(service string) interface{}
// Set registers a service with the given key. It is an error for
// a service to already be registered to this key.
Set(key string, service interface{}) error
// MustSet calls Set and panics on error.
MustSet(service string, value interface{})
// Inject will attempt to populate the given type with values from
// the service container based on the value's struct tags. An error
// may occur if a service has not been registered, a service has a
// different type than expected, or struct tags are malformed.
Inject(obj interface{}) error
}
ServiceContainer is a wrapper around services indexed by a unique name. Services can be retrieved by name, or injected into a struct by reading tagged fields.
func NewServiceContainer ¶
func NewServiceContainer() ServiceContainer
NewServiceContainer creates an empty service container.
func Overlay ¶ added in v1.0.1
func Overlay(base ServiceContainer, services map[string]interface{}) ServiceContainer
Overlay wraps the given service container with an immutable map of services. Calling Get or MustGet on the resulting service container will return a service from the overlay map, then will fall back to the wrapped service container. Similarly, Inject will favor services from the overlay map.
This allows a user to re-assign services in the container for a specific specialized code path. This can be used, for example, to inject a logger with context for the current request or task to a short-lived handler.
Calling Set or MustSet will modify the wrapped container directly.
type ServiceGetter ¶ added in v1.0.1
type ServiceGetter interface {
// Get retrieves the service registered to the given key. It is an
// error for a service not to be registered to this key.
Get(key string) (interface{}, error)
}
ServiceGetter is a subset of a ServiceContainer that only supports the retrieval of a registered service by name.