diff --git a/pkg/generate/code/check.go b/pkg/generate/code/check.go index 5752473b..982b8b35 100644 --- a/pkg/generate/code/check.go +++ b/pkg/generate/code/check.go @@ -21,7 +21,6 @@ import ( ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config" "github.com/aws-controllers-k8s/code-generator/pkg/model" - "github.com/aws-controllers-k8s/code-generator/pkg/names" ) // CheckExceptionMessage returns Go code that contains a condition to @@ -137,7 +136,7 @@ func checkRequiredFieldsMissingFromShape( continue } - resVarPath, err := getSanitizedMemberPath(memberName, r, op, koVarName) + resVarPath, err := r.GetSanitizedMemberPath(memberName, op, koVarName) if err != nil { // If it isn't in our spec/status fields, we have a problem! msg := fmt.Sprintf( @@ -185,7 +184,7 @@ func checkRequiredFieldsMissingFromShapeReadMany( reqIdentifier, _ := FindPluralizedIdentifiersInShape(r, shape) - resVarPath, err := getSanitizedMemberPath(reqIdentifier, r, op, koVarName) + resVarPath, err := r.GetSanitizedMemberPath(reqIdentifier, op, koVarName) if err != nil { return result } @@ -194,33 +193,4 @@ func checkRequiredFieldsMissingFromShapeReadMany( return fmt.Sprintf("%sreturn %s\n", indent, result) } -// getSanitizedMemberPath takes a shape member field, checks for renames, checks -// for existence in Spec and Status, then constructs and returns the var path. -// Returns error if memberName is not present in either Spec or Status. -func getSanitizedMemberPath( - memberName string, - r *model.CRD, - op *awssdkmodel.Operation, - koVarName string) (string, error) { - resVarPath := koVarName - cleanMemberNames := names.New(memberName) - cleanMemberName := cleanMemberNames.Camel - // Check that the field has potentially been renamed - renamedName, wasRenamed := r.InputFieldRename( - op.Name, memberName, - ) - _, found := r.SpecFields[renamedName] - if found && !wasRenamed { - resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + cleanMemberName - } else if found { - resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + renamedName - } else { - _, found = r.StatusFields[memberName] - if !found { - return "", fmt.Errorf( - "the required field %s is NOT present in CR's Spec or Status", memberName) - } - resVarPath = resVarPath + r.Config().PrefixConfig.StatusField + "." + cleanMemberName - } - return resVarPath, nil -} + diff --git a/pkg/generate/code/common.go b/pkg/generate/code/common.go index 17effc45..2aef4f59 100644 --- a/pkg/generate/code/common.go +++ b/pkg/generate/code/common.go @@ -77,39 +77,6 @@ func FindARNIdentifiersInShape( return identifiers } -// FindIdentifiersInCRD returns the identifier fields of a given CRD which -// can be singular or plural. Note, these fields will be the *original* field -// names from the API model shape, not renamed field names. -func FindIdentifiersInCRD( - r *model.CRD) []string { - var identifiers []string - if r == nil { - return identifiers - } - identifierLookup := []string{ - "Id", - "Ids", - r.Names.Original + "Id", - r.Names.Original + "Ids", - "Name", - "Names", - r.Names.Original + "Name", - r.Names.Original + "Names", - } - - for _, id := range identifierLookup { - _, found := r.SpecFields[id] - if !found { - _, found = r.StatusFields[id] - } - if found { - identifiers = append(identifiers, id) - } - } - - return identifiers -} - // FindPluralizedIdentifiersInShape returns the name of a Spec OR Status field // that has a matching pluralized field in the given shape and the name of // the corresponding shape field name. This method will returns the original @@ -122,7 +89,7 @@ func FindPluralizedIdentifiersInShape( shape *awssdkmodel.Shape, ) (crField string, shapeField string) { shapeIdentifiers := FindIdentifiersInShape(r, shape) - crIdentifiers := FindIdentifiersInCRD(r) + crIdentifiers := r.GetIdentifiers() if len(shapeIdentifiers) == 0 || len(crIdentifiers) == 0 { return "", "" } diff --git a/pkg/generate/code/common_test.go b/pkg/generate/code/common_test.go index e1752e28..4aad3129 100644 --- a/pkg/generate/code/common_test.go +++ b/pkg/generate/code/common_test.go @@ -58,7 +58,7 @@ func TestFindIdentifiersInCRD_S3_Bucket_ReadMany_Empty(t *testing.T) { assert.Len(actualIdentifiers, 0) } -func TestFindIdentifiersInCRD_EC2_VPC_StatusField(t *testing.T) { +func TestGetIdentifiers_EC2_VPC_StatusField(t *testing.T) { assert := assert.New(t) require := require.New(t) @@ -68,7 +68,7 @@ func TestFindIdentifiersInCRD_EC2_VPC_StatusField(t *testing.T) { require.NotNil(crd) expIdentifier := "VpcId" - actualIdentifiers := code.FindIdentifiersInCRD(crd) + actualIdentifiers := crd.GetIdentifiers() assert.Len(actualIdentifiers, 1) assert.Equal( strings.TrimSpace(expIdentifier), @@ -76,7 +76,7 @@ func TestFindIdentifiersInCRD_EC2_VPC_StatusField(t *testing.T) { ) } -func TestFindIdentifiersInCRD_S3_Bucket_SpecField(t *testing.T) { +func TestGetIdentifiers_S3_Bucket_SpecField(t *testing.T) { assert := assert.New(t) require := require.New(t) @@ -86,7 +86,7 @@ func TestFindIdentifiersInCRD_S3_Bucket_SpecField(t *testing.T) { require.NotNil(crd) expIdentifier := "Name" - actualIdentifiers := code.FindIdentifiersInCRD(crd) + actualIdentifiers := crd.GetIdentifiers() assert.Len(actualIdentifiers, 1) assert.Equal( strings.TrimSpace(expIdentifier), @@ -94,7 +94,7 @@ func TestFindIdentifiersInCRD_S3_Bucket_SpecField(t *testing.T) { ) } -func TestFindIdentifiersInCRD_APIGatewayV2_API_Multiple(t *testing.T) { +func TestGetIdentifiers_APIGatewayV2_API_Multiple(t *testing.T) { assert := assert.New(t) require := require.New(t) @@ -103,7 +103,7 @@ func TestFindIdentifiersInCRD_APIGatewayV2_API_Multiple(t *testing.T) { require.NotNil(crd) expIdentifiers := []string{"ApiId", "Name"} - actualIdentifiers := code.FindIdentifiersInCRD(crd) + actualIdentifiers := crd.GetIdentifiers() assert.Len(actualIdentifiers, 2) assert.True(ackcompare.SliceStringEqual(expIdentifiers, actualIdentifiers)) } \ No newline at end of file diff --git a/pkg/generate/code/set_sdk.go b/pkg/generate/code/set_sdk.go index 04dacbc7..03ebf9c7 100644 --- a/pkg/generate/code/set_sdk.go +++ b/pkg/generate/code/set_sdk.go @@ -784,8 +784,8 @@ func setSDKReadMany( } } - // Field renames are handled in getSanitizedMemberPath - resVarPath, err = getSanitizedMemberPath(memberName, r, op, sourceVarName) + // Field renames are handled in GetSanitizedMemberPath + resVarPath, err = r.GetSanitizedMemberPath(memberName, op, sourceVarName) if err != nil { // if it's an identifier field check for singular/plural if util.InStrings(memberName, shapeIdentifiers) { @@ -799,7 +799,7 @@ func setSDKReadMany( // 'Id' identifier. if resVarPath == "" || (!strings.HasSuffix(resVarPath, "Id") || !strings.HasSuffix(resVarPath, "Ids")) { - resVarPath, err = getSanitizedMemberPath(flipped, r, op, sourceVarName) + resVarPath, err = r.GetSanitizedMemberPath(flipped, op, sourceVarName) if err != nil { panic(fmt.Sprintf( "Unable to locate identifier field %s in "+ diff --git a/pkg/model/crd.go b/pkg/model/crd.go index 320066ce..f2ccd4e1 100644 --- a/pkg/model/crd.go +++ b/pkg/model/crd.go @@ -657,6 +657,68 @@ func (r *CRD) GetAllRenames(op OpType) (map[string]string, error) { return renames, nil } +// GetIdentifiers returns the identifier fields of a given CRD which +// can be singular or plural. Note, these fields will be the *original* field +// names from the API model shape, not renamed field names. +func (r *CRD) GetIdentifiers() []string { + var identifiers []string + if r == nil { + return identifiers + } + identifierLookup := []string{ + "Id", + "Ids", + r.Names.Original + "Id", + r.Names.Original + "Ids", + "Name", + "Names", + r.Names.Original + "Name", + r.Names.Original + "Names", + } + + for _, id := range identifierLookup { + _, found := r.SpecFields[id] + if !found { + _, found = r.StatusFields[id] + } + if found { + identifiers = append(identifiers, id) + } + } + + return identifiers +} + +// GetSanitizedMemberPath takes a shape member field, checks for renames, checks +// for existence in Spec and Status, then constructs and returns the var path. +// Returns error if memberName is not present in either Spec or Status. +func (r *CRD) GetSanitizedMemberPath( + memberName string, + op *awssdkmodel.Operation, + koVarName string) (string, error) { + resVarPath := koVarName + cleanMemberNames := names.New(memberName) + cleanMemberName := cleanMemberNames.Camel + // Check that the field has potentially been renamed + renamedName, wasRenamed := r.InputFieldRename( + op.Name, memberName, + ) + _, found := r.SpecFields[renamedName] + if found && !wasRenamed { + resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + cleanMemberName + } else if found { + resVarPath = resVarPath + r.Config().PrefixConfig.SpecField + "." + renamedName + } else { + _, found = r.StatusFields[memberName] + if !found { + return "", fmt.Errorf( + "the required field %s is NOT present in CR's Spec or Status", memberName) + } + resVarPath = resVarPath + r.Config().PrefixConfig.StatusField + "." + cleanMemberName + } + return resVarPath, nil +} + // NewCRD returns a pointer to a new `ackmodel.CRD` struct that describes a // single top-level resource in an AWS service API func NewCRD(