Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Capitalize prefix/suffix acronyms in default resource config's kind name #179

Merged
merged 3 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{},
Expand Down
45 changes: 45 additions & 0 deletions pkg/config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions pkg/types/name.go → pkg/types/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package types
package name

import (
"strings"
Expand All @@ -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))
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pkg/types/name_test.go → pkg/types/name/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package types
package name

import (
"testing"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down