di

package
v10.4.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2018 License: BSD-3-Clause, BSD-3-Clause Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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
)
View Source
var EmptyIn = []reflect.Value{}

EmptyIn is just an empty slice of reflect.Value.

Functions

func IndirectType

func IndirectType(typ reflect.Type) reflect.Type

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

func IndirectValue(v reflect.Value) reflect.Value

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 IsFunc

func IsFunc(kindable interface {
	Kind() reflect.Kind
}) bool

IsFunc returns true if the passed type is function.

func IsZero

func IsZero(v reflect.Value) bool

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

func LookupNonZeroFieldsValues(v reflect.Value, skipUnexported bool) (bindValues []reflect.Value)

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

func NumFields(elemTyp reflect.Type, skipUnexported bool) int

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.

func ValueOf

func ValueOf(o interface{}) reflect.Value

ValueOf returns the reflect.Value of "o". If "o" is already a reflect.Value returns "o".

func ValuesOf

func ValuesOf(valuesAsInterface []interface{}) (values []reflect.Value)

ValuesOf same as `ValueOf` but accepts a slice of somethings and returns a slice of reflect.Value.

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).

const (
	// Static is the simple assignable value, a static value.
	Static BindType = iota
	// Dynamic returns a value but it depends on some input arguments from the caller,
	// on serve time.
	Dynamic
)

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

func (d *D) Clone() *D

Clone returns a new Dependency Injection container, it adopts the parent's (current "D") hijacker, good func type checker 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) Hijack

func (d *D) Hijack(fn Hijacker) *D

Hijack sets a hijacker function, read the `Hijacker` 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 built'n 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 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, 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

type TypeChecker func(reflect.Type) bool

TypeChecker checks if a specific field's or function input argument's is valid to be binded.

type Values

type Values []reflect.Value

Values is a shortcut of []reflect.Value, it makes easier to remove and add dependencies.

func NewValues

func NewValues() Values

NewValues returns new empty (dependencies) values.

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

func (bv *Values) AddOnce(value interface{}) bool

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

func (bv *Values) AddValues(values ...reflect.Value)

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) Clone

func (bv Values) Clone() Values

Clone returns a copy of the current values.

func (Values) CloneWithFieldsOf

func (bv Values) CloneWithFieldsOf(s interface{}) Values

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

func (bv Values) Has(value interface{}) bool

Has returns true if a binder responsible to bind and return a type of "typ" is already registered to this controller.

func (Values) Len

func (bv Values) Len() int

Len returns the length of the current "bv" values slice.

func (*Values) Remove

func (bv *Values) Remove(value interface{}, n int) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL