Documentation
¶
Index ¶
- Variables
- func Call(injector *Injector, function any) (err error)
- func Call1[T1 any](injector *Injector, function any) (r1 T1, err error)
- func Call2[T1, T2 any](injector *Injector, function any) (r1 T1, r2 T2, err error)
- func Call3[T1, T2, T3 any](injector *Injector, function any) (r1 T1, r2 T2, r3 T3, err error)
- func Call4[T1, T2, T3, T4 any](injector *Injector, function any) (r1 T1, r2 T2, r3 T3, r4 T4, err error)
- func CallN(injector *Injector, function any) (returns []any, err error)
- func Get[Tkey any](injector *Injector) (value Tkey, err error)
- func GetByName(injector *Injector, name string) (value any, err error)
- func RegisterScope[Tkey any](target *Injector, constructor any) error
- func RegisterScopeError[Tkey any](target *Injector, constructor any) error
- func RegisterSingleton[Tkey any](target *Injector, constructor any) error
- func RegisterSingletonError[Tkey any](target *Injector, constructor any) error
- func RegisterTransient[Tkey any](target *Injector, constructor any) error
- func RegisterTransientError[Tkey any](target *Injector, constructor any) error
- func Verify(injector *Injector) error
- type CacheStrategy
- type Injector
- func (this *Injector) Call(function any) (err error)
- func (this *Injector) Call1(function any) (r1 any, err error)
- func (this *Injector) Call2(function any) (r1, r2 any, err error)
- func (this *Injector) Call3(function any) (r1, r2, r3 any, err error)
- func (this *Injector) Call4(function any) (r1, r2, r3, r4 any, err error)
- func (this *Injector) CallN(function any) (returns []any, err error)
- func (this *Injector) Get(key reflect.Type) (value any, err error)
- func (this *Injector) GetByName(name string) (value any, err error)
- func (this *Injector) RegisterScope(key reflect.Type, constructor any) error
- func (this *Injector) RegisterScopeError(key reflect.Type, constructor any) error
- func (this *Injector) RegisterSingleton(key reflect.Type, constructor any) error
- func (this *Injector) RegisterSingletonError(key reflect.Type, constructor any) error
- func (this *Injector) RegisterTransient(key reflect.Type, constructor any) error
- func (this *Injector) RegisterTransientError(key reflect.Type, constructor any) error
Constants ¶
This section is empty.
Variables ¶
var ( // InjectorError is a generic injector error, usable for detecting if an // error was injector related. InjectorError = errors.New("injector error") // ErrorAlreadyRegistered is returned when a type has already been // registered. ErrorAlreadyRegistered = fmt.Errorf("%w, already registered", InjectorError) // ErrorBadState is a panicking error when an access attempt is made on an // injector that is in a bad state. ErrorBadState = fmt.Errorf("%w, bad injector state", InjectorError) // ErrorDependencyLoop indicates that an unsolvable dependency injection // loop. ErrorDependencyLoop = fmt.Errorf("%w, dependency loop detected", InjectorError) // ErrorNoReturns is returned when a constructor has no return value. ErrorNoReturns = fmt.Errorf("%w, no return values, must be exactly 1 return value", InjectorError) // ErrorNotAFunction is returned when a non-function is passed as a function. ErrorNotAFunction = fmt.Errorf("%w, value is not a function", InjectorError) // ErrorNotAssignable is returned when a constructor returns a type that // cannot be assigned to the key type. ErrorNotAssignable = fmt.Errorf("%w, value is not assignable", InjectorError) // ErrorNotRegistered indicates that a required dependency does not appear // in the registered list. ErrorNotRegistered = fmt.Errorf("%w, not registered", InjectorError) // ErrorNotStructOrInterface is returned when a type is not registerable. ErrorNotStructOrInterface = fmt.Errorf("%w, key type is not a struct or interface", InjectorError) // ErrorTooManyReturns is returned when a constructor has more than 1 return // value. ErrorTooManyReturns = fmt.Errorf("%w, too many return values, must be exactly 1 return value", InjectorError) // ErrorVariadicArguments is returned when a function has a variadic // signature. ErrorVariadicArguments = fmt.Errorf("%w, function has a variadic signature", InjectorError) // ErrorWrongNumberOfReturns is returned when a function has more or less // than the expected number of return values. ErrorWrongNumberOfReturns = fmt.Errorf("%w, wrong number of return values", InjectorError) )
Functions ¶
func Call ¶
Call checks a function's signature then calls the function by injecting all the arguments. Call is used for any function that has no return values.
Parameters:
- injector is the dependency injector to use when making the function call.
- function is the function to be called with injected arguments.
Returns:
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func Call1 ¶
Call1 checks a function's signature then calls the function by injecting all the arguments. Call1 is used for any function that has exactly one return value.
Parameters:
- injector is the dependency injector to use when making the function call.
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func Call2 ¶
Call2 checks a function's signature then calls the function by injecting all the arguments. Call2 is used for any function that has exactly two return values.
Parameters:
- injector is the dependency injector to use when making the function call.
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- r2 is return value 2.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func Call3 ¶
Call3 checks a function's signature then calls the function by injecting all the arguments. Call3 is used for any function that has exactly three return values.
Parameters:
- injector is the dependency injector to use when making the function call.
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- r2 is return value 2.
- r3 is return value 3.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func Call4 ¶
func Call4[T1, T2, T3, T4 any](injector *Injector, function any) (r1 T1, r2 T2, r3 T3, r4 T4, err error)
Call4 checks a function's signature then calls the function by injecting all the arguments. Call4 is used for any function that has exactly four return values.
Parameters:
- injector is the dependency injector to use when making the function call.
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- r2 is return value 2.
- r3 is return value 3.
- r4 is return value 4.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func CallN ¶
CallN checks a function's signature then calls the function by injecting all the arguments. CallN is used for any function with any number of return values.
Parameters:
- injector is the dependency injector to use when making the function call.
- function is the function to be called with injected arguments.
Returns:
- returns contains all return values.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
func Get ¶
Get retrieves the given type using the registered constructor or instance.
Parameters:
- injector is the dependency injector to get the instance from.
Returns:
- value is the registered instance or the result of the registered constructor.
- err is nil unless an error occurred during retrieval.
Errors:
- if Verify() has not been called.
- if Verify() returned an error.
func GetByName ¶
GetByName retrieves the named type using the registered constructor or instance. The name is expected to be truncated down to type name only, package should not be included in the name. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.
Parameters:
- injector is the dependency injector to get the instance from.
- name is the name of the type that should have been registered. Expected to be truncated down to type name only, package should not be included. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.
Returns:
- value is the registered instance or the result of the registered constructor.
- err is nil unless the named type cannot be found.
Errors:
- ErrorNotRegistered is returned if the named type cannot be found.
Errors:
- if Verify() has not been called.
- if Verify() returned an error.
func RegisterScope ¶
RegisterScope adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.
Notes:
- Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use RegisterScopeError instead.
Parameters:
- target is the Injector to register the type in.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func RegisterScopeError ¶
RegisterScopeError adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.
Notes:
- Constructor is expected to return (Tkey, error). If your constructor does not return an error, use RegisterScope instead.
Parameters:
- target is the Injector to register the type in.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func RegisterSingleton ¶
RegisterSingleton adds a constructor for the given type. Every time the type is requested, the same instance is always returned.
Notes:
- Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use RegisterSingletonError
Parameters:
- target is the Injector to register the type in.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func RegisterSingletonError ¶
RegisterSingletonError adds a constructor for the given type. Every time the type is requested, the same instance is always returned.
Notes:
- Constructor is expected to return (Tkey, error). If your constructor does not return an error, use RegisterSingleton instead.
Parameters:
- target is the Injector to register the type in.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func RegisterTransient ¶
RegisterTransient adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.
Notes:
- Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use RegisterTransientError
Parameters:
- target is the Injector to register the type in.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func RegisterTransientError ¶
RegisterTransientError adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.
Notes:
- Constructor is expected to return (Tkey, error). If your constructor does not return an error, use RegisterTransient instead.
Parameters:
- target is the Injector to register the type in.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func Verify ¶
Verify examines all registered types and their corresponding constructors and validates them, otherwise an error is returned.
Parameters:
- target is the Injector to explore and verify all the registered types in.
Errors:
- ErrorDependencyLoop indicates that an unsolvable dependency injection loop.
- ErrorNotRegistered indicates that a required dependency does not appear in the registered list.
Types ¶
type CacheStrategy ¶
type CacheStrategy int
CachingStrategy is any one of a few predefined type-caching backends for the injector. Each caching strategy has its strengths and weaknesses.
const ( // Map uses a Go map for the cache. Good for truly random access. Map CacheStrategy = iota // BubbleList uses a slice that reorders based on the number of times // each element is accessed. Good for highly stable access patterns that // don't change during runtime. BubbleList // PriorityList uses a linked-list that promotes an item all the way to // the front whenever it is accessed. Good for fairly stable access // patterns that can change over time. PriorityList )
type Injector ¶
type Injector struct {
// contains filtered or unexported fields
}
Injector is a dependency-injector meant to be single-use at startup and used in applications where taking a few microseconds generating a dependency is acceptable.
func New ¶
func New(cacheStrategy ...CacheStrategy) *Injector
New creates a new injector, preloaded with itself.
Parameters:
- cacheStrategy defines an optional internal caching strategy. If no caching strategy is chosen, the injector defaults to using Map. If more than one strategy is selected, the first strategy will be used.
Returns:
- Injector with self already registered as a singleton.
func (*Injector) Call ¶
Call checks a function's signature then calls the function by injecting all the arguments. Call is used for any function that has no return values.
Parameters:
- function is the function to be called with injected arguments.
Returns:
- err returns any error encountered during the call.
Error:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func (*Injector) Call1 ¶
Call1 checks a function's signature then calls the function by injecting all the arguments. Call1 is used for any function that has exactly one return value.
Parameters:
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func (*Injector) Call2 ¶
Call2 checks a function's signature then calls the function by injecting all the arguments. Call2 is used for any function that has exactly two return values.
Parameters:
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- r2 is return value 2.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func (*Injector) Call3 ¶
Call3 checks a function's signature then calls the function by injecting all the arguments. Call3 is used for any function that has exactly three return values.
Parameters:
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- r2 is return value 2.
- r3 is return value 3.
- err returns any error encountered during the call.
Returns:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func (*Injector) Call4 ¶
Call4 checks a function's signature then calls the function by injecting all the arguments. Call4 is used for any function that has exactly four return values.
Parameters:
- function is the function to be called with injected arguments.
Returns:
- r1 is return value 1.
- r2 is return value 2.
- r3 is return value 3.
- r4 is return value 4.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
- if the function provided has an incongruent number of return values.
func (*Injector) CallN ¶
CallN checks a function's signature then calls the function by injecting all the arguments. CallN is used for any function with any number of return values.
Parameters:
- function is the function to be called with injected arguments.
Returns:
- returns contains all return values.
- err returns any error encountered during the call.
Errors:
- if calling Get on any of the argument types would error.
- if the function provided is not a function.
- if the function provided is variadic.
func (*Injector) Get ¶
Get retrieves the given type using the registered constructor or instance.
Parameters:
- key is the type to look for a registered instance or constructor for.
Returns:
- The registered instance or the result of the registered constructor.
- err is nil unless an error occurred during retrieval.
Errors:
- if Verify() has not been called.
- if Verify() returned an error.
func (*Injector) GetByName ¶
GetByName retrieves the named type using the registered constructor or instance. The name is expected to be truncated down to type name only, package should not be included in the name. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.
Parameters:
- name is the name of the type that should have been registered. Expected to be truncated down to type name only, package should not be included. Pointer "*" symbols are also assumed to be stripped from the name and should not be included.
Returns:
- value is the registered instance or the result of the registered constructor.
- err is nil unless the named type cannot be found.
Errors:
- ErrorNotRegistered is returned if the named type cannot be found.
Errors:
- if Verify() has not been called.
- if Verify() returned an error.
func (*Injector) RegisterScope ¶
RegisterScope adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.
Notes:
- Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use Injector.RegisterScopeError.
Parameters:
- key is the registered type that the constructor will be registered with.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func (*Injector) RegisterScopeError ¶
RegisterScopeError adds a constructor for the given type. Every time the type is requested in a unique Get() call, the same instance is always returned. If it's requested again in a new Get() call, the constructor is called and a new instance is returned.
Notes:
- Constructor is expected to return (Tkey, error). If your constructor does not return an error, use Injector.RegisterScope instead.
Parameters:
- key is the registered type that the constructor will be registered with.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func (*Injector) RegisterSingleton ¶
RegisterSingleton adds a constructor for the given type. Every time the type is requested, the same instance is always returned.
Notes:
- Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use Injector.RegisterSingletonError
Parameters:
- key is the registered type that the constructor will be registered with.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func (*Injector) RegisterSingletonError ¶
RegisterSingletonError adds a constructor for the given type. Every time the type is requested, the same instance is always returned.
Notes:
- Constructor is expected to return (Tkey, error). If your constructor does not return an error, use Injector.RegisterSingleton instead.
Parameters:
- key is the registered type that the constructor will be registered with.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func (*Injector) RegisterTransient ¶
RegisterTransient adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.
Notes:
- Constructor is expected to be a function that returns exactly one value. if the constructor also returns an error, use Injector.RegisterTransientError.
Parameters:
- key is the registered type that the constructor will be registered with.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.
func (*Injector) RegisterTransientError ¶
RegisterTransientError adds a constructor for the given type. Every time the type is requested, the constructor is always called and a new instance is returned.
Notes:
- Constructor is expected to return (Tkey, error). If your constructor does not return an error, use Injector.RegisterTransient instead.
Parameters:
- key is the registered type that the constructor will be registered with.
- constructor is the requisite function to generate the type.
Errors:
- ErrorAlreadyRegistered is returned when a type has already been registered.
- ErrorNoReturns is returned when a constructor has no return value.
- ErrorNotAFunction is returned when a non-function is passed as a constructor.
- ErrorNotAssignable is returned when a constructor returns a type that cannot be assigned to the key type.
- ErrorNotStructOrInterface is returned when a type is not registerable.
- ErrorTooManyReturns is returned when a constructor has more than 1 return value.
- ErrorVariadicArguments is returned when a constructor has a variadic signature.