inject

package
v0.44.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MockFieldNameSuffixes = [...]string{"Func", "Handler"} // mock结构体字段名称后缀
)

Functions

This section is empty.

Types

type AroundFunc added in v0.5.0

type AroundFunc func(pctx ProxyContext, method reflect.Value, args []reflect.Value) []reflect.Value

func (AroundFunc) Around added in v0.5.0

func (around AroundFunc) Around(pctx ProxyContext, method reflect.Value, args []reflect.Value) []reflect.Value

type Arounder added in v0.5.0

type Arounder interface {
	Around(pctx ProxyContext, method reflect.Value, args []reflect.Value) []reflect.Value
}

type ArounderMap added in v0.6.0

type ArounderMap map[ProxyContext]AroundFunc

func (ArounderMap) Merge added in v0.6.0

func (m ArounderMap) Merge(nm ArounderMap) (mr ArounderMap)

type CtxFunc added in v0.12.0

type CtxFunc func(ctx ProxyContext, method any, args []any) (res []any)

CtxFunc 对于method: func(string, int) (int, error) f := method.(func(string, int) (int, error)) a1 := args[0].(string) a2 := args[1].(int) r1, r2 := f(a1, a2) res = append(res, r1, r2)

type IAroundFunc added in v0.5.0

type IAroundFunc interface {
	Around(pctx ProxyContext, method reflect.Value, args []reflect.Value) []reflect.Value
}

type IIoc added in v0.5.0

type IIoc interface {
	Inject(v any) (err error)
	RegisterProvider(v any) (err error)
}

type IProxyContext added in v0.5.0

type IProxyContext interface {
	LogShortf(format string, args ...any)
	Logf(format string, args ...any)
	String() string
}

type Ioc

type Ioc struct {
	// contains filtered or unexported fields
}

Ioc 控制反转,Inversion of Control

func NewIoc

func NewIoc(
	enableUnexportedFieldSetValue bool,
) *Ioc

func (*Ioc) Inject

func (ioc *Ioc) Inject(v any) (err error)

Inject 注入依赖,传入结构体,根据结构体的字段类型找到对应的provider,执行后将获得的值赋予字段

遍历v的字段,找到字段类型,再根据字段类型找到provider,调用provider获得实例,再把实例值赋给该字段 provider需要在接口定义处注册,注册到一个统一管理的地方 如果provider需要参数,则根据参数类型继续找寻相应的provider,直至初始化完成

func (*Ioc) RegisterProvider

func (ioc *Ioc) RegisterProvider(v any) (err error)

RegisterProvider 注册provider函数,形如`func New(fielda TypeA, fieldb TypeB) (T)`

type IproxyImpl added in v0.5.0

type IproxyImpl interface {
	Around(provider any, mock any, arounder Arounder) any
}

type Proxy added in v0.5.0

type Proxy interface {
	Around(provider any, mock any, arounder Arounder) any
}

Proxy 在层间依赖调用时插入钩子调用,类似AOP

func NewProxy added in v0.5.0

func NewProxy() Proxy

type ProxyContext added in v0.5.0

type ProxyContext struct {
	PkgPath       string
	InterfaceName string // TODO: 接口或结构体的名字,不为空时表示方法,为空时表示函数
	MethodName    string
}

每个包、每个接口、每个方法唯一对应一个方法

func (ProxyContext) LogShortf added in v0.5.0

func (pctx ProxyContext) LogShortf(format string, args ...any)

func (ProxyContext) Logf added in v0.5.0

func (pctx ProxyContext) Logf(format string, args ...any)

func (ProxyContext) String added in v0.5.0

func (pctx ProxyContext) String() string

func (ProxyContext) Uniq added in v0.12.0

func (pctx ProxyContext) Uniq() string

Jump to

Keyboard shortcuts

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