123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package di
- import (
- "fmt"
- "strings"
- "go.uber.org/dig"
- "kpt-event/util/di/xreflect"
- )
- type Option = dig.Option
- type ProvideOption = dig.ProvideOption
- type InvokeOption = dig.InvokeOption
- type HubOption interface {
- fmt.Stringer
- Apply(hub *Hub)
- }
- func Options(opts ...HubOption) HubOption {
- return optionGroup(opts)
- }
- type optionGroup []HubOption
- func (og optionGroup) Apply(hub *Hub) {
- for _, opt := range og {
- opt.Apply(hub)
- }
- }
- func (og optionGroup) String() string {
- items := make([]string, len(og))
- for i, opt := range og {
- items[i] = fmt.Sprint(opt)
- }
- return fmt.Sprintf("di.Options(%s)", strings.Join(items, ", "))
- }
- func Annotation(f func(ann *Annotated)) HubOption {
- annotation := &Annotated{}
- f(annotation)
- return provideOption{
- Targets: []interface{}{*annotation},
- Stack: xreflect.CallerStack(1, 0),
- }
- }
- func Provide(constructors ...interface{}) HubOption {
-
- for _, value := range constructors {
- switch value.(type) {
- case nil:
- panic("untyped nil passed to di.Provide")
- case error:
- panic("error value passed to di.Provide")
- }
- }
- return provideOption{
- Targets: constructors,
- Stack: xreflect.CallerStack(1, 0),
- }
- }
- type provideOption struct {
- Targets []interface{}
- Stack xreflect.Stack
- }
- func (o provideOption) Apply(hub *Hub) {
- for _, target := range o.Targets {
- hub.provides = append(hub.provides, Provided{
- Target: target,
- Stack: o.Stack,
- })
- }
- }
- func (o provideOption) String() string {
- items := make([]string, len(o.Targets))
- for i, c := range o.Targets {
- items[i] = xreflect.FuncName(c)
- }
- return fmt.Sprintf("fx.Provide(%s)", strings.Join(items, ", "))
- }
- func ValidateHub(v bool) HubOption {
- return validateOption{validate: v}
- }
- type validateOption struct {
- validate bool
- }
- func (o validateOption) Apply(hub *Hub) {
- hub.validate = o.validate
- }
- func (o validateOption) String() string {
- return fmt.Sprintf("fx.validate(%v)", o.validate)
- }
|