4 Star 10 Fork 2

Gitee 极速下载/go-ioc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/mylxsw/go-ioc
克隆/下载
condition.go 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
package ioc
import (
"errors"
"reflect"
)
type Conditional interface {
getInitFunc() interface{}
getOnCondition() interface{}
matched(cc Container) (bool, error)
}
type conditional struct {
init interface{}
on interface{}
}
// WithCondition 创建 Conditional 接口实例
// init 参数为传递个 Singleton/Prototype 方法的实例创建方法
// onCondition 参数支持两种形式
// - `onCondition(依赖注入参数列表...) bool`
// - `onCondition(依赖注入参数列表...) (bool, error)`
func WithCondition(init interface{}, onCondition interface{}) Conditional {
if onCondition == nil {
panic("invalid argument onCondition: can not be nil [onCondition() bool or onCondition() (bool, error)]")
}
onType := reflect.TypeOf(onCondition)
if onType.Kind() != reflect.Func {
panic("invalid argument onCondition: must be a func [onCondition() bool or onCondition() (bool, error)]")
}
argCount := onType.NumOut()
if argCount != 1 && argCount != 2 {
panic("invalid argument onCondition: onCondition() bool or onCondition() (bool, error)")
}
if onType.Out(0).Kind() != reflect.Bool {
panic("invalid argument onCondition: return value must be bool [onCondition() bool or onCondition() (bool, error)]")
}
// onCondition() (bool, error)
if argCount == 2 {
if onType.Out(1) != reflect.TypeOf(errors.New("")) {
panic("invalid argument onCondition: the second return value must be error [onCondition() (bool, error)]")
}
}
return conditional{init: init, on: onCondition}
}
func (cond conditional) getInitFunc() interface{} {
return cond.init
}
func (cond conditional) getOnCondition() interface{} {
return cond.on
}
func (cond conditional) matched(cc Container) (bool, error) {
res, err := cc.Call(cond.on)
if err != nil {
return false, err
}
if len(res) == 2 {
matched, err := res[0], res[1]
return matched.(bool), err.(error)
}
return res[0].(bool), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/go-ioc.git
git@gitee.com:mirrors/go-ioc.git
mirrors
go-ioc
go-ioc
master

搜索帮助