@@ -74,24 +74,17 @@ func (container *ContainerInstance) resolveStructFields(instanceType reflect.Typ
7474 return instance
7575}
7676
77- // resolveFunctionArgs - Resolves the args of our function we bound to the container
78- // parameters is an array of values that we wish to provide which is optional
79- // parameters will first be assigned starting at index 0 of the functions args
80- // Then we'll look at the function args, and if we assigned a value from the parameters already
81- // it will use that, otherwise we'll look the type up in the container and resolve it
82- func (container * ContainerInstance ) resolveFunctionArgs (function reflect.Value , parameters ... any ) []reflect.Value {
77+ type FuncArgResolverInterceptor = func (index int , argType reflect.Type , typeZeroVal reflect.Value ) (reflect.Value , bool )
78+
79+ func (container * ContainerInstance ) ResolveFunctionArgsWithInterceptor (function reflect.Value , interceptor FuncArgResolverInterceptor , parameters ... any ) []reflect.Value {
8380 inArgCount := 0
8481
8582 if ! function .IsValid () || function .IsZero () {
8683 return []reflect.Value {}
8784 }
8885
89- functionType := function .Type ()
90- if functionType .Kind () == reflect .Ptr {
91- inArgCount = functionType .Elem ().NumIn ()
92- } else {
93- inArgCount = functionType .NumIn ()
94- }
86+ functionType := getType (function )
87+ inArgCount = functionType .NumIn ()
9588
9689 // We'll put the types of all in args into this array,
9790 // so we don't have to keep running .In()
@@ -118,24 +111,36 @@ func (container *ContainerInstance) resolveFunctionArgs(function reflect.Value,
118111
119112 // Assign parameter values from the provided parameters list first
120113 if len (parameters ) > 0 {
121- for i := 0 ; i < len (parameters ); i ++ {
122- paramVal := reflect .ValueOf (parameters [i ])
114+ parametersType := reflect .TypeOf (parameters )
115+
116+ // We can provide a function as an "interceptor" instead...
117+ if parametersType .Kind () == reflect .Array || parametersType .Kind () == reflect .Slice {
118+ for i := 0 ; i < len (parameters ); i ++ {
119+ paramVal := reflect .ValueOf (parameters [i ])
120+
121+ // We'll only assign the param from the provided list, if the type matches?
122+ inArg := inArgTypes [i ]
123+ if inArg == paramVal .Type () {
124+ assignArg (i , paramVal )
125+ }
126+ }
123127
124- // We'll only assign the param from the provided list, if the type matches?
125- inArg := inArgTypes [i ]
126- if inArg == paramVal .Type () {
127- assignArg (i , paramVal )
128+ // If our provided parameters fulfils all the function args, let's just early return
129+ if assignedCount >= inArgCount {
130+ return args
128131 }
129132 }
130133
131- // If our provided parameters fulfils all the function args, let's just early return
132- if assignedCount >= inArgCount {
133- return args
134- }
135134 }
136135
137136 // Now we'll try to resolve any other types from the container
138137 for i := 0 ; i < inArgCount ; i ++ {
138+ interceptedVal , didIntercept := interceptor (i , inArgTypes [i ], args [i ])
139+ if didIntercept {
140+ assignArg (i , interceptedVal )
141+ continue
142+ }
143+
139144 // We already assigned it, let's skip...
140145 if assignedArgs [i ] {
141146 continue
@@ -153,9 +158,22 @@ func (container *ContainerInstance) resolveFunctionArgs(function reflect.Value,
153158 }
154159
155160 return args
161+
162+ }
163+
164+ // ResolveFunctionArgs - Resolves the args of our function we bound to the container
165+ // parameters is an array of values that we wish to provide which is optional
166+ // parameters will first be assigned starting at index 0 of the functions args
167+ // Then we'll look at the function args, and if we assigned a value from the parameters already
168+ // it will use that, otherwise we'll look the type up in the container and resolve it
169+ func (container * ContainerInstance ) ResolveFunctionArgs (function reflect.Value , parameters ... any ) []reflect.Value {
170+ interceptor := func (index int , argType reflect.Type , typeZeroVal reflect.Value ) (reflect.Value , bool ) {
171+ return typeZeroVal , false
172+ }
173+ return container .ResolveFunctionArgsWithInterceptor (function , interceptor , parameters ... )
156174}
157175
158- // resolveFunctionArg - Used in resolveFunctionArgs , we pass an arg type and attempt to
176+ // resolveFunctionArg - Used in ResolveFunctionArgs , we pass an arg type and attempt to
159177// resolve it from the container, if the type doesn't exist in the container
160178// we'll return a zero value version of the type
161179func (container * ContainerInstance ) resolveFunctionArg (arg reflect.Type ) (reflect.Value , bool ) {
0 commit comments