di

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

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

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 Container

type Container []reflect.Value

Container is a shortcut for []reflect.Value

func NewContainer

func NewContainer() Container

NewContainer returns new empty Container

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

func (c *Container) 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 (*Container) AddValue

func (c *Container) AddValue(val reflect.Value)

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

func (c Container) Clone() Container

Clone returns a copy of the current value

func (Container) CloneWithFieldsOf

func (c Container) CloneWithFieldsOf(i interface{}) Container

CloneWithFieldsOf will return a copy of the current container with provided struct fields that are filled(non-zero) by the caller

func (Container) Has

func (c Container) 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 (*Container) InjectDeps added in v1.4.0

func (c *Container) InjectDeps(dest interface{}, ctx ...reflect.Value)

InjectDeps accepts a destination struct and any optional context value(s), and injects registered dependencies to the destination object

func (Container) Len

func (c Container) Len() int

Len returns Length of current Container slice

func (*Container) Provide added in v1.4.0

func (c *Container) Provide(constructor interface{}) (interface{}, error)

Provide registers constructor function and invokes it with injected values from container to constructor

func (*Container) ProvideAndRegister added in v1.4.0

func (c *Container) ProvideAndRegister(constructor interface{}) error

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

func (c *Container) 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.

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.

Jump to

Keyboard shortcuts

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