Documentation
¶
Overview ¶
Package di provides mechanisms to decouple the implementation of dependency injection from the types being constructed.
Index ¶
- Variables
- func Resolve[T any](resolver Resolver) (T, error)
- type Closer
- type ContextCloser
- type Factory
- type InvalidImplementation
- type InvalidResolution
- type Lifetime
- type NoDefaultFactory
- type NonConcreteImplementation
- type Registry
- type Resolver
- type RootProvider
- type Scope
- type ScopedValueRequestedFromRootProvider
- type UndefinedLifetime
- type UnknownType
- type UnsharableType
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidImplementation = errors.New("implementation type is not assignable to target type")
ErrInvalidImplementation is returned when an attempt is made to register an implementation type for a target type is not assignable to.
var ErrInvalidResolution = errors.New("value from Resolver does not have requested type")
ErrInvalidResolution is returned when the Resolve function receives a value from a Resolver that cannot be assigned to the requested type.
NOTE: This error ALWAYS points to a broken implementation of Resolver. The contract of Resolver requires returned values to be assignable to the requested type, but the absence of generic methods in Go's type system prevents this from being enforced statically.
var ErrNilFactory = errors.New("factory cannot be nil")
ErrNilFactory is returned when an attempt is made to register a nil factory.
var ErrNilResolver = errors.New("cannot resolve instances from nil Resolver")
ErrNilResolver is returned when the Resolve function receives a nil Resolver argument.
var ErrNoDefaultFactory = errors.New("implementation type has no default factory")
ErrNoDefaultFactory is returned when an attempt is made to register an implementation type for which the package cannot provide a default factory to obtain instances from.
var ErrNonConcreteImplementation = errors.New("implementation type must be concrete")
ErrNonConcreteImplementation is returned when an attempt is made to register an implementation that is not a concrete type.
var ErrResolverError = errors.New("Resolver returned error")
ErrResolverError is returned when the Resolve function receives an error from a Resolver.
var ErrScopedValueRequestedFromRootProvider = errors.New("RootProvider cannot resolve a scoped value")
ErrScopedValueRequestedFromRootProvider is returned when an attempt is made to resolve a scoped value from a RootProvider.
var ErrUndefinedLifetime = errors.New("undefined lifetime")
ErrUndefinedLifetime is returned when an attempt is made to register a type with a Lifetime whose value is not one of the defined values Transient, Scoped, or Singleton.
var ErrUnknownType = errors.New("requested type is unknown")
ErrUnknownType is returned when an attempt is made to resolve a value from a provider but the requested type is unknown.
var ErrUnsharableType = errors.New("unsharable type cannot be registered with non-Transient lifetime")
ErrUnsharableType is returned when an unsharable type is registered with a Lifetime other than Transient.
Functions ¶
Types ¶
type ContextCloser ¶
A ContextCloser is a value that can be closed with a context.Context.
type Factory ¶
A Factory is a function that makes instances of T using a Resolver to initialize dependencies.
func GetDefaultFactory ¶
GetDefaultFactory returns the default factory for the requested type, or ErrNoDefaultFactory if the type has no default factory.
type InvalidImplementation ¶
type InvalidImplementation struct {
// Type is the type that cannot be assigned to [InvalidImplementation.Target].
Type reflect.Type
// Target is the type to which [InvalidImplementation.Type] cannot be assigned.
Target reflect.Type
}
An InvalidImplementation is an [error] indicating that an attempt was made to register an implementation type for a target type it is not assignable to. Calling errors.Is with an InvalidImplementation and ErrInvalidImplementation returns true.
func (InvalidImplementation) Error ¶
func (err InvalidImplementation) Error() string
Error implements [error].
func (InvalidImplementation) Is ¶
func (err InvalidImplementation) Is(target error) bool
Is indicates that an InvalidImplementation is ErrInvalidImplementation.
type InvalidResolution ¶
type InvalidResolution struct {
// Requested is the type that was requested from the [Resolver].
Requested reflect.Type
// Returned is the type of the value the [Resolver] returned.
Returned reflect.Type
}
A InvalidResolution is an [error] indicating that a Resolver returned a a value that could not be assigned to the requested type. Calling errors.Is with an InvalidResolution and ErrInvalidResolution returns true.
func (InvalidResolution) Error ¶
func (err InvalidResolution) Error() string
Error implements [error].
func (InvalidResolution) Is ¶
func (InvalidResolution) Is(target error) bool
Is indicates that an InvalidResolution is ErrInvalidResolution.
type Lifetime ¶
type Lifetime int
A Lifetime expresses the conditions under which an instance of a type will be instantiated or reused across distinct resolutions.
const ( // Transient means that a new instance is created every time the type is resolved. Transient Lifetime = iota + 1 // Scoped means that the type is instantiated once per resolution scope and the same instance // is returned every time the type is resolved in a given scope. // // NOTE: The [Scoped] [Lifetime] can only be used for [Sharable Types]. // // [Sharable Types]: https://github.com/ttd2089/garlic?tab=readme-ov-file#sharable-types Scoped // Singleton means that the type is only ever instantiated once and the same instance is // returned every time the type is resolved across all resolution scopes. // // NOTE: The [Singleton] [Lifetime] can only be used for [Sharable Types]. // // [Sharable Types]: https://github.com/ttd2089/garlic?tab=readme-ov-file#sharable-types Singleton )
type NoDefaultFactory ¶
type NoDefaultFactory struct {
// Type is the type for which the package cannot provide a default factory.
Type reflect.Type
}
An NoDefaultFactory is an [error] indicating that an attempt was made to register an implementation type for which the package cannot provide a default factory to obtain instances from. Calling errors.Is with a NoDefaultFactory and ErrNoDefaultFactory returns true.
func (NoDefaultFactory) Error ¶
func (err NoDefaultFactory) Error() string
Error implements [error].
func (NoDefaultFactory) Is ¶
func (NoDefaultFactory) Is(target error) bool
Is indicates that an NoDefaultFactory is ErrNoDefaultFactory.
type NonConcreteImplementation ¶
A NonConcreteImplementation is an [error] indicating that an attempt was made to register an implementation type that is not a concrete type. Calling errors.Is with a NonConcreteImplementation and ErrNonConcreteImplementation returns true.
func (NonConcreteImplementation) Error ¶
func (err NonConcreteImplementation) Error() string
Error implements [error].
func (NonConcreteImplementation) Is ¶
func (err NonConcreteImplementation) Is(target error) bool
Is indicates that a NonConcreteImplementation is ErrNonConcreteImplementation.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
A Registry is a collection into which services can be registered and from which a RootProvider may be built.
func RegisterFactory ¶
func RegisterType ¶
RegisterType is a shorthand for calling RegisterFactory using the result of calling GetDefaultFactory for the [Impl] type.
func (Registry) BuildRootProvider ¶
func (r Registry) BuildRootProvider() (RootProvider, error)
type Resolver ¶
type Resolver interface {
// Resolve provides an instance of the requested type if one is registered. Implementations
// MUST ensure that the values returned are assignable to the requested type.
Resolve(reflect.Type) (any, error)
}
A Resolver resolves instances of a requested type.
type RootProvider ¶
type RootProvider struct {
// contains filtered or unexported fields
}
A RootProvider is a [Provider] that can resolve Transient and Singleton values.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
A Scope is a [Provider] that can resolve Scoped values in addition to Transient and Singleton values. A Scope will create a single instance of a value for a type registered
type ScopedValueRequestedFromRootProvider ¶
A ScopedValueRequestedFromRootProvider is an [error] indicating that an attempt was made to resolve a scoped value from a RootProvider. Calling errors.Is with a ScopedValueRequestedFromRootProvider and ErrScopedValueRequestedFromRootProvider returns true.
func (ScopedValueRequestedFromRootProvider) Error ¶
func (err ScopedValueRequestedFromRootProvider) Error() string
Error implements [error].
func (ScopedValueRequestedFromRootProvider) Is ¶
func (err ScopedValueRequestedFromRootProvider) Is(target error) bool
Is indicates that a ScopedValueRequestedFromRootProvider is ErrScopedValueRequestedFromRootProvider.
type UndefinedLifetime ¶
type UndefinedLifetime struct {
// Value is the undefined value.
Value Lifetime
}
An UndefinedLifetime is an [error] indicating that an attempt was made to register a type with a Lifetime whose value is not one of the defined values Transient, Scoped, or Singleton. Calling errors.Is with an UndefinedLifetime and ErrUndefinedLifetime returns true.
func (UndefinedLifetime) Error ¶
func (err UndefinedLifetime) Error() string
Error implements [error].
func (UndefinedLifetime) Is ¶
func (err UndefinedLifetime) Is(target error) bool
Is indicates that an UndefinedLifetime is ErrUndefinedLifetime.
type UnknownType ¶
An UnknownType is an [error] indicating that an attempt was made to resolve a value from a provider but the requested type was unknown. Calling errors.Is with a UnknownType and ErrUnknownType returns true.
func (UnknownType) Is ¶
func (err UnknownType) Is(target error) bool
Is indicates that a UnknownType is ErrUnknownType.
type UnsharableType ¶
type UnsharableType struct {
// Type is the unsharable type.
Type reflect.Type
// Lifetime is the non-transient lifetime.
Lifetime Lifetime
}
A UnsharableType is an [error] indicating that an attempt was made to register an unsharable type with a Lifetime other than Transient. Calling errors.Is with a UnsharableType and ErrUnsharableType returns true.
func (UnsharableType) Is ¶
func (err UnsharableType) Is(target error) bool
Is indicates that a UnsharableType is ErrUnsharableType.