inject: gitea.com/macaron/inject Index | Files

package inject

import "gitea.com/macaron/inject"

Package inject provides utilities for mapping and injecting dependencies in various ways.

Index

Package Files

inject.go

func InterfaceOf Uses

func InterfaceOf(value interface{}) reflect.Type

InterfaceOf dereferences a pointer to an Interface type. It panics if value is not an pointer to an interface.

func IsFastInvoker Uses

func IsFastInvoker(h interface{}) bool

IsFastInvoker check interface is FastInvoker

type Applicator Uses

type Applicator interface {
    // Maps dependencies in the Type map to each field in the struct
    // that is tagged with 'inject'. Returns an error if the injection
    // fails.
    Apply(interface{}) error
}

Applicator represents an interface for mapping dependencies to a struct.

type FastInvoker Uses

type FastInvoker interface {
    // Invoke attempts to call the ordinary functions. If f is a function
    // with the appropriate signature, f.Invoke([]interface{}) is a Call that calls f.
    // Returns a slice of reflect.Value representing the returned values of the function.
    // Returns an error if the injection fails.
    Invoke([]interface{}) ([]reflect.Value, error)
}

FastInvoker represents an interface in order to avoid the calling function via reflection.

example:

type handlerFuncHandler func(http.ResponseWriter, *http.Request) error
func (f handlerFuncHandler)Invoke([]interface{}) ([]reflect.Value, error){
	ret := f(p[0].(http.ResponseWriter), p[1].(*http.Request))
	return []reflect.Value{reflect.ValueOf(ret)}, nil
}

type funcHandler func(int, string)
func (f funcHandler)Invoke([]interface{}) ([]reflect.Value, error){
	f(p[0].(int), p[1].(string))
	return nil, nil
}

type Injector Uses

type Injector interface {
    Applicator
    Invoker
    TypeMapper
    // SetParent sets the parent of the injector. If the injector cannot find a
    // dependency in its Type map it will check its parent before returning an
    // error.
    SetParent(Injector)
}

Injector represents an interface for mapping and injecting dependencies into structs and function arguments.

func New Uses

func New() Injector

New returns a new Injector.

type Invoker Uses

type Invoker interface {
    // Invoke attempts to call the interface{} provided as a function,
    // providing dependencies for function arguments based on Type. Returns
    // a slice of reflect.Value representing the returned values of the function.
    // Returns an error if the injection fails.
    Invoke(interface{}) ([]reflect.Value, error)
}

Invoker represents an interface for calling functions via reflection.

type TypeMapper Uses

type TypeMapper interface {
    // Maps the interface{} value based on its immediate type from reflect.TypeOf.
    Map(interface{}) TypeMapper
    // Maps the interface{} value based on the pointer of an Interface provided.
    // This is really only useful for mapping a value as an interface, as interfaces
    // cannot at this time be referenced directly without a pointer.
    MapTo(interface{}, interface{}) TypeMapper
    // Provides a possibility to directly insert a mapping based on type and value.
    // This makes it possible to directly map type arguments not possible to instantiate
    // with reflect like unidirectional channels.
    Set(reflect.Type, reflect.Value) TypeMapper
    // Returns the Value that is mapped to the current type. Returns a zeroed Value if
    // the Type has not been mapped.
    GetVal(reflect.Type) reflect.Value
}

TypeMapper represents an interface for mapping interface{} values based on type.

Package inject imports 2 packages (graph) and is imported by 1 packages. Updated 2019-08-23. Refresh now. Tools for package owners.