11package main
22
33import (
4+ "cmp"
45 "context"
56 "encoding/json"
67 "reflect"
@@ -63,7 +64,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
6364
6465 // The request response cycle for the Crossplane ExtraResources API requires that function-extra-resources
6566 // tells Crossplane what it wants.
66- // Then a new rquest is sent to function-extra-resources with those resources present at the ExtraResources field.
67+ // Then a new request is sent to function-extra-resources with those resources present at the RequiredResources field.
6768 //
6869 // function-extra-resources does not know if it has requested the resources already or not.
6970 //
@@ -73,7 +74,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
7374 return rsp , nil
7475 }
7576
76- // Pull extra resources from the ExtraResources request field.
77+ // Pull extra resources from the RequiredResources request field.
7778 extraResources , err := request .GetRequiredResources (req )
7879 if err != nil {
7980 response .Fatal (rsp , errors .Errorf ("fetching extra resources %T: %w" , req , err ))
@@ -92,7 +93,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
9293 //
9394 // TODO(reedjosh): look into resources.AsStruct or simlar since unsturctured k8s objects are already almost json.
9495 // structpb.NewList(v []interface{}) should create an array like.
95- // Combining this and similar structures from the structpb lib should should be done to create
96+ // Combining this and similar structures from the structpb lib should be done to create
9697 // a map[string][object] container into which the found extra resources can be dumped.
9798 //
9899 // The found extra resources should then be directly marhsal-able via:
@@ -116,13 +117,13 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
116117
117118// Build requirements takes input and outputs an array of external resoruce requirements to request
118119// from Crossplane's external resource API.
119- func buildRequirements (in * v1beta1.Input , xr * resource.Composite ) (* fnv1.Requirements , error ) { //nolint:gocyclo // Adding non-nil validations increases function complexity.
120- extraResources := make (map [string ]* fnv1.ResourceSelector , len (in .Spec .ExtraResources ))
120+ func buildRequirements (in * v1beta1.Input , xr * resource.Composite ) (* fnv1.Requirements , error ) { //nolint:gocyclo,gocognit // Adding non-nil validations increases function complexity.
121+ resources := make (map [string ]* fnv1.ResourceSelector , len (in .Spec .ExtraResources ))
121122 for _ , extraResource := range in .Spec .ExtraResources {
122123 extraResName := extraResource .Into
123124 switch extraResource .Type {
124125 case v1beta1 .ResourceSourceTypeReference , "" :
125- extraResources [extraResName ] = & fnv1.ResourceSelector {
126+ resources [extraResName ] = & fnv1.ResourceSelector {
126127 ApiVersion : extraResource .APIVersion ,
127128 Kind : extraResource .Kind ,
128129 Match : & fnv1.ResourceSelector_MatchName {
@@ -156,7 +157,7 @@ func buildRequirements(in *v1beta1.Input, xr *resource.Composite) (*fnv1.Require
156157 if len (matchLabels ) == 0 {
157158 continue
158159 }
159- extraResources [extraResName ] = & fnv1.ResourceSelector {
160+ resources [extraResName ] = & fnv1.ResourceSelector {
160161 ApiVersion : extraResource .APIVersion ,
161162 Kind : extraResource .Kind ,
162163 Match : & fnv1.ResourceSelector_MatchLabels {
@@ -166,7 +167,7 @@ func buildRequirements(in *v1beta1.Input, xr *resource.Composite) (*fnv1.Require
166167 }
167168 }
168169 }
169- return & fnv1.Requirements {Resources : extraResources }, nil
170+ return & fnv1.Requirements {Resources : resources }, nil
170171}
171172
172173// Verify Min/Max and sort extra resources by field path within a single kind.
@@ -254,35 +255,56 @@ func sortExtrasByFieldPath(extras []resource.Required, path string) error { //no
254255 if valj == nil {
255256 valj = reflect .Zero (t ).Interface ()
256257 }
257- switch t .Kind () { //nolint:exhaustive // we only support these types
258- case reflect .Float64 :
259- return vali .(float64 ) < valj .(float64 )
260- case reflect .Float32 :
261- return vali .(float32 ) < valj .(float32 )
262- case reflect .Int64 :
263- return vali .(int64 ) < valj .(int64 )
264- case reflect .Int32 :
265- return vali .(int32 ) < valj .(int32 )
266- case reflect .Int16 :
267- return vali .(int16 ) < valj .(int16 )
268- case reflect .Int8 :
269- return vali .(int8 ) < valj .(int8 )
270- case reflect .Int :
271- return vali .(int ) < valj .(int )
272- case reflect .String :
273- return vali .(string ) < valj .(string )
274- default :
275- // should never happen
276- err = errors .Errorf ("unsupported type %q for sorting" , t )
258+ less , lessErr := lessByKind (t .Kind (), vali , valj )
259+ if lessErr != nil {
260+ err = lessErr
277261 return false
278262 }
263+ return less
279264 })
280265 if err != nil {
281266 return err
282267 }
283268
284- for i := 0 ; i < len ( extras ); i ++ {
269+ for i := range extras {
285270 extras [i ] = p [i ].ec
286271 }
287272 return nil
288273}
274+
275+ func lessByKind (kind reflect.Kind , a , b any ) (bool , error ) {
276+ switch kind { //nolint:exhaustive // we only support these types
277+ case reflect .Float64 :
278+ return lessAs [float64 ](a , b )
279+ case reflect .Float32 :
280+ return lessAs [float32 ](a , b )
281+ case reflect .Int64 :
282+ return lessAs [int64 ](a , b )
283+ case reflect .Int32 :
284+ return lessAs [int32 ](a , b )
285+ case reflect .Int16 :
286+ return lessAs [int16 ](a , b )
287+ case reflect .Int8 :
288+ return lessAs [int8 ](a , b )
289+ case reflect .Int :
290+ return lessAs [int ](a , b )
291+ case reflect .String :
292+ return lessAs [string ](a , b )
293+ default :
294+ return false , errors .Errorf ("unsupported type %q for sorting" , kind )
295+ }
296+ }
297+
298+ func lessAs [T cmp.Ordered ](a , b any ) (bool , error ) {
299+ av , ok := a .(T )
300+ if ! ok {
301+ var t T
302+ return false , errors .Errorf ("cannot compare type %T as %T" , a , t )
303+ }
304+ bv , ok := b .(T )
305+ if ! ok {
306+ var t T
307+ return false , errors .Errorf ("cannot compare type %T as %T" , b , t )
308+ }
309+ return cmp .Less (av , bv ), nil
310+ }
0 commit comments