Skip to content

Commit 138195a

Browse files
Fix required fields for renamed ReadOne input (#113)
Description of changes: The generator was failing with a panic when it could not find a renamed field in the `ReadOne` describe/create operation while creating the `GoCodeRequiredFieldsMissingFromReadOneInput` template. This pull request ensures the code attempts to rename the field, if specified in the config, before using the field name in any output. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 316cbcb commit 138195a

File tree

5 files changed

+3029
-2
lines changed

5 files changed

+3029
-2
lines changed

pkg/generate/code/check.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func CheckRequiredFieldsMissingFromShape(
8787
r,
8888
koVarName,
8989
indentLevel,
90+
op,
9091
shape,
9192
)
9293
}
@@ -95,6 +96,7 @@ func checkRequiredFieldsMissingFromShape(
9596
r *model.CRD,
9697
koVarName string,
9798
indentLevel int,
99+
op *awssdkmodel.Operation,
98100
shape *awssdkmodel.Shape,
99101
) string {
100102
indent := strings.Repeat("\t", indentLevel)
@@ -134,9 +136,15 @@ func checkRequiredFieldsMissingFromShape(
134136
cleanMemberName := cleanMemberNames.Camel
135137

136138
resVarPath := koVarName
137-
_, found := r.SpecFields[memberName]
138-
if found {
139+
// Check that the field has potentially been renamed
140+
renamedName, wasRenamed := r.InputFieldRename(
141+
op.Name, memberName,
142+
)
143+
_, found := r.SpecFields[renamedName]
144+
if found && !wasRenamed {
139145
resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + cleanMemberName
146+
} else if found {
147+
resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + renamedName
140148
} else {
141149
_, found = r.StatusFields[memberName]
142150
if !found {

pkg/generate/code/check_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,24 @@ func TestCheckRequiredFields_Attributes_StatusAndSpecField(t *testing.T) {
9494
strings.TrimSpace(gotCode),
9595
)
9696
}
97+
98+
func TestCheckRequiredFields_RenamedSpecField(t *testing.T) {
99+
assert := assert.New(t)
100+
require := require.New(t)
101+
102+
g := testutil.NewGeneratorForService(t, "eks")
103+
104+
crd := testutil.GetCRDByName(t, g, "FargateProfile")
105+
require.NotNil(crd)
106+
107+
expRequiredFieldsCode := `
108+
return r.ko.Spec.ClusterName == nil || r.ko.Spec.Name == nil
109+
`
110+
gotCode := code.CheckRequiredFieldsMissingFromShape(
111+
crd, model.OpTypeGet, "r.ko", 1,
112+
)
113+
assert.Equal(
114+
strings.TrimSpace(expRequiredFieldsCode),
115+
strings.TrimSpace(gotCode),
116+
)
117+
}

0 commit comments

Comments
 (0)