diff --git a/pkg/config/defaults.go b/pkg/config/defaults.go index c31baf61..89a8fd72 100644 --- a/pkg/config/defaults.go +++ b/pkg/config/defaults.go @@ -21,6 +21,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/iancoleman/strcase" + + typeName "github.com/crossplane-contrib/terrajet/pkg/types/name" ) // Commonly used resource configurations. @@ -95,7 +97,7 @@ func DefaultResource(name string, terraformSchema *schema.Resource, opts ...Reso Name: name, TerraformResource: terraformSchema, ShortGroup: group, - Kind: kind, + Kind: typeName.NewFromCamel(kind).Camel, Version: "v1alpha1", ExternalName: NameAsIdentifier, References: map[string]Reference{}, diff --git a/pkg/config/defaults_test.go b/pkg/config/defaults_test.go index 741f7df1..75a2e72a 100644 --- a/pkg/config/defaults_test.go +++ b/pkg/config/defaults_test.go @@ -66,6 +66,51 @@ func TestDefaultResource(t *testing.T) { Sensitive: NopSensitive, }, }, + "NameWithPrefixAcronym": { + reason: "It should return prefix acronym in capital case", + args: args{ + name: "aws_db_sql_server", + }, + want: &Resource{ + Name: "aws_db_sql_server", + ShortGroup: "db", + Kind: "SQLServer", + Version: "v1alpha1", + ExternalName: NameAsIdentifier, + References: map[string]Reference{}, + Sensitive: NopSensitive, + }, + }, + "NameWithSuffixAcronym": { + reason: "It should return suffix acronym in capital case", + args: args{ + name: "aws_db_server_id", + }, + want: &Resource{ + Name: "aws_db_server_id", + ShortGroup: "db", + Kind: "ServerID", + Version: "v1alpha1", + ExternalName: NameAsIdentifier, + References: map[string]Reference{}, + Sensitive: NopSensitive, + }, + }, + "NameWithMultipleAcronyms": { + reason: "It should return both prefix & suffix acronyms in capital case", + args: args{ + name: "aws_db_sql_server_id", + }, + want: &Resource{ + Name: "aws_db_sql_server_id", + ShortGroup: "db", + Kind: "SQLServerID", + Version: "v1alpha1", + ExternalName: NameAsIdentifier, + References: map[string]Reference{}, + Sensitive: NopSensitive, + }, + }, } // TODO(muvaf): Find a way to compare function pointers. diff --git a/pkg/types/builder.go b/pkg/types/builder.go index a492b748..4ab4ba8e 100644 --- a/pkg/types/builder.go +++ b/pkg/types/builder.go @@ -30,6 +30,7 @@ import ( "github.com/crossplane-contrib/terrajet/pkg/config" "github.com/crossplane-contrib/terrajet/pkg/types/comments" + "github.com/crossplane-contrib/terrajet/pkg/types/name" ) const ( @@ -102,7 +103,7 @@ func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPa var obsTags []string //nolint:prealloc for _, snakeFieldName := range keys { sch := res.Schema[snakeFieldName] - fieldName := NewNameFromSnake(snakeFieldName) + fieldName := name.NewFromSnake(snakeFieldName) comment, err := comments.New(sch.Description) if err != nil { return nil, nil, errors.Wrapf(err, "cannot build comment for description: %s", sch.Description) @@ -159,7 +160,7 @@ func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPa tfTag = "-" fieldType = typeSecretKeySelector - jsonTag = NewNameFromCamel(fieldNameCamel).LowerCamelComputed + jsonTag = name.NewFromCamel(fieldNameCamel).LowerCamelComputed if sch.Optional { fieldType = types.NewPointer(typeSecretKeySelector) jsonTag += ",omitempty" diff --git a/pkg/types/name.go b/pkg/types/name/name.go similarity index 93% rename from pkg/types/name.go rename to pkg/types/name/name.go index 6712658c..30c92883 100644 --- a/pkg/types/name.go +++ b/pkg/types/name/name.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package types +package name import ( "strings" @@ -23,9 +23,9 @@ import ( "github.com/iancoleman/strcase" ) -// NewNameFromSnake produces a Name, using given snake case string as source of +// NewFromSnake produces a Name, using given snake case string as source of // truth. -func NewNameFromSnake(s string) Name { +func NewFromSnake(s string) Name { originals := strings.Split(s, "_") camels := make([]string, len(originals)) computedCamels := make([]string, len(originals)) @@ -46,15 +46,15 @@ func NewNameFromSnake(s string) Name { } } -// NewNameFromCamel produces a Name, using given camel case string as source of +// NewFromCamel produces a Name, using given camel case string as source of // truth. -func NewNameFromCamel(s string) Name { +func NewFromCamel(s string) Name { originals := camelcase.Split(s) snakes := make([]string, len(originals)) for i, org := range originals { snakes[i] = strings.ToLower(org) } - return NewNameFromSnake(strings.Join(snakes, "_")) + return NewFromSnake(strings.Join(snakes, "_")) } // Name holds different variants of a name. diff --git a/pkg/types/name_test.go b/pkg/types/name/name_test.go similarity index 97% rename from pkg/types/name_test.go rename to pkg/types/name/name_test.go index 13c477a1..15b151cc 100644 --- a/pkg/types/name_test.go +++ b/pkg/types/name/name_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package types +package name import ( "testing" @@ -81,7 +81,7 @@ func TestNewNameFromSnake(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { - got := NewNameFromSnake(tc.in) + got := NewFromSnake(tc.in) if diff := cmp.Diff(tc.want, got); diff != "" { t.Errorf("\nNewNameFromSnake(...): -want, +got:\n%s", diff) @@ -149,7 +149,7 @@ func TestNewNameFromCamel(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { - got := NewNameFromCamel(tc.in) + got := NewFromCamel(tc.in) if diff := cmp.Diff(tc.want, got); diff != "" { t.Errorf("\nNewNameFromSnake(...): -want, +got:\n%s", diff) diff --git a/pkg/types/reference.go b/pkg/types/reference.go index 75a5cf5e..eb7bcfe0 100644 --- a/pkg/types/reference.go +++ b/pkg/types/reference.go @@ -11,6 +11,7 @@ import ( "github.com/crossplane-contrib/terrajet/pkg/config" "github.com/crossplane-contrib/terrajet/pkg/types/comments" + "github.com/crossplane-contrib/terrajet/pkg/types/name" ) const ( @@ -42,8 +43,8 @@ func (g *Builder) generateReferenceFields(t *types.TypeName, f *types.Var, r con sfn = f.Name() + "Selector" } - rn := NewNameFromCamel(rfn) - sn := NewNameFromCamel(sfn) + rn := name.NewFromCamel(rfn) + sn := name.NewFromCamel(sfn) refTag := fmt.Sprintf(`json:"%s,omitempty" tf:"-"`, rn.LowerCamelComputed) selTag := fmt.Sprintf(`json:"%s,omitempty" tf:"-"`, sn.LowerCamelComputed)