Skip to content

Commit

Permalink
Correct the generated code for nested parent field reference.
Browse files Browse the repository at this point in the history
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
  • Loading branch information
Vandita2020 committed Oct 24, 2022
1 parent 21c6d42 commit 9a275d8
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions templates/pkg/resource/references.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func resolveReferenceFor{{ $field.FieldPathWithUnderscore }}(

{{- $fp := ConstructFieldPath $field.Path -}}
{{ $_ := $fp.Pop -}}
{{ $isNested := gt $fp.Size 0 -}}
{{ $isNested := gt $fp.Size 0 -}}
{{ $isList := eq $field.ShapeRef.Shape.Type "list" -}}
{{ if and (not $isList) (not $isNested) -}}
if ko.Spec.{{ $field.ReferenceFieldPath }} != nil &&
Expand Down Expand Up @@ -168,15 +168,11 @@ func resolveReferenceFor{{ $field.FieldPathWithUnderscore }}(
}
{{ else -}}
if ko.Spec.{{ $field.ReferenceFieldPath }} != nil &&
len(ko.Spec.{{ $field.ReferenceFieldPath }}) > 0 {
resolvedReferences := []*string{}
for _, arrw := range ko.Spec.{{ $field.ReferenceFieldPath }} {
arr := arrw.From
ko.Spec.{{ $field.ReferenceFieldPath }}.From != nil {
arr := ko.Spec.{{ $field.ReferenceFieldPath }}.From
{{ template "read_referenced_resource_and_validate" $field }}
referencedValue := string(*obj.{{ $field.FieldConfig.References.Path }})
resolvedReferences = append(resolvedReferences, &referencedValue)
}
ko.Spec.{{ $field.Path }} = resolvedReferences
referencedValue := string(*obj.{{ $field.FieldConfig.References.Path }})
ko.Spec.{{ $field.Path }} = &referencedValue
}
return nil
}
Expand Down

0 comments on commit 9a275d8

Please sign in to comment.