Documentation
¶
Index ¶
- func Get[T any](container *Container, token Token, modules ...*Module) (T, error)
- func MustResolve[T any](token Token, injections Injections) T
- func Require(injections Injections, tokens ...Token)
- func Resolve[T any](token Token, injections Injections) (T, error)
- type CircularInjectionError
- type Container
- type DependencyError
- type Factory
- type FactoryProviderInjection
- func (provider *FactoryProviderInjection[T]) AssignOn(module *Module)
- func (provider *FactoryProviderInjection[T]) AssignedTo() *Module
- func (provider *FactoryProviderInjection[T]) Create(injections Injections) (*Injection, error)
- func (provider *FactoryProviderInjection[T]) Exportable() bool
- func (provider *FactoryProviderInjection[T]) Injections() []Token
- func (provider *FactoryProviderInjection[T]) Scope() Scope
- func (provider *FactoryProviderInjection[T]) Token() Token
- type IProvider
- type Injection
- type Injections
- type Module
- type Scope
- type Token
- type Tokenized
- type ValueProviderInjection
- func (provider *ValueProviderInjection[T]) AssignOn(module *Module)
- func (provider *ValueProviderInjection[T]) AssignedTo() *Module
- func (provider *ValueProviderInjection[T]) Create(Injections) (*Injection, error)
- func (provider *ValueProviderInjection[T]) Exportable() bool
- func (provider *ValueProviderInjection[T]) Injections() []Token
- func (provider *ValueProviderInjection[T]) Scope() Scope
- func (provider *ValueProviderInjection[T]) Token() Token
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustResolve ¶
func MustResolve[T any](token Token, injections Injections) T
MustResolve returns token from injections as T or panics.
func Require ¶
func Require(injections Injections, tokens ...Token)
Require panics if any token is missing from injections.
Types ¶
type CircularInjectionError ¶
type CircularInjectionError struct {
// Tokens is the cycle path.
Tokens []Token
// contains filtered or unexported fields
}
CircularInjectionError reports a module or provider cycle.
func (*CircularInjectionError) Error ¶
func (err *CircularInjectionError) Error() string
Error returns the formatted cycle error.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container owns modules and resolves provider dependencies.
func (*Container) AddModules ¶
AddModules registers modules before Run.
type DependencyError ¶
type DependencyError struct {
// contains filtered or unexported fields
}
DependencyError reports an invalid or missing dependency.
func (*DependencyError) Error ¶
func (err *DependencyError) Error() string
Error returns the formatted dependency error.
type Factory ¶
type Factory[T any] struct { // Injects lists constructor dependency tokens. Injects []Token // ValueScope selects Singleton or Prototype behavior. ValueScope Scope // Constructor builds the value from resolved dependencies. Constructor func(Injections) (T, error) }
Factory defines how to build a provider value.
func NewFactory ¶
func NewFactory[T any](injects []Token, valueScope Scope, constructor func(Injections) (T, error)) Factory[T]
NewFactory returns a Factory.
type FactoryProviderInjection ¶
type FactoryProviderInjection[T any] struct { Key Token Export bool Factory Factory[T] // contains filtered or unexported fields }
FactoryProviderInjection provides values from a Factory.
func FactoryProvider ¶
func FactoryProvider[T any](token Token, factory Factory[T], exportable bool) *FactoryProviderInjection[T]
FactoryProvider returns a provider backed by factory.
func (*FactoryProviderInjection[T]) AssignOn ¶
func (provider *FactoryProviderInjection[T]) AssignOn(module *Module)
AssignOn binds the provider to module.
func (*FactoryProviderInjection[T]) AssignedTo ¶
func (provider *FactoryProviderInjection[T]) AssignedTo() *Module
AssignedTo returns the owning module.
func (*FactoryProviderInjection[T]) Create ¶
func (provider *FactoryProviderInjection[T]) Create(injections Injections) (*Injection, error)
Create builds or returns the provider value.
func (*FactoryProviderInjection[T]) Exportable ¶
func (provider *FactoryProviderInjection[T]) Exportable() bool
Exportable reports whether importers can use this provider.
func (*FactoryProviderInjection[T]) Injections ¶
func (provider *FactoryProviderInjection[T]) Injections() []Token
Injections returns the factory dependency tokens.
func (*FactoryProviderInjection[T]) Scope ¶
func (provider *FactoryProviderInjection[T]) Scope() Scope
Scope returns the configured lifecycle.
func (*FactoryProviderInjection[T]) Token ¶
func (provider *FactoryProviderInjection[T]) Token() Token
Token returns the configured or derived provider token.
type IProvider ¶
type IProvider interface {
// Token returns the provider token.
Token() Token
// Injections returns dependency tokens required by Create.
Injections() []Token
// Create builds the provider value from resolved dependencies.
Create(Injections) (*Injection, error)
// Exportable reports whether importers can use this provider.
Exportable() bool
// Scope returns the provider lifecycle.
Scope() Scope
// AssignOn binds the provider to a module.
AssignOn(module *Module)
// AssignedTo returns the owning module.
AssignedTo() *Module
}
IProvider describes a value the container can create.
type Injections ¶
type Injections []*Injection
Injections is the dependency set passed to a factory constructor.
func (Injections) MustResolve ¶
func (injections Injections) MustResolve(token Token) any
MustResolve returns token from injections as any or panics.
func (Injections) Require ¶
func (injections Injections) Require(tokens ...Token)
Require panics if any token is missing from injections.
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module groups providers and imports.
type Token ¶
type Token = string
Token identifies a provider or module.
func CreateToken ¶
CreateToken returns the struct name of T as a Token.
type ValueProviderInjection ¶
type ValueProviderInjection[T any] struct { Key Token Export bool Value T // contains filtered or unexported fields }
ValueProviderInjection provides an existing singleton value.
func ValueProvider ¶
func ValueProvider[T any](token Token, value T, exportable bool) *ValueProviderInjection[T]
ValueProvider returns a singleton provider for value.
func (*ValueProviderInjection[T]) AssignOn ¶
func (provider *ValueProviderInjection[T]) AssignOn(module *Module)
AssignOn binds the provider to module.
func (*ValueProviderInjection[T]) AssignedTo ¶
func (provider *ValueProviderInjection[T]) AssignedTo() *Module
AssignedTo returns the owning module.
func (*ValueProviderInjection[T]) Create ¶
func (provider *ValueProviderInjection[T]) Create(Injections) (*Injection, error)
Create returns the stored value as an Injection.
func (*ValueProviderInjection[T]) Exportable ¶
func (provider *ValueProviderInjection[T]) Exportable() bool
Exportable reports whether importers can use this provider.
func (*ValueProviderInjection[T]) Injections ¶
func (provider *ValueProviderInjection[T]) Injections() []Token
Injections returns nil for value providers.
func (*ValueProviderInjection[T]) Scope ¶
func (provider *ValueProviderInjection[T]) Scope() Scope
Scope returns Singleton for value providers.
func (*ValueProviderInjection[T]) Token ¶
func (provider *ValueProviderInjection[T]) Token() Token
Token returns the configured or derived provider token.