You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was thinking the code would look something like this:
funcInterfaceOf(valueinterface{}) reflect.Type {
t:=reflect.TypeOf(value)
fort.Kind() ==reflect.Ptr {
t=t.Elem()
}
ift.Kind() ==reflect.Slice&&t.Elem().Kind() ==reflect.Interface {
returnt
}
ift.Kind() !=reflect.Interface {
panic("Called inject.InterfaceOf with a value that is not a pointer to an interface. (*MyInterface)(nil) but got "+t.Kind().String())
}
returnt
}
func (inj*injector) Invoke(finterface{}) ([]reflect.Value, error) {
t:=reflect.TypeOf(f)
varin=make([]reflect.Value, t.NumIn()) //Panic if t is not kind of Funcfori:=0; i<t.NumIn(); i++ {
argType:=t.In(i)
val:=inj.Get(argType)
ifval.IsValid() {
in[i] =valcontinue
}
if!val.IsValid() &&t.IsVariadic() &&i==t.NumIn()-1 {
in[i] =reflect.New(argType).Elem()
break
}
returnnil, fmt.Errorf("Value not found for type %v", argType)
}
ift.IsVariadic() &&len(in) ==t.NumIn() {
returnreflect.ValueOf(f).CallSlice(in), nil
}
returnreflect.ValueOf(f).Call(in), nil
}
The text was updated successfully, but these errors were encountered:
Usage like this:
I was thinking the code would look something like this:
The text was updated successfully, but these errors were encountered: