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 2 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.NewNameFromCamel(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.NewNameFromSnake(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.NewNameFromCamel(fieldNameCamel).LowerCamelComputed
if sch.Optional {
fieldType = types.NewPointer(typeSecretKeySelector)
jsonTag += ",omitempty"
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 1 addition & 1 deletion 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
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.NewNameFromCamel(rfn)
ulucinar marked this conversation as resolved.
Show resolved Hide resolved
sn := name.NewNameFromCamel(sfn)
refTag := fmt.Sprintf(`json:"%s,omitempty" tf:"-"`, rn.LowerCamelComputed)
selTag := fmt.Sprintf(`json:"%s,omitempty" tf:"-"`, sn.LowerCamelComputed)

Expand Down