inject

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 4 Imported by: 3

README

inject

Dependency injection for go

This a modified version of codegangsta/inject. And refer to flamego/flamego/inject.

中文

Usage

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

type Applicator
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 Injector
// Injector represents an interface for mapping and injecting dependencies into
// structs and function arguments.
type Injector interface {
    Applicator
    Invoker
    TypeMapper
    // Reset will reset Injector, include reset mapped value and parent
    Reset()
    // 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
}

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

func New
func New() Injector

New returns a new Injector.

type Invoker
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
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(val interface{}, pointerToInterface 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.
	Get(reflect.Type) reflect.Value
}

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValueNotFound  = errors.New("value not found")
	ErrValueCanNotSet = errors.New("value can not set")
)

Functions

func InterfaceOf

func InterfaceOf(value interface{}) reflect.Type

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

func IsFastInvoker

func IsFastInvoker(handler interface{}) bool

IsFastInvoker returns true if the `handler` implements FastInvoker.

Types

type Applicator

type Applicator interface {
	// Apply 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

type FastInvoker interface {
	// Invoke attempts to call the `interface{}` provided as interface method,
	// 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)
}

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

type Injector

type Injector interface {
	Applicator
	Invoker
	TypeMapper
	// Reset will reset Injector, include reset mapped value and parent
	Reset()
	// 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
}

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

func New

func New() Injector

New returns a new Injector.

type Invoker

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

type TypeMapper interface {
	// Map maps the `interface{}` values based on their immediate type from
	// reflect.TypeOf.
	Map(...interface{}) TypeMapper
	// MapTo 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(val interface{}, pointerToInterface interface{}) TypeMapper
	// Set 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
	// Value returns the reflect.Value that is mapped to the reflect.Type. It
	// returns a zeroed reflect.Value if the Type has not been mapped.
	Value(reflect.Type) reflect.Value
	// Load value into val. It returns an error if the value is not found or value can't set.
	Load(val interface{}) error
}

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

Jump to

Keyboard shortcuts

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