@@ -244,20 +244,24 @@ func ResolveReferencesForField(field *model.Field, sourceVarName string, indentL
244244 fieldAccessPrefix = fmt .Sprintf ("%s.%s" , fieldAccessPrefix , cur .GetReferenceFieldName ().Camel )
245245
246246 iterVarName := fmt .Sprintf ("iter%d" , idx )
247+ aggRefName := fmt .Sprintf ("resolved%d" , idx )
247248
248249 // 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 )
251253
252254 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 )
257259
258- outPrefix += getReferencedStateForField (field , indentLevel + idx )
260+ outPrefix += getReferencedStateForField (field , indentLevel + idx + 1 )
259261
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 )
261265 outPrefix += fmt .Sprintf ("%s}\n " , indent )
262266 case ("map" ):
263267 panic ("references cannot be within a map" )
0 commit comments