@@ -244,20 +244,24 @@ func ResolveReferencesForField(field *model.Field, sourceVarName string, indentL
244
244
fieldAccessPrefix = fmt .Sprintf ("%s.%s" , fieldAccessPrefix , cur .GetReferenceFieldName ().Camel )
245
245
246
246
iterVarName := fmt .Sprintf ("iter%d" , idx )
247
+ aggRefName := fmt .Sprintf ("resolved%d" , idx )
247
248
248
249
// base case for references in a list
249
- outPrefix += fmt .Sprintf ("%s%s = %s{}\n " , indent , targetVarName , field .GoType )
250
- outPrefix += fmt .Sprintf ("%sfor _, %s := range %s {\n " , indent , iterVarName , fieldAccessPrefix )
250
+ outPrefix += fmt .Sprintf ("%sif len(%s) > 0 {\n " , indent , fieldAccessPrefix )
251
+ outPrefix += fmt .Sprintf ("%s\t %s := %s{}\n " , indent , aggRefName , field .GoType )
252
+ outPrefix += fmt .Sprintf ("%s\t for _, %s := range %s {\n " , indent , iterVarName , fieldAccessPrefix )
251
253
252
254
fieldAccessPrefix = iterVarName
253
- outPrefix += fmt .Sprintf ("%s\t arr := %s.From\n " , indent , fieldAccessPrefix )
254
- outPrefix += fmt .Sprintf ("%s\t if arr == nil || arr.Name == nil || *arr.Name == \" \" {\n " , indent )
255
- outPrefix += fmt .Sprintf ("%s\t \t return fmt.Errorf(\" provided resource reference is nil or empty: %s\" )\n " , indent , field .ReferenceFieldPath ())
256
- outPrefix += fmt .Sprintf ("%s\t }\n " , indent )
255
+ outPrefix += fmt .Sprintf ("%s\t \ t arr := %s.From\n " , indent , fieldAccessPrefix )
256
+ outPrefix += fmt .Sprintf ("%s\t \ t if arr == nil || arr.Name == nil || *arr.Name == \" \" {\n " , indent )
257
+ outPrefix += fmt .Sprintf ("%s\t \t \ t return fmt.Errorf(\" provided resource reference is nil or empty: %s\" )\n " , indent , field .ReferenceFieldPath ())
258
+ outPrefix += fmt .Sprintf ("%s\t \t }\n " , indent )
257
259
258
- outPrefix += getReferencedStateForField (field , indentLevel + idx )
260
+ outPrefix += getReferencedStateForField (field , indentLevel + idx + 1 )
259
261
260
- outPrefix += fmt .Sprintf ("%s\t %s = append(%s, (%s)(obj.%s))\n " , indent , targetVarName , targetVarName , field .ShapeRef .Shape .MemberRef .GoType (), field .FieldConfig .References .Path )
262
+ outPrefix += fmt .Sprintf ("%s\t \t %s = append(%s, (%s)(obj.%s))\n " , indent , aggRefName , aggRefName , field .ShapeRef .Shape .MemberRef .GoType (), field .FieldConfig .References .Path )
263
+ outPrefix += fmt .Sprintf ("%s\t }\n " , indent )
264
+ outPrefix += fmt .Sprintf ("%s\t %s = %s\n " , indent , targetVarName , aggRefName )
261
265
outPrefix += fmt .Sprintf ("%s}\n " , indent )
262
266
case ("map" ):
263
267
panic ("references cannot be within a map" )
0 commit comments