Skip to content

Commit 7edaa44

Browse files
authored
Merge pull request #38 from jaypipes/renames
handle renamed fields in SetResource generation
2 parents df96615 + ad46714 commit 7edaa44

File tree

6 files changed

+119
-13
lines changed

6 files changed

+119
-13
lines changed

pkg/generate/code/set_resource.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ func SetResource(
197197
var found bool
198198
var targetMemberShapeRef *awssdkmodel.ShapeRef
199199
targetAdaptedVarName := targetVarName
200-
f, found = r.SpecFields[memberName]
200+
201+
// Check that the field has potentially been renamed
202+
renamedName, _ := r.InputFieldRename(
203+
op.Name, memberName,
204+
)
205+
f, found = r.SpecFields[renamedName]
201206
if found {
202207
targetAdaptedVarName += cfg.PrefixConfig.SpecField
203208
if !performSpecUpdate {
@@ -452,7 +457,11 @@ func setResourceReadMany(
452457
var found bool
453458
var targetMemberShapeRef *awssdkmodel.ShapeRef
454459
targetAdaptedVarName := targetVarName
455-
f, found = r.SpecFields[memberName]
460+
// Check that the field has potentially been renamed
461+
renamedName, _ := r.InputFieldRename(
462+
op.Name, memberName,
463+
)
464+
f, found = r.SpecFields[renamedName]
456465
if found {
457466
targetAdaptedVarName += cfg.PrefixConfig.SpecField
458467
} else {

pkg/generate/code/set_resource_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,3 +1945,79 @@ func TestSetResource_SQS_Queue_GetAttributes(t *testing.T) {
19451945
code.SetResourceGetAttributes(crd.Config(), crd, "resp", "ko", 1),
19461946
)
19471947
}
1948+
1949+
func TestSetResource_RDS_DBSubnetGroup_ReadMany(t *testing.T) {
1950+
assert := assert.New(t)
1951+
require := require.New(t)
1952+
1953+
g := testutil.NewGeneratorForService(t, "rds")
1954+
1955+
crd := testutil.GetCRDByName(t, g, "DBSubnetGroup")
1956+
require.NotNil(crd)
1957+
1958+
// There are renamed fields for Name and Description in order to
1959+
// "de-stutter" the field names. We want to verify that the SetResource for
1960+
// the DescribeDBSubnetGroups API operation sets these fields in the Spec
1961+
// properly
1962+
expected := `
1963+
found := false
1964+
for _, elem := range resp.DBSubnetGroups {
1965+
if elem.DBSubnetGroupArn != nil {
1966+
if ko.Status.ACKResourceMetadata == nil {
1967+
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
1968+
}
1969+
tmpARN := ackv1alpha1.AWSResourceName(*elem.DBSubnetGroupArn)
1970+
ko.Status.ACKResourceMetadata.ARN = &tmpARN
1971+
}
1972+
if elem.DBSubnetGroupDescription != nil {
1973+
ko.Spec.Description = elem.DBSubnetGroupDescription
1974+
}
1975+
if elem.DBSubnetGroupName != nil {
1976+
ko.Spec.Name = elem.DBSubnetGroupName
1977+
}
1978+
if elem.SubnetGroupStatus != nil {
1979+
ko.Status.SubnetGroupStatus = elem.SubnetGroupStatus
1980+
}
1981+
if elem.Subnets != nil {
1982+
f4 := []*svcapitypes.Subnet{}
1983+
for _, f4iter := range elem.Subnets {
1984+
f4elem := &svcapitypes.Subnet{}
1985+
if f4iter.SubnetAvailabilityZone != nil {
1986+
f4elemf0 := &svcapitypes.AvailabilityZone{}
1987+
if f4iter.SubnetAvailabilityZone.Name != nil {
1988+
f4elemf0.Name = f4iter.SubnetAvailabilityZone.Name
1989+
}
1990+
f4elem.SubnetAvailabilityZone = f4elemf0
1991+
}
1992+
if f4iter.SubnetIdentifier != nil {
1993+
f4elem.SubnetIdentifier = f4iter.SubnetIdentifier
1994+
}
1995+
if f4iter.SubnetOutpost != nil {
1996+
f4elemf2 := &svcapitypes.Outpost{}
1997+
if f4iter.SubnetOutpost.Arn != nil {
1998+
f4elemf2.ARN = f4iter.SubnetOutpost.Arn
1999+
}
2000+
f4elem.SubnetOutpost = f4elemf2
2001+
}
2002+
if f4iter.SubnetStatus != nil {
2003+
f4elem.SubnetStatus = f4iter.SubnetStatus
2004+
}
2005+
f4 = append(f4, f4elem)
2006+
}
2007+
ko.Status.Subnets = f4
2008+
}
2009+
if elem.VpcId != nil {
2010+
ko.Status.VPCID = elem.VpcId
2011+
}
2012+
found = true
2013+
break
2014+
}
2015+
if !found {
2016+
return nil, ackerr.NotFound
2017+
}
2018+
`
2019+
assert.Equal(
2020+
expected,
2021+
code.SetResource(crd.Config(), crd, model.OpTypeList, "resp", "ko", 1, false),
2022+
)
2023+
}

pkg/generate/generator.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (g *Generator) GetCRDs() ([]*ackmodel.CRD, error) {
116116
createOp.Name, memberName,
117117
)
118118
memberNames := names.New(renamedName)
119-
memberNames.ModelOrginal = memberName
119+
memberNames.ModelOriginal = memberName
120120
if memberName == "Attributes" && g.cfg.UnpacksAttributesMap(crdName) {
121121
crd.UnpackAttributes()
122122
continue
@@ -174,8 +174,14 @@ func (g *Generator) GetCRDs() ([]*ackmodel.CRD, error) {
174174
if memberShapeRef.Shape == nil {
175175
return nil, ErrNilShapePointer
176176
}
177-
memberNames := names.New(memberName)
178-
if _, found := crd.SpecFields[memberName]; found {
177+
// Check that the field in the output shape isn't the same as
178+
// fields in the input shape (where the input shape has potentially
179+
// been renamed)
180+
renamedName, _ := crd.InputFieldRename(
181+
createOp.Name, memberName,
182+
)
183+
memberNames := names.New(renamedName)
184+
if _, found := crd.SpecFields[renamedName]; found {
179185
// We don't put fields that are already in the Spec struct into
180186
// the Status struct
181187
continue
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
ignore:
22
shape_names:
3-
- DBSecurityGroupMembershipList
3+
- DBSecurityGroupMembershipList
4+
resources:
5+
DBSubnetGroup:
6+
renames:
7+
operations:
8+
DescribeDBSubnetGroups:
9+
input_fields:
10+
DBSubnetGroupName: Name
11+
DBSubnetGroupDescription: Description
12+
CreateDBSubnetGroup:
13+
input_fields:
14+
DBSubnetGroupName: Name
15+
DBSubnetGroupDescription: Description
16+
DeleteDBSubnetGroup:
17+
input_fields:
18+
DBSubnetGroupName: Name

pkg/model/field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (f *Field) IsRequired() bool {
4242
if f.FieldConfig != nil && f.FieldConfig.IsRequired != nil {
4343
return *f.FieldConfig.IsRequired
4444
}
45-
return util.InStrings(f.Names.ModelOrginal, f.CRD.Ops.Create.InputRef.Shape.Required)
45+
return util.InStrings(f.Names.ModelOriginal, f.CRD.Ops.Create.InputRef.Shape.Required)
4646
}
4747

4848
// newField returns a pointer to a new Field object

pkg/names/names.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ var goKeywords = []string{
147147
}
148148

149149
type Names struct {
150-
ModelOrginal string
151-
Original string
152-
Camel string
153-
CamelLower string
154-
Lower string
155-
Snake string
150+
ModelOriginal string
151+
Original string
152+
Camel string
153+
CamelLower string
154+
Lower string
155+
Snake string
156156
}
157157

158158
func New(original string) Names {

0 commit comments

Comments
 (0)