Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidTarget = errors.New("invalid target to Fetch")
ErrInvalidTarget is returned when the target passed to Fetch is not a pointer
Functions ¶
This section is empty.
Types ¶
type BaseContainer ¶
type BaseContainer struct {
// contains filtered or unexported fields
}
BaseContainer is the container implementation of this package.
func (*BaseContainer) Fetch ¶
func (c *BaseContainer) Fetch(target interface{}) (err error)
Fetch builds a value out of the container to fill the given target, which must be a pointer.
Matching is done by type.
It returns an error in the following cases:
- the target is not a pointer,
- there is no provider for the target type,
- it detects a cycle,
- the provider returns an error,
- the provider panics.
func (*BaseContainer) LogTo ¶
func (c *BaseContainer) LogTo(l *log.Logger)
LogTo sets the container logger, for debugging purpose.
func (*BaseContainer) Register ¶
func (c *BaseContainer) Register(p Provider)
Register registers the given provider.
It panics if the provider key has already been registered.
func (*BaseContainer) RegisterFrom ¶
func (c *BaseContainer) RegisterFrom(struc interface{})
RegisterFrom uses reflection to register constants and methods from the given struct.
type BuildError ¶
type BuildError struct { // The provider that failed. Provider Provider }
BuildError is the error returned when the provider returns an invalid reflect.Value.
func (*BuildError) Error ¶
func (e *BuildError) Error() string
type BuildPanicError ¶
type BuildPanicError struct { // The provider that paniced. Provider Provider // The panic value as an error. Err error }
BuildPanicError is the error returned when the provider panics.
func (*BuildPanicError) Error ¶
func (e *BuildPanicError) Error() string
type ConstantProvider ¶
ConstantProvider holds a value to return as is.
func (*ConstantProvider) Key ¶
func (c *ConstantProvider) Key() interface{}
Key returns the constant type.
func (*ConstantProvider) Provide ¶
func (c *ConstantProvider) Provide(Container) (reflect.Value, error)
Provide simply returns the constant.
func (*ConstantProvider) String ¶
func (c *ConstantProvider) String() string
type Container ¶
type Container interface { // Register a new Provider. Register(Provider) // Fetch sets target to a value matching its type and built from the container. Fetch(target interface{}) error }
Container is the generic container interface
type CycleError ¶
type CycleError struct { // The list of provider involved in the cycle. Providers []Provider }
CycleError is the error returned when the container detects a cycle.
Example ¶
// Container setup ctn := New() ctn.Register(Func(strconv.Itoa)) // func Itoa(i int) string ctn.Register(Func(strconv.Atoi)) // func Atoi(s string) (int, error) // Container use var a int err := ctn.Fetch(&a) fmt.Print(err)
Output: cannot inject argument #0 of func(string) (int, error): cannot inject argument #0 of func(int) string: cycle involving these providers: [Singleton(func(string) (int, error)) Singleton(func(int) string)]
func (*CycleError) Error ¶
func (e *CycleError) Error() string
type FuncArgumentError ¶
type FuncArgumentError struct { // The provider that failed. Func *FuncProvider // The returned error. Err error // The argument position. Index int }
FuncArgumentError is returned by FuncProvider.Provider when an argument cannot be pulled from the container.
func (*FuncArgumentError) Error ¶
func (e *FuncArgumentError) Error() string
type FuncCallError ¶
type FuncCallError struct { // The provider that failed. Func *FuncProvider // The returned error. Err error // The arguments that was passed to the function. Args []reflect.Value }
FuncCallError is returned when the func returned an actual error as its second return value.
func (*FuncCallError) Error ¶
func (e *FuncCallError) Error() string
type FuncProvider ¶
type FuncProvider struct { // The function itself. Func reflect.Value // The types of its arguments. ArgumentTypes []reflect.Type // The type of the firstr returned valued. ReturnType reflect.Type // Indicates that the function returns an error in second position. ReturnsError bool }
FuncProvider wraps a function to build the wanted value from arguments pulled from the container.
func (*FuncProvider) Key ¶
func (p *FuncProvider) Key() interface{}
Key returns the type of the first return value of the function.
func (*FuncProvider) Provide ¶
func (p *FuncProvider) Provide(container Container) (value reflect.Value, err error)
Provide fetchs the function argments by type from the container and then call the functions.
If the function returns an error, it is wrapped and returned by Provide.
func (*FuncProvider) String ¶
func (p *FuncProvider) String() string
String returns the function signature.
type NoProviderError ¶
type NoProviderError struct {
// The key that was not found.
Key interface{}
}
NoProviderError is the error returned when there is no provider for a given key in the container.
func (*NoProviderError) Error ¶
func (e *NoProviderError) Error() string
type Provider ¶
type Provider interface { // Provide is used to build the value. // The Container can be used to pull in dependencies needed to build the value. Provide(Container) (reflect.Value, error) // Key returns a value used to index to this provider in the Container. // This can be anything, but the expected types are string and reflect.Type. Key() interface{} // This is not strictly required but it is very useful for debugging. fmt.Stringer }
Provider defines an interface for building values out of a Container.
func Constant ¶
func Constant(value interface{}) Provider
Constant creates a ConstantProvider for the given value.
Example ¶
// Container setup ctn := New() ctn.Register(Constant("/etc/hosts")) // Container use var path string if err := ctn.Fetch(&path); err != nil { panic(err) } fmt.Print(path)
Output: /etc/hosts
func Func ¶
func Func(fn interface{}) Provider
Func builds a FuncProvider for the given function.
The returned provided is a Singleton, to ensure the function is called only once.
Func panics if the function does not respect the following conditions:
- The function returns less than one value or more than two.
- If the function returns two values, the second one must be of type error.
Example ¶
// Container setup ctn := New() ctn.Register(Func(strconv.Itoa)) ctn.Register(Constant(25)) // Container use var s string if err := ctn.Fetch(&s); err != nil { panic(err) } fmt.Println(s)
Output: 25
type Singleton ¶
type Singleton struct { // The actual provider Provider // contains filtered or unexported fields }
Singleton wraps another provider to guarantee it is used only once.
Example ¶
// Container setup ctn := New() // Func returns an already-Singleton-wrapped provider ctn.Register(Func(func() int { fmt.Println("Called !") return 5 })) // Container use var a, b int if err := ctn.Fetch(&a); err != nil { panic(err) } if err := ctn.Fetch(&b); err != nil { panic(err) } fmt.Println(a, b)
Output: Called ! 5 5