-
Notifications
You must be signed in to change notification settings - Fork 226
Replace To/FromACKTags with to/fromACKTagsWithKeyOrder #573
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
a-hilaly
merged 1 commit into
aws-controllers-k8s:main
from
michaelhtm:change-to/fromACKsign
Mar 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package code | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws-controllers-k8s/code-generator/pkg/model" | ||
) | ||
|
||
// GoCodeToACKTags returns Go code that converts Resource Tags | ||
// to ACK Tags. If Resource Tags field is of type list, we | ||
// also maintain and return the order of the list as a []string | ||
// | ||
// | ||
// | ||
// Sample output: | ||
// | ||
// for _, k := range keyOrder { | ||
// v, ok := tags[k] | ||
// if ok { | ||
// tag := svcapitypes.Tag{Key: &k, Value: &v} | ||
// result = append(result, &tag) | ||
// delete(tags, k) | ||
// } | ||
// } | ||
// for k, v := range tags { | ||
// tag := svcapitypes.Tag{Key: &k, Value: &v} | ||
// result = append(result, &tag) | ||
// } | ||
func GoCodeConvertToACKTags(r *model.CRD, sourceVarName string, targetVarName string, keyOrderVarName string, indentLevel int) string { | ||
|
||
out := "\n" | ||
indent := strings.Repeat("\t", indentLevel) | ||
tagField, err := r.GetTagField() | ||
if err != nil { | ||
panic("error: resource does not have tags. ignore in generator.yaml") | ||
} | ||
|
||
if tagField == nil { | ||
return "" | ||
} | ||
|
||
tagFieldShapeType := tagField.ShapeRef.Shape.Type | ||
keyMemberName := r.GetTagKeyMemberName() | ||
valueMemberName := r.GetTagValueMemberName() | ||
|
||
out += fmt.Sprintf("%sif len(%s) == 0 {\n", indent, sourceVarName) | ||
out += fmt.Sprintf("%s\treturn %s, %s\n", indent, targetVarName, keyOrderVarName) | ||
out += fmt.Sprintf("%s}\n", indent) | ||
|
||
switch tagFieldShapeType { | ||
case "list": | ||
out += fmt.Sprintf("%sfor _, t := range %s {\n", indent, sourceVarName) | ||
out += fmt.Sprintf("%s\tif t.%s != nil {\n", indent, keyMemberName) | ||
out += fmt.Sprintf("%s\t\t%s = append(%s, *t.%s)\n", indent, keyOrderVarName, keyOrderVarName, keyMemberName) | ||
out += fmt.Sprintf("%s\t\tif t.%s != nil {\n", indent, valueMemberName) | ||
out += fmt.Sprintf("%s\t\t\t%s[*t.%s] = *t.%s\n", indent, targetVarName, keyMemberName, valueMemberName) | ||
out += fmt.Sprintf("%s\t\t} else {\n", indent) | ||
out += fmt.Sprintf("%s\t\t\t%s[*t.%s] = \"\"\n", indent, targetVarName, keyMemberName) | ||
out += fmt.Sprintf("%s\t\t}\n", indent) | ||
out += fmt.Sprintf("%s\t}\n", indent) | ||
out += fmt.Sprintf("%s}\n", indent) | ||
|
||
case "map": | ||
out += fmt.Sprintf("%sfor k, v := range %s {\n", indent, sourceVarName) | ||
out += fmt.Sprintf("%s\tif v == nil {\n", indent) | ||
out += fmt.Sprintf("%s\t\t%s[k] = \"\"\n", indent, targetVarName) | ||
out += fmt.Sprintf("%s\t} else {\n", indent) | ||
out += fmt.Sprintf("%s\t\t%s[k] = *v\n", indent, targetVarName) | ||
out += fmt.Sprintf("%s\t}\n", indent) | ||
out += fmt.Sprintf("%s}\n", indent) | ||
default: | ||
msg := "error: tag type can only be a list or a map" | ||
panic(msg) | ||
} | ||
|
||
return out | ||
} | ||
|
||
// GoCodeFromACKTags returns Go code that converts ACKTags | ||
// to the Resource Tag shape type. Tag fields can only be | ||
// maps or lists of Tag Go type. If Tag field is a list, | ||
// when converting from ACK Tags, we try to preserve the | ||
// original order | ||
// | ||
// | ||
// | ||
// Sample output: | ||
// | ||
// for _, k := range keyOrder { | ||
// v, ok := tags[k] | ||
// if ok { | ||
// tag := svcapitypes.Tag{Key: &k, Value: &v} | ||
// result = append(result, &tag) | ||
// delete(tags, k) | ||
// } | ||
// } | ||
// for k, v := range tags { | ||
// tag := svcapitypes.Tag{Key: &k, Value: &v} | ||
// result = append(result, &tag) | ||
// } | ||
func GoCodeFromACKTags(r *model.CRD, tagsSourceVarName string, orderVarName string, targetVarName string, indentLevel int) string { | ||
out := "\n" | ||
indent := strings.Repeat("\t", indentLevel) | ||
tagField, _ := r.GetTagField() | ||
|
||
if tagField == nil { | ||
return "" | ||
} | ||
|
||
tagFieldShapeType := tagField.ShapeRef.Shape.Type | ||
tagFieldGoType := tagField.GoTypeElem | ||
keyMemberName := r.GetTagKeyMemberName() | ||
valueMemberName := r.GetTagValueMemberName() | ||
|
||
switch tagFieldShapeType { | ||
case "list": | ||
out += fmt.Sprintf("%sfor _, k := range %s {\n", indent, orderVarName) | ||
out += fmt.Sprintf("%s\tv, ok := %s[k]\n", indent, tagsSourceVarName) | ||
out += fmt.Sprintf("%s\tif ok {\n", indent) | ||
out += fmt.Sprintf("%s\t\ttag := svcapitypes.%s{%s: &k, %s: &v}\n", indent, tagFieldGoType, keyMemberName, valueMemberName) | ||
out += fmt.Sprintf("%s\t\t%s = append(%s, &tag)\n", indent, targetVarName, targetVarName) | ||
out += fmt.Sprintf("%s\t\tdelete(%s, k)\n", indent, tagsSourceVarName) | ||
out += fmt.Sprintf("%s\t}\n", indent) | ||
out += fmt.Sprintf("%s}\n", indent) | ||
case "map": | ||
out += fmt.Sprintf("%s_ = %s\n", indent, orderVarName) | ||
michaelhtm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default: | ||
msg := "error: tag type can only be a list of a map" | ||
panic(msg) | ||
} | ||
|
||
out += fmt.Sprintf("%sfor k, v := range %s {\n", indent, tagsSourceVarName) | ||
switch tagFieldShapeType { | ||
case "list": | ||
out += fmt.Sprintf("%s\ttag := svcapitypes.%s{%s: &k, %s: &v}\n", indent, tagFieldGoType, keyMemberName, valueMemberName) | ||
out += fmt.Sprintf("%s\t%s = append(%s, &tag)\n", indent, targetVarName, targetVarName) | ||
case "map": | ||
out += fmt.Sprintf("%s\t%s[k] = &v\n", indent, targetVarName) | ||
} | ||
out += fmt.Sprintf("%s}\n", indent) | ||
|
||
return out | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package code_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aws-controllers-k8s/code-generator/pkg/generate/code" | ||
"github.com/aws-controllers-k8s/code-generator/pkg/testutil" | ||
) | ||
|
||
func TestToACKTagsForListShape(t *testing.T) { | ||
assert := assert.New(t) | ||
require := require.New(t) | ||
|
||
g := testutil.NewModelForService(t, "ec2") | ||
|
||
crd := testutil.GetCRDByName(t, g, "Vpc") | ||
require.NotNil(crd) | ||
|
||
expectedSyncedConditions := ` | ||
if len(tags) == 0 { | ||
return result, keyOrder | ||
} | ||
for _, t := range tags { | ||
if t.Key != nil { | ||
keyOrder = append(keyOrder, *t.Key) | ||
if t.Value != nil { | ||
result[*t.Key] = *t.Value | ||
} else { | ||
result[*t.Key] = "" | ||
} | ||
} | ||
} | ||
` | ||
assert.Equal( | ||
expectedSyncedConditions, | ||
code.GoCodeConvertToACKTags( | ||
crd, "tags", "result", "keyOrder", 1, | ||
), | ||
) | ||
} | ||
|
||
func TestToACKTagsForMapShape(t *testing.T) { | ||
assert := assert.New(t) | ||
require := require.New(t) | ||
|
||
g := testutil.NewModelForService(t, "apigatewayv2") | ||
|
||
crd := testutil.GetCRDByName(t, g, "Api") | ||
require.NotNil(crd) | ||
|
||
expectedSyncedConditions := ` | ||
if len(tags) == 0 { | ||
return result, keyOrder | ||
} | ||
for k, v := range tags { | ||
if v == nil { | ||
result[k] = "" | ||
} else { | ||
result[k] = *v | ||
} | ||
} | ||
` | ||
assert.Equal( | ||
expectedSyncedConditions, | ||
code.GoCodeConvertToACKTags( | ||
crd, "tags", "result", "keyOrder", 1, | ||
), | ||
) | ||
} | ||
|
||
func TestFromACKTagsForListShape(t *testing.T) { | ||
assert := assert.New(t) | ||
require := require.New(t) | ||
|
||
g := testutil.NewModelForService(t, "ec2") | ||
|
||
crd := testutil.GetCRDByName(t, g, "Vpc") | ||
require.NotNil(crd) | ||
|
||
expectedSyncedConditions := ` | ||
for _, k := range keyOrder { | ||
v, ok := tags[k] | ||
if ok { | ||
tag := svcapitypes.Tag{Key: &k, Value: &v} | ||
result = append(result, &tag) | ||
delete(tags, k) | ||
} | ||
} | ||
for k, v := range tags { | ||
tag := svcapitypes.Tag{Key: &k, Value: &v} | ||
result = append(result, &tag) | ||
} | ||
` | ||
assert.Equal( | ||
expectedSyncedConditions, | ||
code.GoCodeFromACKTags( | ||
crd, "tags", "keyOrder", "result", 1, | ||
), | ||
) | ||
} | ||
|
||
func TestFromACKTagsForMapShape(t *testing.T) { | ||
assert := assert.New(t) | ||
require := require.New(t) | ||
|
||
g := testutil.NewModelForService(t, "apigatewayv2") | ||
|
||
crd := testutil.GetCRDByName(t, g, "Api") | ||
require.NotNil(crd) | ||
|
||
expectedSyncedConditions := ` | ||
_ = keyOrder | ||
for k, v := range tags { | ||
result[k] = &v | ||
} | ||
` | ||
assert.Equal( | ||
expectedSyncedConditions, | ||
code.GoCodeFromACKTags( | ||
crd, "tags", "keyOrder", "result", 1, | ||
), | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.