Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct the generated code for nested parent field reference. #369

Merged
merged 1 commit into from
Nov 4, 2022

Conversation

Vandita2020
Copy link
Member

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 because 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

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@@ -117,7 +117,7 @@ func resolveReferenceFor{{ $field.FieldPathWithUnderscore }}(

{{- $fp := ConstructFieldPath $field.Path -}}
{{ $_ := $fp.Pop -}}
{{ $isNested := gt $fp.Size 0 -}}
{{ $isNested := gt $fp.Size 0 -}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind removing the whitespace at the end of this line please, @Vandita2020? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -143,6 +143,17 @@ func resolveReferenceFor{{ $field.FieldPathWithUnderscore }}(
}
return nil
}
{{ else if not $isList -}}
//is nested
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a Go template comment instead of a Go comment:

{{/* nested field */}}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -143,6 +143,17 @@ func resolveReferenceFor{{ $field.FieldPathWithUnderscore }}(
}
return nil
}
{{ else if not $isList -}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm mistaken, isNested could be false here.... so better to do this?

{{ if $isNested and (not $isList) -}}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, it is always going to be true here because of L132.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I missed that! :)

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
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+++

@jaypipes
Copy link
Collaborator

jaypipes commented Nov 1, 2022

/test s3-controller-test

@a-hilaly
Copy link
Member

a-hilaly commented Nov 4, 2022

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Nov 4, 2022
@ack-bot
Copy link
Collaborator

ack-bot commented Nov 4, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: A-Hilaly, jaypipes, Vandita2020

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-bot
Copy link
Collaborator

ack-bot commented Nov 4, 2022

@Vandita2020: The following tests failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
s3-controller-test 560c702 link /test s3-controller-test
unit-test 560c702 link /test unit-test

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@a-hilaly
Copy link
Member

a-hilaly commented Nov 4, 2022

Merging as the test failure are related to gopkg.in issues (see niemeyer/gopkg#63)

@a-hilaly a-hilaly merged commit 18745aa into aws-controllers-k8s:main Nov 4, 2022
ack-prow bot pushed a commit that referenced this pull request Feb 1, 2023
#398)

Fixes aws-controllers-k8s/community#1653
Reverts #369

This pull request introduced a change to the way lists of references were accessed, but incorrectly falls back on a conditional that will treat a list of references as a struct. This breaks the `ec2` and `rds` controllers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errorneous generated code for ResolveReferences for nested non array types
4 participants