Documentation ¶
Overview ¶
Package di provides dependency injection for the Iris Hero and Iris MVC new features. It's used internally by "hero" and "mvc" packages directly.
Index ¶
- Variables
- func IndirectType(typ reflect.Type) reflect.Type
- func IndirectValue(v reflect.Value) reflect.Value
- func IsError(typ reflect.Type) bool
- func IsFunc(kindable interface{ ... }) bool
- func IsNil(v reflect.Value) bool
- func IsZero(v reflect.Value) bool
- func LookupNonZeroFieldsValues(v reflect.Value, skipUnexported bool) (bindValues []reflect.Value)
- func MakeReturnValue(fn reflect.Value, goodFunc TypeChecker) (func([]reflect.Value) reflect.Value, reflect.Type, error)
- 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 D
- type FuncInjector
- type Hijacker
- type Scope
- type Sorter
- 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)
- type TypeChecker
- type Values
- func (bv *Values) Add(values ...interface{})
- func (bv *Values) AddOnce(value interface{}) bool
- func (bv *Values) AddValues(values ...reflect.Value)
- func (bv Values) Clone() Values
- func (bv Values) CloneWithFieldsOf(s interface{}) Values
- func (bv Values) Has(value interface{}) bool
- func (bv Values) Len() int
- func (bv *Values) Remove(value interface{}, n int) bool
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultHijacker is the hijacker used on the package-level Struct & Func functions. DefaultHijacker Hijacker // DefaultTypeChecker is the typechecker used on the package-level Struct & Func functions. DefaultTypeChecker TypeChecker )
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 IsNil ¶
IsNil same as `reflect.IsNil` but a bit safer to use, returns false if not a correct type.
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 MakeReturnValue ¶
func MakeReturnValue(fn reflect.Value, goodFunc TypeChecker) (func([]reflect.Value) reflect.Value, reflect.Type, error)
MakeReturnValue takes any function that accept custom values and returns something, it returns a binder function, which accepts a slice of reflect.Value and returns a single one reflect.Value for that. It's being used to resolve the input parameters on a "x" consumer faster.
The "fn" can have the following form: `func(myService) MyViewModel`.
The return type of the "fn" should be a value instance, not a pointer, for your own protection. The binder function should return only one value.
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. FuncInjector and StructInjector keeps information about their input arguments/or fields, these properties contain a `BindObject` inside them.
func MakeBindObject ¶
func MakeBindObject(v reflect.Value, goodFunc TypeChecker) (b BindObject, err error)
MakeBindObject accepts any "v" value, struct, pointer or a function and a type checker that is used to check if the fields (if "v.elem()" is struct) or the input arguments (if "v.elem()" is func) are valid to be included as the final object's dependencies, even if the caller added more the "di" is smart enough to select what each "v" needs and what not before serve time.
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 D ¶
type D struct { Values // contains filtered or unexported fields }
D is the Dependency Injection container, it contains the Values that can be changed before the injectors. `Struct` and the `Func` methods returns an injector for specific struct instance-value or function.
func New ¶
func New() *D
New creates and returns a new Dependency Injection container. See `Values` field and `Func` and `Struct` methods for more.
func (*D) Clone ¶
Clone returns a new Dependency Injection container, it adopts the parent's (current "D") hijacker, good func type checker, sorter and all dependencies values.
func (*D) Func ¶
func (d *D) Func(fn interface{}) *FuncInjector
Func is being used to return a new injector based on a function, if it contains input arguments that the types of those are matching with one or more of the `Values` then they are binded to the function's input argument when called with the injector's `Inject` method.
func (*D) GoodFunc ¶
func (d *D) GoodFunc(fn TypeChecker) *D
GoodFunc sets a type checker for a valid function that can be binded, read the `TypeChecker` type for more explanation.
func (*D) Struct ¶
func (d *D) Struct(s interface{}) *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.
type FuncInjector ¶
type FuncInjector struct { // Length is the number of the valid, final binded input arguments. Length int // Valid is True when `Length` is > 0, it's statically set-ed for // performance reasons. Has bool // contains filtered or unexported fields }
FuncInjector 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 Func ¶
func Func(fn interface{}, values ...reflect.Value) *FuncInjector
Func is being used to return a new injector based on a function, if it contains input arguments that the types of those are matching with one or more of the `Values` then they are binded to the function's input argument when called with the injector's `Inject` method.
func MakeFuncInjector ¶
func MakeFuncInjector(fn reflect.Value, hijack Hijacker, goodFunc TypeChecker, values ...reflect.Value) *FuncInjector
MakeFuncInjector returns a new func injector, which will be the object that the caller should use to bind input arguments of the "fn" function.
The hijack and the goodFunc are optional, the "values" is the dependencies collection.
func (*FuncInjector) Call ¶
func (s *FuncInjector) Call(ctx ...reflect.Value) []reflect.Value
Call calls the "Inject" with a new slice of input arguments that are computed by the length of the input argument from the MakeFuncInjector's "fn" function.
If the function needs a receiver, so the caller should be able to in[0] = receiver before injection, then the `Inject` method should be used instead.
func (*FuncInjector) Inject ¶
func (s *FuncInjector) Inject(in *[]reflect.Value, ctx ...reflect.Value)
Inject accepts an already created slice of input arguments and fills them, the "ctx" is optional and it's used on the dependencies that depends on one or more input arguments, these are the "ctx".
func (*FuncInjector) Retry ¶
func (s *FuncInjector) Retry(retryFn func(inIndex int, inTyp reflect.Type) (reflect.Value, bool)) bool
Retry used to add missing dependencies, i.e path parameter builtin bindings if not already exists in the `hero.Handler`, once, only for that func injector.
func (*FuncInjector) String ¶
func (s *FuncInjector) String() (trace string)
String returns a debug trace text.
type Hijacker ¶
type Hijacker func(reflect.Type) (*BindObject, bool)
Hijacker is a type which is used to catch fields or function's input argument to bind a custom object based on their 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 Sorter ¶
Sorter is the type for sort customization of a struct's fields and its available bindable values.
Sorting applies only when a field can accept more than one registered value.
var SortByNumMethods Sorter = func(t1 reflect.Type, t2 reflect.Type) bool { if t1.Kind() != t2.Kind() { return true } if k := t1.Kind(); k == reflect.Interface || k == reflect.Struct { return t1.NumMethod() > t2.NumMethod() } else if k != reflect.Struct { return false } return true }
SortByNumMethods is a builtin sorter to sort fields and values based on their type and its number of methods, highest number of methods goes first.
It is the default sorter on package-level struct injector function `Struct`.
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, hijack Hijacker, goodFunc TypeChecker, sorter Sorter, 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.
The hijack and the goodFunc are optional, the "values" is the dependencies collection.
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), hero and mvc takes only one context value and this is the `context.Contex`. It applies the bindings to the "dest" struct. It calls the InjectElem.
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.
type TypeChecker ¶
TypeChecker checks if a specific field's or function input argument's is valid to be binded.
type Values ¶
Values is a shortcut of []reflect.Value, it makes easier to remove and add dependencies.
func (*Values) Add ¶
func (bv *Values) Add(values ...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 (*Values) 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 (*Values) AddValues ¶
AddValues same as `Add` but accepts reflect.Value dependencies instead of interface{} and appends them to the list if they pass some checks.
func (Values) CloneWithFieldsOf ¶
CloneWithFieldsOf will return a copy of the current values plus the "s" struct's fields that are filled(non-zero) by the caller.
func (Values) Has ¶
Has returns true if a binder responsible to bind and return a type of "typ" is already registered to this controller.
func (*Values) 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.