Skip to content

Commit 5182a53

Browse files
authored
Refactor identifier and path logic to crd.go (#174)
Issue #, if available: [#922](aws-controllers-k8s/community#922) Description of changes: * Moving 2 helper funcs to become *methods* of CRD Struct for clearer encapsulation: * `GetSanitizedMemberPath` * `GetIdentifiers` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9f6ca92 commit 5182a53

File tree

5 files changed

+75
-76
lines changed

5 files changed

+75
-76
lines changed

pkg/generate/code/check.go

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
2323
"github.com/aws-controllers-k8s/code-generator/pkg/model"
24-
"github.com/aws-controllers-k8s/code-generator/pkg/names"
2524
)
2625

2726
// CheckExceptionMessage returns Go code that contains a condition to
@@ -137,7 +136,7 @@ func checkRequiredFieldsMissingFromShape(
137136
continue
138137
}
139138

140-
resVarPath, err := getSanitizedMemberPath(memberName, r, op, koVarName)
139+
resVarPath, err := r.GetSanitizedMemberPath(memberName, op, koVarName)
141140
if err != nil {
142141
// If it isn't in our spec/status fields, we have a problem!
143142
msg := fmt.Sprintf(
@@ -185,7 +184,7 @@ func checkRequiredFieldsMissingFromShapeReadMany(
185184

186185
reqIdentifier, _ := FindPluralizedIdentifiersInShape(r, shape)
187186

188-
resVarPath, err := getSanitizedMemberPath(reqIdentifier, r, op, koVarName)
187+
resVarPath, err := r.GetSanitizedMemberPath(reqIdentifier, op, koVarName)
189188
if err != nil {
190189
return result
191190
}
@@ -194,33 +193,4 @@ func checkRequiredFieldsMissingFromShapeReadMany(
194193
return fmt.Sprintf("%sreturn %s\n", indent, result)
195194
}
196195

197-
// getSanitizedMemberPath takes a shape member field, checks for renames, checks
198-
// for existence in Spec and Status, then constructs and returns the var path.
199-
// Returns error if memberName is not present in either Spec or Status.
200-
func getSanitizedMemberPath(
201-
memberName string,
202-
r *model.CRD,
203-
op *awssdkmodel.Operation,
204-
koVarName string) (string, error) {
205-
resVarPath := koVarName
206-
cleanMemberNames := names.New(memberName)
207-
cleanMemberName := cleanMemberNames.Camel
208-
// Check that the field has potentially been renamed
209-
renamedName, wasRenamed := r.InputFieldRename(
210-
op.Name, memberName,
211-
)
212-
_, found := r.SpecFields[renamedName]
213-
if found && !wasRenamed {
214-
resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + cleanMemberName
215-
} else if found {
216-
resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + renamedName
217-
} else {
218-
_, found = r.StatusFields[memberName]
219-
if !found {
220-
return "", fmt.Errorf(
221-
"the required field %s is NOT present in CR's Spec or Status", memberName)
222-
}
223-
resVarPath = resVarPath + r.Config().PrefixConfig.StatusField + "." + cleanMemberName
224-
}
225-
return resVarPath, nil
226-
}
196+

pkg/generate/code/common.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,6 @@ func FindARNIdentifiersInShape(
7777
return identifiers
7878
}
7979

80-
// FindIdentifiersInCRD returns the identifier fields of a given CRD which
81-
// can be singular or plural. Note, these fields will be the *original* field
82-
// names from the API model shape, not renamed field names.
83-
func FindIdentifiersInCRD(
84-
r *model.CRD) []string {
85-
var identifiers []string
86-
if r == nil {
87-
return identifiers
88-
}
89-
identifierLookup := []string{
90-
"Id",
91-
"Ids",
92-
r.Names.Original + "Id",
93-
r.Names.Original + "Ids",
94-
"Name",
95-
"Names",
96-
r.Names.Original + "Name",
97-
r.Names.Original + "Names",
98-
}
99-
100-
for _, id := range identifierLookup {
101-
_, found := r.SpecFields[id]
102-
if !found {
103-
_, found = r.StatusFields[id]
104-
}
105-
if found {
106-
identifiers = append(identifiers, id)
107-
}
108-
}
109-
110-
return identifiers
111-
}
112-
11380
// FindPluralizedIdentifiersInShape returns the name of a Spec OR Status field
11481
// that has a matching pluralized field in the given shape and the name of
11582
// the corresponding shape field name. This method will returns the original
@@ -122,7 +89,7 @@ func FindPluralizedIdentifiersInShape(
12289
shape *awssdkmodel.Shape,
12390
) (crField string, shapeField string) {
12491
shapeIdentifiers := FindIdentifiersInShape(r, shape)
125-
crIdentifiers := FindIdentifiersInCRD(r)
92+
crIdentifiers := r.GetIdentifiers()
12693
if len(shapeIdentifiers) == 0 || len(crIdentifiers) == 0 {
12794
return "", ""
12895
}

pkg/generate/code/common_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestFindIdentifiersInCRD_S3_Bucket_ReadMany_Empty(t *testing.T) {
5858
assert.Len(actualIdentifiers, 0)
5959
}
6060

61-
func TestFindIdentifiersInCRD_EC2_VPC_StatusField(t *testing.T) {
61+
func TestGetIdentifiers_EC2_VPC_StatusField(t *testing.T) {
6262
assert := assert.New(t)
6363
require := require.New(t)
6464

@@ -68,15 +68,15 @@ func TestFindIdentifiersInCRD_EC2_VPC_StatusField(t *testing.T) {
6868
require.NotNil(crd)
6969

7070
expIdentifier := "VpcId"
71-
actualIdentifiers := code.FindIdentifiersInCRD(crd)
71+
actualIdentifiers := crd.GetIdentifiers()
7272
assert.Len(actualIdentifiers, 1)
7373
assert.Equal(
7474
strings.TrimSpace(expIdentifier),
7575
strings.TrimSpace(actualIdentifiers[0]),
7676
)
7777
}
7878

79-
func TestFindIdentifiersInCRD_S3_Bucket_SpecField(t *testing.T) {
79+
func TestGetIdentifiers_S3_Bucket_SpecField(t *testing.T) {
8080
assert := assert.New(t)
8181
require := require.New(t)
8282

@@ -86,15 +86,15 @@ func TestFindIdentifiersInCRD_S3_Bucket_SpecField(t *testing.T) {
8686
require.NotNil(crd)
8787

8888
expIdentifier := "Name"
89-
actualIdentifiers := code.FindIdentifiersInCRD(crd)
89+
actualIdentifiers := crd.GetIdentifiers()
9090
assert.Len(actualIdentifiers, 1)
9191
assert.Equal(
9292
strings.TrimSpace(expIdentifier),
9393
strings.TrimSpace(actualIdentifiers[0]),
9494
)
9595
}
9696

97-
func TestFindIdentifiersInCRD_APIGatewayV2_API_Multiple(t *testing.T) {
97+
func TestGetIdentifiers_APIGatewayV2_API_Multiple(t *testing.T) {
9898
assert := assert.New(t)
9999
require := require.New(t)
100100

@@ -103,7 +103,7 @@ func TestFindIdentifiersInCRD_APIGatewayV2_API_Multiple(t *testing.T) {
103103
require.NotNil(crd)
104104

105105
expIdentifiers := []string{"ApiId", "Name"}
106-
actualIdentifiers := code.FindIdentifiersInCRD(crd)
106+
actualIdentifiers := crd.GetIdentifiers()
107107
assert.Len(actualIdentifiers, 2)
108108
assert.True(ackcompare.SliceStringEqual(expIdentifiers, actualIdentifiers))
109109
}

pkg/generate/code/set_sdk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ func setSDKReadMany(
784784
}
785785
}
786786

787-
// Field renames are handled in getSanitizedMemberPath
788-
resVarPath, err = getSanitizedMemberPath(memberName, r, op, sourceVarName)
787+
// Field renames are handled in GetSanitizedMemberPath
788+
resVarPath, err = r.GetSanitizedMemberPath(memberName, op, sourceVarName)
789789
if err != nil {
790790
// if it's an identifier field check for singular/plural
791791
if util.InStrings(memberName, shapeIdentifiers) {
@@ -799,7 +799,7 @@ func setSDKReadMany(
799799
// 'Id' identifier.
800800
if resVarPath == "" || (!strings.HasSuffix(resVarPath, "Id") ||
801801
!strings.HasSuffix(resVarPath, "Ids")) {
802-
resVarPath, err = getSanitizedMemberPath(flipped, r, op, sourceVarName)
802+
resVarPath, err = r.GetSanitizedMemberPath(flipped, op, sourceVarName)
803803
if err != nil {
804804
panic(fmt.Sprintf(
805805
"Unable to locate identifier field %s in "+

pkg/model/crd.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,68 @@ func (r *CRD) GetAllRenames(op OpType) (map[string]string, error) {
657657
return renames, nil
658658
}
659659

660+
// GetIdentifiers returns the identifier fields of a given CRD which
661+
// can be singular or plural. Note, these fields will be the *original* field
662+
// names from the API model shape, not renamed field names.
663+
func (r *CRD) GetIdentifiers() []string {
664+
var identifiers []string
665+
if r == nil {
666+
return identifiers
667+
}
668+
identifierLookup := []string{
669+
"Id",
670+
"Ids",
671+
r.Names.Original + "Id",
672+
r.Names.Original + "Ids",
673+
"Name",
674+
"Names",
675+
r.Names.Original + "Name",
676+
r.Names.Original + "Names",
677+
}
678+
679+
for _, id := range identifierLookup {
680+
_, found := r.SpecFields[id]
681+
if !found {
682+
_, found = r.StatusFields[id]
683+
}
684+
if found {
685+
identifiers = append(identifiers, id)
686+
}
687+
}
688+
689+
return identifiers
690+
}
691+
692+
// GetSanitizedMemberPath takes a shape member field, checks for renames, checks
693+
// for existence in Spec and Status, then constructs and returns the var path.
694+
// Returns error if memberName is not present in either Spec or Status.
695+
func (r *CRD) GetSanitizedMemberPath(
696+
memberName string,
697+
op *awssdkmodel.Operation,
698+
koVarName string) (string, error) {
699+
resVarPath := koVarName
700+
cleanMemberNames := names.New(memberName)
701+
cleanMemberName := cleanMemberNames.Camel
702+
// Check that the field has potentially been renamed
703+
renamedName, wasRenamed := r.InputFieldRename(
704+
op.Name, memberName,
705+
)
706+
_, found := r.SpecFields[renamedName]
707+
if found && !wasRenamed {
708+
resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + cleanMemberName
709+
} else if found {
710+
resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + renamedName
711+
} else {
712+
_, found = r.StatusFields[memberName]
713+
if !found {
714+
return "", fmt.Errorf(
715+
"the required field %s is NOT present in CR's Spec or Status", memberName)
716+
}
717+
resVarPath = resVarPath + r.Config().PrefixConfig.StatusField + "." + cleanMemberName
718+
}
719+
return resVarPath, nil
720+
}
721+
660722
// NewCRD returns a pointer to a new `ackmodel.CRD` struct that describes a
661723
// single top-level resource in an AWS service API
662724
func NewCRD(

0 commit comments

Comments
 (0)