Skip to content

Refactor SetResourceForStruct to iterate over targetShape members #289

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

Merged
merged 5 commits into from
Feb 11, 2022
Merged

Refactor SetResourceForStruct to iterate over targetShape members #289

merged 5 commits into from
Feb 11, 2022

Conversation

brycahta
Copy link
Contributor

Issue #, if available: aws-controllers-k8s/community#1146

After a recent update to /pkg/model/field.go, shapes may contain additional fields/members with a "nested" format such as DHCPOptions.Fields: [DHCPConfigurations TagSpecifications.Tags.Value Tags.Value TagSpecifications DHCPConfigurations.Values TagSpecifications.ResourceType TagSpecifications.Tags Tags.Key DHCPOptionsID OwnerID DHCPConfigurations.Key TagSpecifications.Tags.Key DryRun Tags] The purpose of that update is to allow users to easily fetch nested shapes and their corresponding configurations using the "nested" field name as the key.

However, SetResourceForStruct will never have the opportunity to process and/or fetch shapes and configs for these "nested" fields because the algorithm processes fields iff they exist in sourceShape. Note, sourceShape comes directly from aws-sdk in this context, so will NOT have these additional, nested fields.

This patch iterates over targetShape.Members instead of sourceShape so these "nested" fields can be included in the lookup logic, but continues to use sourceShape index for consistency throughout codebase. There's no change in functionality, but this paves the way for a follow-up PR to check a targetShape's nested Field SetConfig if the Field cannot be found in the sourceShape:

sourceMemberShapeRef := sourceShape.MemberRefs[targetMemberName]
		if sourceMemberShapeRef == nil {
			continue
		}

                // Future patch: check if targetMemberName has SetConfig logic that needs to be processed

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

Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

@brycahta this is fantastic, thank you! One teeny little request to panic instead of continue when we cannot determine a source shape index, but otherwise this is ready to ship. A++

}
}
return -1, fmt.Errorf("Could not find %s in shape %s", memberName, shape.ShapeName)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice job with this. It will allow the index numbers in variable names to remain consistent, cutting down on unnecessary code changes in generated controllers. thank you!

for memberIndex, memberName := range sourceShape.MemberNames() {
targetMemberShapeRef := targetShape.MemberRefs[memberName]
if targetMemberShapeRef == nil {
for _, targetMemberName := range targetShape.MemberNames() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will be just fine for now (and is a perfect stepping stone for future commits, as you allude to in your PR summary). Long-term, I'd be keen to change this to loop over r.Fields[targetMemberName].MemberFields, though, which will contain all the additional custom fields as well.

cleanNames := names.New(memberName)
sourceAdaptedVarName := sourceVarName + "." + memberName
// Upstream logic iterates over sourceShape members and therefore uses
// the sourceShape's index; continue using sourceShape's index here for consistency.
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

Sweet. thanks again, @brycahta! :)

@jaypipes
Copy link
Collaborator

/lgtm

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

ack-bot commented Feb 11, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brycahta, jaypipes

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 ack-bot merged commit ed9e054 into aws-controllers-k8s:main Feb 11, 2022
@brycahta brycahta deleted the refactor-SetResourceForStruct branch February 14, 2022 19:45
ack-bot pushed a commit that referenced this pull request Feb 17, 2022
…mbers (#292)

Issue #, if available: aws-controllers-k8s/community#1146

This is a follow-up to #289. A `targetShape` without members in `SetResourceForStruct` will not be processed today; however, this is **not** desired behavior for all APIs such as EC2's DHCPOptions resource. 

This patch will look up and consume `SetConfig` in `SetResourceForStruct` when `targetShape` has no members like when DHCPOptions' field, `DHCPConfigurations.Values []*string`, aligns (by name) with aws-sdk's output shape, `DhcpConfigurations.Values []*AttributeValue`. Instead of skipping the processing of `Values []*string` field, it will check if a `SetConfig` exists and if it's valid (referencing a *real* shape in sourceShape), then use that updated field to source the target field's value resulting in using `DhcpConfigurations.Values.Value *string`  to populate the resource's `DHCPConfigurations.Values []*string`.






By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
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.

3 participants