Documentation
¶
Index ¶
- Variables
- func IndirectType(typ reflect.Type) reflect.Type
- func IndirectValue(v reflect.Value) reflect.Value
- func IsZero(v reflect.Value) bool
- func LookupNonZeroFieldsValues(v reflect.Value, skipUnexported bool) (bindValues []reflect.Value)
- func NumFields(elemTyp reflect.Type, skipUnexported bool) int
- func ValueOf(o interface{}) reflect.Value
- func ValuesOf(valuesAsInterface []interface{}) (values []reflect.Value)
- type BindObject
- type BindType
- type Container
- func (c *Container) Add(value interface{})
- func (c *Container) AddOnce(value interface{}) bool
- func (c *Container) AddValue(val reflect.Value)
- func (c Container) Clone() Container
- func (c Container) CloneWithFieldsOf(i interface{}) Container
- func (c Container) Has(value interface{}) bool
- func (c *Container) InjectDeps(dest interface{}, ctx ...reflect.Value)
- func (c Container) Len() int
- func (c *Container) Provide(constructor interface{}) (interface{}, error)
- func (c *Container) ProvideAndRegister(constructor interface{}) error
- func (c *Container) Register(value interface{})
- func (c *Container) Remove(value interface{}, n int) bool
- type Scope
- type StructInjector
- func (s *StructInjector) Acquire() reflect.Value
- func (s *StructInjector) AcquireSlice() []reflect.Value
- func (s *StructInjector) Inject(dest interface{}, ctx ...reflect.Value)
- func (s *StructInjector) InjectElem(destElem reflect.Value, ctx ...reflect.Value)
- func (s *StructInjector) String() (trace string)
Constants ¶
This section is empty.
Variables ¶
var EmptyIn = []reflect.Value{}
EmptyIn is just an empty slice of reflect.Value.
Functions ¶
func IndirectType ¶
IndirectType returns the value of a pointer-type "typ". If "typ" is a pointer, array, chan, map or slice it returns its Elem, otherwise returns the typ as it's.
func IndirectValue ¶
IndirectValue returns the reflect.Value that "v" points to. If "v" is a nil pointer, Indirect returns a zero Value. If "v" is not a pointer, Indirect returns v.
func IsZero ¶
IsZero returns true if a value is nil. Remember; fields to be checked should be exported otherwise it returns false. Notes for users: Boolean's zero value is false, even if not set-ed. UintXX are not zero on 0 because they are pointers to.
func LookupNonZeroFieldsValues ¶
LookupNonZeroFieldsValues lookup for filled fields based on the "v" struct value instance. It returns a slice of reflect.Value (same type as `Values`) that can be binded, like the end-developer's custom values.
func NumFields ¶
NumFields returns the total number of fields, and the embedded, even if the embedded struct is not exported, it will check for its exported fields.
Types ¶
type BindObject ¶
type BindObject struct { Type reflect.Type // the Type of 'Value' or the type of the returned 'ReturnValue' . Value reflect.Value BindType BindType ReturnValue func([]reflect.Value) reflect.Value }
BindObject contains the dependency value's read-only information.
StructInjector keeps information about their input arguments/or fields, these properties contain a `BindObject` inside them.
func MakeBindObject ¶
func MakeBindObject(v reflect.Value) (b BindObject)
MakeBindObject accepts any "v" value, struct, pointer or a function
func (*BindObject) Assign ¶
func (b *BindObject) Assign(ctx []reflect.Value, toSetter func(reflect.Value))
Assign sets the values to a setter, "toSetter" contains the setter, so the caller can use it for multiple and different structs/functions as well.
func (*BindObject) IsAssignable ¶
func (b *BindObject) IsAssignable(to reflect.Type) bool
IsAssignable checks if "to" type can be used as "b.Value/ReturnValue".
type BindType ¶
type BindType uint32
BindType is the type of a binded object/value, it's being used to check if the value is accessible after a function call with a "ctx" when needed ( Dynamic type) or it's just a struct value (a service | Static type).
type Container ¶
Container is a shortcut for []reflect.Value
func (*Container) Add ¶
func (c *Container) Add(value interface{})
Add adds values as dependencies, if the struct's fields or the function's input arguments needs them, they will be defined as bindings (at build-time) and they will be used (at serve-time).
func (*Container) AddOnce ¶
AddOnce binds a value to the controller's field with the same type, if it's not binded already.
Returns false if binded already or the value is not the proper one for binding, otherwise true.
func (*Container) AddValue ¶
AddValue adds values as dependencies, if the struct's fields or the function's input arguments needs them, they will be defined as bindings (at build-time) and they will be used (at serve-time).
func (Container) CloneWithFieldsOf ¶
CloneWithFieldsOf will return a copy of the current container with provided struct fields that are filled(non-zero) by the caller
func (Container) Has ¶
Has returns true if a binder responsible to bind and return a type of "typ" is already registered to this controller.
func (*Container) InjectDeps ¶ added in v1.4.0
InjectDeps accepts a destination struct and any optional context value(s), and injects registered dependencies to the destination object
func (*Container) Provide ¶ added in v1.4.0
Provide registers constructor function and invokes it with injected values from container to constructor
func (*Container) ProvideAndRegister ¶ added in v1.4.0
ProvideAndRegister registers constructor function and creates instances and register them to container
func (*Container) Register ¶ added in v1.4.0
func (c *Container) Register(value interface{})
Register appends one or more values as dependecies
func (*Container) Remove ¶
Remove unbinds a binding value based on the type, it returns true if at least one field is not binded anymore.
The "n" indicates the number of elements to remove, if <=0 then it's 1, this is useful because you may have bind more than one value to two or more fields with the same type.
type Scope ¶
type Scope uint8
Scope is the struct injector's struct value scope/permant state. See `Stateless` and `Singleton`.
const ( // Stateless is the scope that the struct should be different on each binding, // think it like `Request Scoped`, per-request struct for mvc. Stateless Scope = iota // Singleton is the scope that the struct is the same // between calls, it has no dynamic dependencies or // any unexported fields that is not seted on creation, // so it doesn't need to be created on each call/request. Singleton )
type StructInjector ¶
type StructInjector struct { // is true when contains bindable fields and it's a valid target struct, // it maybe 0 but struct may contain unexported fields or exported but no bindable (Stateless) // see `setState`. Has bool CanInject bool // if any bindable fields when the state is NOT singleton. Scope Scope // contains filtered or unexported fields }
StructInjector keeps the data that are needed in order to do the binding injection as fast as possible and with the best possible and safest way.
func MakeStructInjector ¶
func MakeStructInjector(v reflect.Value, values ...reflect.Value) *StructInjector
MakeStructInjector returns a new struct injector, which will be the object that the caller should use to bind exported fields or embedded unexported fields that contain exported fields of the "v" struct value or pointer.
func Struct ¶
func Struct(s interface{}, values ...reflect.Value) *StructInjector
Struct is being used to return a new injector based on a struct value instance, if it contains fields that the types of those are matching with one or more of the `Values` then they are binded with the injector's `Inject` and `InjectElem` methods.
func (*StructInjector) Acquire ¶
func (s *StructInjector) Acquire() reflect.Value
Acquire returns a new value of the struct or the same struct that is used for resolving the dependencies. If the scope is marked as singleton then it returns the first instance, otherwise it creates new and returns it.
See `Singleton` and `Stateless` for more.
func (*StructInjector) AcquireSlice ¶
func (s *StructInjector) AcquireSlice() []reflect.Value
AcquireSlice same as `Acquire` but it returns a slice of values structs, this can be used when a struct is passed as an input parameter on a function, again if singleton then it returns a pre-created slice which contains the first struct value given by the struct injector's user.
func (*StructInjector) Inject ¶
func (s *StructInjector) Inject(dest interface{}, ctx ...reflect.Value)
Inject accepts a destination struct and any optional context value(s),
func (*StructInjector) InjectElem ¶
func (s *StructInjector) InjectElem(destElem reflect.Value, ctx ...reflect.Value)
InjectElem same as `Inject` but accepts a reflect.Value and bind the necessary fields directly.
func (*StructInjector) String ¶
func (s *StructInjector) String() (trace string)
String returns a debug trace message.