Skip to content

Commit c2635c9

Browse files
author
Nicholas Thomson
committed
Fix overwriting spec paths for empty reference slices
1 parent eae4aea commit c2635c9

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

pkg/generate/code/resource_reference.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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\tfor _, %s := range %s {\n", indent, iterVarName, fieldAccessPrefix)
251253

252254
fieldAccessPrefix = iterVarName
253-
outPrefix += fmt.Sprintf("%s\tarr := %s.From\n", indent, fieldAccessPrefix)
254-
outPrefix += fmt.Sprintf("%s\tif arr == nil || arr.Name == nil || *arr.Name == \"\" {\n", indent)
255-
outPrefix += fmt.Sprintf("%s\t\treturn 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\tarr := %s.From\n", indent, fieldAccessPrefix)
256+
outPrefix += fmt.Sprintf("%s\t\tif arr == nil || arr.Name == nil || *arr.Name == \"\" {\n", indent)
257+
outPrefix += fmt.Sprintf("%s\t\t\treturn 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")

pkg/generate/code/resource_reference_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,20 @@ func Test_ResolveReferencesForField_SliceOfReferences(t *testing.T) {
269269
crd := testutil.GetCRDByName(t, g, "VpcLink")
270270
require.NotNil(crd)
271271
expected :=
272-
` ko.Spec.SecurityGroupIDs = []*string{}
273-
for _, iter0 := range ko.Spec.SecurityGroupRefs {
274-
arr := iter0.From
275-
if arr == nil || arr.Name == nil || *arr.Name == "" {
276-
return fmt.Errorf("provided resource reference is nil or empty: SecurityGroupRefs")
277-
}
278-
obj := &ec2apitypes.SecurityGroup{}
279-
if err := getReferencedResourceState_SecurityGroup(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
280-
return err
272+
` if len(ko.Spec.SecurityGroupRefs) > 0 {
273+
resolved0 := []*string{}
274+
for _, iter0 := range ko.Spec.SecurityGroupRefs {
275+
arr := iter0.From
276+
if arr == nil || arr.Name == nil || *arr.Name == "" {
277+
return fmt.Errorf("provided resource reference is nil or empty: SecurityGroupRefs")
278+
}
279+
obj := &ec2apitypes.SecurityGroup{}
280+
if err := getReferencedResourceState_SecurityGroup(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
281+
return err
282+
}
283+
resolved0 = append(resolved0, (*string)(obj.Status.ID))
281284
}
282-
ko.Spec.SecurityGroupIDs = append(ko.Spec.SecurityGroupIDs, (*string)(obj.Status.ID))
285+
ko.Spec.SecurityGroupIDs = resolved0
283286
}
284287
`
285288

0 commit comments

Comments
 (0)