@@ -50,6 +50,20 @@ type Invoker interface {
5050 Invoke (interface {}) ([]reflect.Value , error )
5151}
5252
53+ // FastInvoker represents an interface for fast calling functions external function.
54+ type FastInvoker interface {
55+ // Invoke
56+ Invoke ([]interface {}) ([]reflect.Value , error )
57+ }
58+
59+ // IsFastInvoker check interface is FastInvoker
60+ func IsFastInvoker (h interface {}) bool {
61+ if _ , ok := h .(FastInvoker ); ok {
62+ return true
63+ }
64+ return false
65+ }
66+
5367// TypeMapper represents an interface for mapping interface{} values based on type.
5468type TypeMapper interface {
5569 // Maps the interface{} value based on its immediate type from reflect.TypeOf.
@@ -102,18 +116,52 @@ func New() Injector {
102116// It panics if f is not a function
103117func (inj * injector ) Invoke (f interface {}) ([]reflect.Value , error ) {
104118 t := reflect .TypeOf (f )
119+ numIn := t .NumIn ()
120+ switch v := f .(type ) {
121+ case FastInvoker :
122+ return inj .fastInvoke (v , t , numIn )
123+ default :
124+ return inj .callInvoke (f , t , numIn )
125+ }
126+ }
105127
106- var in = make ([]reflect.Value , t .NumIn ()) //Panic if t is not kind of Func
107- for i := 0 ; i < t .NumIn (); i ++ {
108- argType := t .In (i )
109- val := inj .GetVal (argType )
110- if ! val .IsValid () {
111- return nil , fmt .Errorf ("Value not found for type %v" , argType )
112- }
128+ // fastInvoke fastInvoke call
129+ func (inj * injector ) fastInvoke (f FastInvoker , t reflect.Type , numIn int ) ([]reflect.Value , error ) {
130+ var in []interface {}
131+ if numIn > 0 {
132+ in = make ([]interface {}, numIn ) //Panic if t is not kind of Func
133+ var argType reflect.Type
134+ var val reflect.Value
135+ for i := 0 ; i < numIn ; i ++ {
136+ argType = t .In (i )
137+ val = inj .GetVal (argType )
138+ if ! val .IsValid () {
139+ return nil , fmt .Errorf ("Value not found for type %v" , argType )
140+ }
113141
114- in [i ] = val
142+ in [i ] = val .Interface ()
143+ }
115144 }
145+ return f .Invoke (in )
146+ }
116147
148+ // callInvoke reflect.Value.Call
149+ func (inj * injector ) callInvoke (f interface {}, t reflect.Type , numIn int ) ([]reflect.Value , error ) {
150+ var in []reflect.Value
151+ if numIn > 0 {
152+ in = make ([]reflect.Value , numIn ) //Panic if t is not kind of Func
153+ var argType reflect.Type
154+ var val reflect.Value
155+ for i := 0 ; i < numIn ; i ++ {
156+ argType = t .In (i )
157+ val = inj .GetVal (argType )
158+ if ! val .IsValid () {
159+ return nil , fmt .Errorf ("Value not found for type %v" , argType )
160+ }
161+
162+ in [i ] = val
163+ }
164+ }
117165 return reflect .ValueOf (f ).Call (in ), nil
118166}
119167
0 commit comments