Skip to content

Commit 7052f68

Browse files
authored
Consider renaming when trying to find pluralized identifier shapes (#304)
Description of changes: Currently `FindPluralizedIdentifiersInShape` doesn't take into consideration CRD field renames. This patch enhances the function to be able to lookup field renames and properly find the matching identifier field. Fixes aws-controllers-k8s/community#1208 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 00cd850 commit 7052f68

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

pkg/generate/code/common.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,18 @@ func FindPluralizedIdentifiersInShape(
109109
pluralize := pluralize.NewClient()
110110
for _, si := range shapeIdentifiers {
111111
for _, ci := range crIdentifiers {
112-
if strings.EqualFold(pluralize.Singular(si),
113-
pluralize.Singular(ci)) {
112+
// If the identifier field is renamed, we must take that into
113+
// consideration in order to find the corresponding matching
114+
// shapeIdentifier.
115+
siRenamed, _ := r.Config().ResourceFieldRename(
116+
r.Names.Original,
117+
op.Name,
118+
pluralize.Singular(si),
119+
)
120+
if strings.EqualFold(
121+
siRenamed,
122+
pluralize.Singular(ci),
123+
) {
114124
// The CRD identifiers being used for comparison reflect any
115125
// renamed field names in the API model shape.
116126
if crField == "" {

pkg/generate/code/set_sdk_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,3 +1800,30 @@ func TestSetSDK_EC2_VPC_ReadMany(t *testing.T) {
18001800
code.SetSDK(crd.Config(), crd, model.OpTypeList, "r.ko", "res", 1),
18011801
)
18021802
}
1803+
1804+
func Test_SetSDK_ECR_Repository_newListRequestPayload(t *testing.T) {
1805+
assert := assert.New(t)
1806+
require := require.New(t)
1807+
1808+
g := testutil.NewModelForServiceWithOptions(t, "ecr", &testutil.TestingModelOptions{
1809+
GeneratorConfigFile: "generator-renamed-identifier-field.yaml",
1810+
})
1811+
1812+
crd := testutil.GetCRDByName(t, g, "Repository")
1813+
require.NotNil(crd)
1814+
1815+
expected := `
1816+
if r.ko.Status.RegistryID != nil {
1817+
res.SetRegistryId(*r.ko.Status.RegistryID)
1818+
}
1819+
if r.ko.Spec.Name != nil {
1820+
f3 := []*string{}
1821+
f3 = append(f3, r.ko.Spec.Name)
1822+
res.SetRepositoryNames(f3)
1823+
}
1824+
`
1825+
assert.Equal(
1826+
expected,
1827+
code.SetSDK(crd.Config(), crd, model.OpTypeList, "r.ko", "res", 1),
1828+
)
1829+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resources:
2+
Repository:
3+
renames:
4+
operations:
5+
CreateRepository:
6+
input_fields:
7+
RepositoryName: Name
8+
DescribeRepositories:
9+
input_fields:
10+
RepositoryName: Name

0 commit comments

Comments
 (0)