Documentation ¶
Index ¶
- Variables
- func MustGetT[T any](c Container, id string) T
- type Container
- type ContainerBuilder
- type DecoratorDef
- type DefaultBuilder
- func (b *DefaultBuilder) Add(def Definition) ContainerBuilder
- func (b *DefaultBuilder) Build(ctx context.Context) (*DefaultContainer, error)
- func (b *DefaultBuilder) Get(id string) Definition
- func (b *DefaultBuilder) Has(id string) bool
- func (b *DefaultBuilder) MustBuild(ctx context.Context) *DefaultContainer
- type DefaultContainer
- type Definition
- type Factory
- type FactoryCtx
- type FactoryFn
- type FactoryFnWithContext
- type FactoryFnWithError
- type ParamDef
- type ServiceDef
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrCircularDependency is returned when a dependency cycle has been detected ErrCircularDependency = errors.New("circular dependency detected") // ErrUnknownService is returned if a requested service does not exist ErrUnknownService = errors.New("unknown service") // ErrServiceFactoryFailed is returned when the factory cannot instantiate the service ErrServiceFactoryFailed = errors.New("factory failed to instantiate service") )
Functions ¶
Types ¶
type Container ¶
type Container interface { // Has will return TRUE when a service or param by given id exist, otherwise FALSE Has(id string) bool // Get will return a plain value (for ParamDef) or the instance (for ServiceDef and DecoratorDef) by id Get(id string) (any, error) // MustGet will return the param value or service instance by id // Beware that this can panic at runtime if any instantiation errors occur! // Consider to explicitly call Boot() before using it MustGet(id string) any // Inject will take a struct as target and sets the field values according to tagged service ids. // Example: // // type MyStruct struct { // TimeService *TimeService `inject:"service.time"` // TimeFormat string `inject:"param.time_format"` // } Inject(target any) error // Boot will instantiate all services eagerly. It is not mandatory to call Boot() since all // services (except decorated services) will be instantiated lazy per default. // Beware that lazy instantiation can cause a panic at runtime when retrieving values via MustGet()! // If you want to ensure that every service can be instantiated properly it is recommended to call Boot() // before first use of MustGet(). Boot() error // Ctx returns the context.Context Ctx() context.Context }
Container abstraction interface
type ContainerBuilder ¶ added in v0.0.10
type ContainerBuilder interface { // Add will add a new Definition to the ContainerBuilder. The val argument can be either a // - ServiceDef for a service // - DecoratorDef if you want to decorate another service // - ParamDef any parameter value of any type Add(def Definition) ContainerBuilder // Get returns a Definition by its ID, otherwise nil if it does not exist Get(id string) Definition // Has returns TRUE if a Definition of given ID exists Has(id string) bool }
ContainerBuilder abstraction interface
type DecoratorDef ¶
type DecoratorDef interface { Definition Factory() Factory Instance() any Decorates() string Decorated() Definition WithID(id string) DecoratorDef WithFactory(factory Factory) DecoratorDef WithInstance(instance any) DecoratorDef WithDecorates(id string) DecoratorDef WithDecorated(def Definition) DecoratorDef }
DecoratorDef abstraction interface
type DefaultBuilder ¶ added in v0.0.10
type DefaultBuilder struct {
// contains filtered or unexported fields
}
func Builder ¶ added in v0.0.10
func Builder(defs ...Definition) *DefaultBuilder
Builder returns a new ContainerBuilder instance
func (*DefaultBuilder) Add ¶ added in v0.0.10
func (b *DefaultBuilder) Add(def Definition) ContainerBuilder
func (*DefaultBuilder) Build ¶ added in v0.0.10
func (b *DefaultBuilder) Build(ctx context.Context) (*DefaultContainer, error)
func (*DefaultBuilder) Get ¶ added in v0.0.10
func (b *DefaultBuilder) Get(id string) Definition
func (*DefaultBuilder) Has ¶ added in v0.0.10
func (b *DefaultBuilder) Has(id string) bool
func (*DefaultBuilder) MustBuild ¶ added in v0.0.10
func (b *DefaultBuilder) MustBuild(ctx context.Context) *DefaultContainer
type DefaultContainer ¶ added in v0.0.10
func (*DefaultContainer) Boot ¶ added in v0.0.10
func (c *DefaultContainer) Boot() error
func (*DefaultContainer) Ctx ¶ added in v0.0.10
func (c *DefaultContainer) Ctx() context.Context
func (*DefaultContainer) Get ¶ added in v0.0.10
func (c *DefaultContainer) Get(id string) (any, error)
func (*DefaultContainer) Has ¶ added in v0.0.10
func (c *DefaultContainer) Has(id string) bool
func (*DefaultContainer) Inject ¶ added in v0.0.10
func (c *DefaultContainer) Inject(target any) error
func (*DefaultContainer) MustGet ¶ added in v0.0.10
func (c *DefaultContainer) MustGet(id string) any
type Factory ¶ added in v0.0.6
type Factory interface { FactoryFn() FactoryFn FactoryFnWithError() FactoryFnWithError FactoryFnWithContext() FactoryFnWithContext Instance() any }
func WithContextFn ¶ added in v0.0.6
func WithContextFn(fn FactoryFnWithContext) Factory
func WithErrorFn ¶ added in v0.0.6
func WithErrorFn(fn FactoryFnWithError) Factory
func WithInstance ¶ added in v0.0.7
type FactoryCtx ¶
type FactoryFnWithContext ¶ added in v0.0.6
type FactoryFnWithContext = func(ctx FactoryCtx) (any, error)
FactoryFnWithContext to define an anonymous functions
type FactoryFnWithError ¶ added in v0.0.6
FactoryFnWithError to define an anonymous functions
type ParamDef ¶
type ParamDef interface { Definition Value() any WithID(id string) ParamDef WithValue(v any) ParamDef }
ParamDef abstraction interface
type ServiceDef ¶
type ServiceDef interface { Definition Factory() Factory Instance() any WithID(id string) ServiceDef WithFactory(factory Factory) ServiceDef WithInstance(instance any) ServiceDef }
ServiceDef abstraction interface
func Service ¶
func Service(id string, factory Factory) ServiceDef
Service returns a new instance of ServiceDef
Source Files ¶
Click to show internal directories.
Click to hide internal directories.