Skip to content

Commit 56be584

Browse files
authored
Fix generation of attribute-based APIs (#376)
The code generation for attribute-based APIs (SNS, SQS, etc) had not been kept up to date for 2+ years, meaning that the SNS and SQS controllers could not be regenerated to bring in updated ACK runtimes or aws-sdk-go releases. This commit gets the attribute-based API code generation back into shape. Most of the changes in here were around the code generator being able to find either ResourceConfig or FieldConfig objects using case-insensitive matching, which turned out to be the main cause for code generator failures when re-generating the SNS controller. Signed-off-by: Jay Pipes <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 62c73f2 commit 56be584

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

pkg/config/resource.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package config
1515

1616
import (
17+
"strings"
18+
1719
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
1820

1921
"github.com/aws-controllers-k8s/code-generator/pkg/util"
@@ -387,8 +389,8 @@ func (c *Config) ResourceContainsAttributesMap(resourceName string) bool {
387389
if c == nil {
388390
return false
389391
}
390-
resGenConfig, found := c.Resources[resourceName]
391-
if found {
392+
resGenConfig := c.GetResourceConfig(resourceName)
393+
if resGenConfig != nil {
392394
if resGenConfig.UnpackAttributesMapConfig != nil {
393395
return true
394396
}
@@ -450,13 +452,19 @@ func (c *Config) ResourceSetsSingleAttribute(resourceName string) bool {
450452
return resGenConfig.UnpackAttributesMapConfig.SetAttributesSingleAttribute
451453
}
452454

453-
// GetResourceConfig returns the ResourceConfig for a given resource name
455+
// GetResourceConfig returns the ResourceConfig for a given resource name,
456+
// searching case-insensitively.
454457
func (c *Config) GetResourceConfig(resourceName string) *ResourceConfig {
455458
if c == nil {
456459
return nil
457460
}
458-
rc, _ := c.Resources[resourceName]
459-
return &rc
461+
462+
for resName, resCfg := range c.Resources {
463+
if strings.EqualFold(resName, resourceName) {
464+
return &resCfg
465+
}
466+
}
467+
return nil
460468
}
461469

462470
// GetCompareIgnoredFieldPaths returns the list of field paths to ignore when
@@ -658,3 +666,18 @@ func (c *Config) TagsAreIgnored(resName string) bool {
658666
}
659667
return false
660668
}
669+
670+
// GetFieldConfig accepts a string name of a field and returns the FieldConfig
671+
// object inside the ResourceConfig matching (case-insensitively) the supplied
672+
// field name, or nil if that field has no FieldConfig.
673+
func (rc *ResourceConfig) GetFieldConfig(subject string) *FieldConfig {
674+
if rc == nil {
675+
return nil
676+
}
677+
for fieldName, fieldCfg := range rc.Fields {
678+
if strings.EqualFold(fieldName, subject) {
679+
return fieldCfg
680+
}
681+
}
682+
return nil
683+
}

pkg/generate/code/compare.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func CompareResource(
9494
) string {
9595
out := "\n"
9696

97-
fieldConfigs := cfg.GetFieldConfigs(r.Names.Original)
97+
resConfig := cfg.GetResourceConfig(r.Names.Camel)
9898

9999
// We need a deterministic order to traverse our top-level fields...
100100
specFieldNames := []string{}
@@ -111,18 +111,21 @@ func CompareResource(
111111
secondResAdaptedVarName := secondResVarName + cfg.PrefixConfig.SpecField
112112
secondResAdaptedVarName += "." + specField.Names.Camel
113113

114+
var fieldConfig *ackgenconfig.FieldConfig
114115
var compareConfig *ackgenconfig.CompareFieldConfig
115116

116-
// TODO(amine,john): This is fragile. We actually need to have a way of
117-
// normalizing names in a lossless fashion...
118-
//
119-
// We chose to normalize names as camel case.
120-
fieldNameCamel := names.New(fieldName).Camel
121-
fieldConfig := fieldConfigs[fieldNameCamel]
117+
if resConfig != nil {
118+
fieldConfig = resConfig.GetFieldConfig(fieldName)
119+
}
122120
if fieldConfig != nil {
123121
compareConfig = fieldConfig.Compare
124122
}
125123

124+
if fieldConfig != nil && fieldConfig.IsAttribute {
125+
// NOTE(jaypipes): We compare the Attributes collection
126+
// specially...
127+
continue
128+
}
126129
if compareConfig != nil && compareConfig.IsIgnored {
127130
continue
128131
}

pkg/generate/code/set_sdk.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ func SetSDKGetAttributes(
406406
indent := strings.Repeat("\t", indentLevel)
407407

408408
inputFieldOverrides := map[string][]string{}
409-
rConfig, ok := cfg.Resources[r.Names.Original]
410-
if !ok {
409+
rConfig := cfg.GetResourceConfig(r.Names.Original)
410+
if rConfig == nil {
411411
// This is a bug in the code generation if this occurs...
412412
msg := fmt.Sprintf(
413413
"called SetSDKGetAttributes for a resource '%s' that doesn't have a ResourceConfig",
@@ -421,7 +421,6 @@ func SetSDKGetAttributes(
421421
inputFieldOverrides[memberName] = override.Values
422422
}
423423
}
424-
425424
for _, memberName := range inputShape.MemberNames() {
426425
if r.IsPrimaryARNField(memberName) {
427426
// if ko.Status.ACKResourceMetadata != nil && ko.Status.ACKResourceMetadata.ARN != nil {

pkg/model/op.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ func OpTypeFromString(s string) OpType {
173173
return OpTypeGet
174174
case "list", "readmany", "read_many":
175175
return OpTypeList
176-
case "getattributes":
176+
case "getattributes", "get_attributes":
177177
return OpTypeGetAttributes
178-
case "setattributes":
178+
case "setattributes", "set_attributes":
179179
return OpTypeSetAttributes
180180
}
181181

0 commit comments

Comments
 (0)