di

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package di provides mechanisms to decouple the implementation of dependency injection from the types being constructed.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
var ErrNilFactory = errors.New("factory cannot be nil")

ErrNilFactory is returned when an attempt is made to register a nil factory.

View Source
var ErrNilResolver = errors.New("cannot resolve instances from nil Resolver")

ErrNilResolver is returned when the Resolve function receives a nil Resolver argument.

View Source
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.

View Source
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.

View Source
var ErrResolverError = errors.New("Resolver returned error")

ErrResolverError is returned when the Resolve function receives an error from a Resolver.

View Source
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.

View Source
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.

View Source
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.

View Source
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

func Resolve

func Resolve[T any](resolver Resolver) (T, error)

Resolve obtains an instance of the requested type from a Resolver. An [error] is returned when the Resolver returns an [error] or a value that is not assignable to T.

Types

type Closer

type Closer interface {
	Close() error
}

A Closer is a value that can be closed.

type ContextCloser

type ContextCloser interface {
	Close(context.Context) error
}

A ContextCloser is a value that can be closed with a context.Context.

type Factory

type Factory[T any] func(Resolver) (T, error)

A Factory is a function that makes instances of T using a Resolver to initialize dependencies.

func GetDefaultFactory

func GetDefaultFactory[T any]() (Factory[T], error)

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
)

func (Lifetime) String

func (lifetime Lifetime) String() string

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

type NonConcreteImplementation struct {

	// Type is the non-concrete type.
	Type reflect.Type
}

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 RegisterFactory[Target any, Impl any](
	registry Registry,
	lifetime Lifetime,
	factory Factory[Impl],
) (Registry, error)

func RegisterType

func RegisterType[Target any, Impl any](registry Registry, lifetime Lifetime) (Registry, error)

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.

func (RootProvider) NewScope

func (provider RootProvider) NewScope() Scope

NewScope creates a new Scope which can resolve Scoped values as well as Transient and Singleton values.

func (RootProvider) Resolve

func (provider RootProvider) Resolve(typ reflect.Type) (any, error)

Resolve returns an instance of the requested type if it was registered as a Transient or Singleton value.

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

func (Scope) Close

func (scope Scope) Close(ctx context.Context) []error

func (Scope) NewScope

func (scope Scope) NewScope() Scope

NewScope creates a new Scope which can resolve Scoped values as well as Transient and Singleton values.

func (Scope) Resolve

func (scope Scope) Resolve(typ reflect.Type) (any, error)

Resolve returns an instance of the requested type if it was registered.

type ScopedValueRequestedFromRootProvider

type ScopedValueRequestedFromRootProvider struct {

	// Type is the unknown type.
	Type reflect.Type
}

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

Error implements [error].

func (ScopedValueRequestedFromRootProvider) Is

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

type UnknownType struct {

	// Type is the unknown type.
	Type reflect.Type
}

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) Error

func (err UnknownType) Error() string

Error implements [error].

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) Error

func (err UnsharableType) Error() string

Error implements [error].

func (UnsharableType) Is

func (err UnsharableType) Is(target error) bool

Is indicates that a UnsharableType is ErrUnsharableType.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL