Documentation ¶
Index ¶
- func DoGet[T any](i DependencyRetriever, keys ...string) (result T, err error)
- func DoGetAll[T any](i DependencyRetriever, keys ...string) (result []T, err error)
- func DoGetGen[T any](i DependencyRetriever, elements []InstancePairAny, keys ...string) (result T, err error)
- func DoGetGenFunc[T any](i DependencyRetriever, binder func(Injector) error, keys ...string) (result T, err error)
- func Get[T any](i DependencyRetriever, keys ...string) T
- func GetAll[T any](i DependencyRetriever, keys ...string) []T
- func GetGen[T any](i DependencyRetriever, elements []InstancePairAny, keys ...string) T
- func GetGenFunc[T any](i DependencyRetriever, binder func(Injector) error, keys ...string) T
- func NewCycleDetectorInjector(configs ...Config) *cycleDetectorInjector
- func Override[T any](i Injector, bind Bind[T], keys ...string)
- func Register[T any](i Injector, bind Bind[T], keys ...string)
- func RegisterConstructor[T any](i Injector, bindFunc func(binder types.Binder[T]) Bind[T], ...)
- func RegisterConstructorArgs1[T, K any](i Injector, bindFunc func(binder types.Binder[T]) Bind[T], ...)
- func RegisterConstructorArgs1Err[T, K any](i Injector, bindFunc func(binder types.Binder[T]) Bind[T], ...)
- func RegisterConstructorArgs2[T, K, P any](i Injector, bindFunc func(binder types.Binder[T]) Bind[T], ...)
- func RegisterConstructorArgs2Err[T, K, P any](i Injector, bindFunc func(binder types.Binder[T]) Bind[T], ...)
- func RegisterConstructorErr[T any](i Injector, bindFunc func(binder types.Binder[T]) Bind[T], ...)
- func RegisterInstance[T any](i Injector, value T, keys ...string)
- func RegisterSingleton[T any](i Injector, binder types.Binder[T], keys ...string)
- func SetGlobalInjector(i Injector)
- type Bind
- type BindKey
- type Config
- type ConstructorArg1
- type ConstructorArg2
- type ConstructorEmpty
- type DependencyRetriever
- type Injector
- type InstancePairAny
- type ReflectionOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoGet ¶ added in v1.2.0
func DoGet[T any](i DependencyRetriever, keys ...string) (result T, err error)
DoGet directly access a retriever and returns the type that was bound in it. Additionally, it returns an error which indicates if the bind was found or not.
Receives: DependencyRetriever (required); key (optional)
func DoGetAll ¶ added in v1.8.0
func DoGetAll[T any](i DependencyRetriever, keys ...string) (result []T, err error)
DoGetAll directly access a retriever and returns a list of element that match requested types that was bound in it. Additionally, it returns an error which indicates if the instance was found or not.
Receives: DependencyRetriever (required); key (optional)
func DoGetGen ¶ added in v1.2.0
func DoGetGen[T any]( i DependencyRetriever, elements []InstancePairAny, keys ...string, ) (result T, err error)
DoGetGen creates a sub-injector and access the retriever to generate and return a Factory bind Additionally, it returns an error which indicates if the bind was found or not.
Receives: DependencyRetriever (required); []InstancePairAny (required); key (optional)
func DoGetGenFunc ¶ added in v1.2.0
func DoGetGenFunc[T any]( i DependencyRetriever, binder func(Injector) error, keys ...string, ) (result T, err error)
DoGetGenFunc creates a sub-injector and access the retriever to generate and return a Factory bind Additionally, it returns an error which indicates if the bind was found or not.
Receives: DependencyRetriever (required); func(Injector) (required); key (optional)
func Get ¶
func Get[T any](i DependencyRetriever, keys ...string) T
Get directly access a retriever and returns the type that was bound in it.
Receives: DependencyRetriever (required); key (optional)
func GetAll ¶ added in v1.8.0
func GetAll[T any](i DependencyRetriever, keys ...string) []T
GetAll directly access a retriever and returns all instance types that was bound in it and match qualifier.
Receives: DependencyRetriever (required); key (optional)
func GetGen ¶ added in v1.1.0
func GetGen[T any](i DependencyRetriever, elements []InstancePairAny, keys ...string) T
GetGen creates a sub-injector and access the retriever to generate and return a Factory bind
Receives: DependencyRetriever (required); []InstancePairAny (required); key (optional)
func GetGenFunc ¶ added in v1.1.0
func GetGenFunc[T any](i DependencyRetriever, binder func(Injector) error, keys ...string) T
GetGenFunc creates a sub-injector and access the retriever to generate and return a Factory bind
Receives: DependencyRetriever (required); func(Injector) (required); key (optional)
func NewCycleDetectorInjector ¶ added in v1.4.0
func NewCycleDetectorInjector(configs ...Config) *cycleDetectorInjector
NewCycleDetectorInjector creates a new Injector that is able to check for cycle dependencies during runtime.
As it is much slower that the injector.StandardInjector, it is only recommended to be used in test files.
func Override ¶ added in v1.3.0
Override works like the Register function, allowing to register a bind that was already registered. It also must be called during binds setup, because the library doesn't support registering dependencies while get at same time.
This is not supported in multithreading applications because it does not have race protection
func Register ¶
Register must be called first, because the library doesn't support registering dependencies while get at same time. This is not supported in multithreading applications because it does not have race protection
func RegisterConstructor ¶ added in v1.8.2
func RegisterConstructor[T any]( i Injector, bindFunc func(binder types.Binder[T]) Bind[T], constructor func() T, keys ...string, )
RegisterConstructor registers a constructor function that returns a value of type T without an error.
func RegisterConstructorArgs1 ¶ added in v1.8.2
func RegisterConstructorArgs1[T, K any]( i Injector, bindFunc func(binder types.Binder[T]) Bind[T], constructor func(K) T, keys ...string, )
RegisterConstructorArgs1 registers a constructor function with one argument that returns a value of type T without an error.
func RegisterConstructorArgs1Err ¶ added in v1.8.2
func RegisterConstructorArgs1Err[T, K any]( i Injector, bindFunc func(binder types.Binder[T]) Bind[T], constructor func(K) (T, error), keys ...string, )
RegisterConstructorArgs1Err registers a constructor function with one argument that returns a value of type T and an error.
func RegisterConstructorArgs2 ¶ added in v1.8.2
func RegisterConstructorArgs2[T, K, P any]( i Injector, bindFunc func(binder types.Binder[T]) Bind[T], constructor func(K, P) T, keys ...string, )
RegisterConstructorArgs2 registers a constructor function with two arguments that returns a value of type T without an error.
func RegisterConstructorArgs2Err ¶ added in v1.8.2
func RegisterConstructorArgs2Err[T, K, P any]( i Injector, bindFunc func(binder types.Binder[T]) Bind[T], constructor func(K, P) (T, error), keys ...string, )
RegisterConstructorArgs2Err registers a constructor function with two arguments that returns a value of type T and an error.
func RegisterConstructorErr ¶ added in v1.8.2
func RegisterConstructorErr[T any]( i Injector, bindFunc func(binder types.Binder[T]) Bind[T], constructor func() (T, error), keys ...string, )
RegisterConstructorErr registers a constructor function that returns a value of type T and an error.
func RegisterInstance ¶
RegisterInstance directly generates an instance bind without needing to write it.
Receives: Injector (required); value (required); key (optional)
func RegisterSingleton ¶
RegisterSingleton directly generates a singleton bind without needing to write it.
Receives: Injector (required); Binder (required); key (optional)
func SetGlobalInjector ¶ added in v1.1.0
func SetGlobalInjector(i Injector)
SetGlobalInjector receives a custom injector and saves it to be used as a global injector
Types ¶
type Bind ¶
Bind is directly copy from types.Bind
func Factory ¶
Factory generates a bind that will be generated everytime a dependency with its type is requested by the Injector.
This bind don't hold any object or pointer references, and will use the given types.Binder function everytime to generate a new instance of the requested type.
As this bind doesn't hold any pointer and/or objects, there is no problem to use it in multiples goroutines at once. Just be careful with calls of the DependencyRetriever, if try to get and modify values from an Instance bind, it can end in a race-condition.
func Instance ¶
Instance generates a bind that will be registered as a single instance during bind register in the Injector.
This bind type has no protection over concurrency, so it's not recommended to be used a struct that performs some operation that will modify its attributes.
It's recommended to use it only for read value injections. For concurrent mutable binds is recommended to use Singleton or LazySingleton
func LazySingleton ¶
LazySingleton generates a bind that works the same as the Singleton, with the only difference being that it's a lazy bind. So, it only will generate the singleton instance on the first Get call.
It is useful in cases that you want to instantiate heavier objects only when it's needed.
type BindKey ¶ added in v1.5.0
BindKey is the internal type used to generate all type keys, and used to retrieve all types from the injector. Is not supposed to use directly without the remy library, as this remove the main use of the remy-generics methods
type Config ¶
type Config struct { // ParentInjector defines an Injector that will be used as a parent one, which will make possible to access it's // registered binds. ParentInjector Injector // CanOverride defines if a bind can be overridden if it is registered twice. CanOverride bool // DuckTypeElements informs to the injector that it can try to discover if the requested type // is on one of the already registered one. // // CAUTION: It costly a lot, since it will try to discover all registered elements DuckTypeElements bool // GenerifyInterfaces defines the method to check for interface binds. // If this parameter is true, then an interface that is defined in two different packages, // but has the same signature methods, will generate the same key. If is false, all interfaces will generate // a different key. GenerifyInterfaces bool // UseReflectionType defines the injector to use reflection when saving and retrieving types. // This parameter is useful when you want to use types with different modules but the same name and package names. // // Optional, default is false. UseReflectionType bool }
Config defines needed configuration to instantiate a new injector
type ConstructorArg1 ¶ added in v1.8.2
ConstructorArg1 defines a constructor function with one argument of type K that returns a value of type T and an error.
func (ConstructorArg1[T, K]) Binder ¶ added in v1.8.2
func (cons ConstructorArg1[T, K]) Binder(retriever types.DependencyRetriever) (value T, err error)
Binder retrieves the dependency of type K, then calls the constructor function for ConstructorArg1 and returns the constructed value and any error encountered.
type ConstructorArg2 ¶ added in v1.8.2
ConstructorArg2 defines a constructor function with two arguments of types K and P that returns a value of type T and an error.
func (ConstructorArg2[T, K, P]) Binder ¶ added in v1.8.2
func (cons ConstructorArg2[T, K, P]) Binder(retriever types.DependencyRetriever) (value T, err error)
Binder retrieves the dependencies of types K and P, then calls the constructor function for ConstructorArg2 and returns the constructed value and any error encountered.
type ConstructorEmpty ¶ added in v1.8.2
ConstructorEmpty defines a constructor function with no arguments that returns a value of type T and an error.
func (ConstructorEmpty[T]) Binder ¶ added in v1.8.2
func (cons ConstructorEmpty[T]) Binder(types.DependencyRetriever) (T, error)
Binder calls the constructor function for ConstructorEmpty and returns the constructed value and any error encountered.
type DependencyRetriever ¶
type DependencyRetriever = types.DependencyRetriever
type Injector ¶
func NewInjector ¶
func NewInjector(configs ...Config) Injector
type InstancePairAny ¶ added in v1.1.0
type InstancePairAny = types.InstancePair[any]
type ReflectionOptions ¶ added in v1.5.0
type ReflectionOptions = types.ReflectionOptions
ReflectionOptions All options internally used to know how and when to use the `reflect` package