Skip to content

Commit

Permalink
Prefixing nested attribute names with the name of the parent attribut…
Browse files Browse the repository at this point in the history
…e when the attribute name matches any of the generated custom value method names (#76)
  • Loading branch information
bendbennett committed Oct 26, 2023
1 parent b13d310 commit bdee822
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 21 deletions.
343 changes: 341 additions & 2 deletions internal/schema/custom_nested_object_test.go

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions internal/schema/framework_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ func (identifier FrameworkIdentifier) ToCamelCase() string {
return string(unicode.ToLower(firstLetter)) + pascal[size:]
}

// ToPrefixCamelCase will return a camel case formatted string of the identifier,
// prefixed with a camel-cased version of the supplied name if the identifier is
// a generated custom value method name.
// Example:
// - equal(something) -> somethingEqual
// - type(something) -> somethingType
func (identifier FrameworkIdentifier) ToPrefixCamelCase(name string) string {
pascalCase := identifier.ToPascalCase()

methodNames := identifier.methodNames()

for _, v := range methodNames {
if pascalCase == v {
return FrameworkIdentifier(name + pascalCase).ToCamelCase()
}
}

return FrameworkIdentifier(pascalCase).ToCamelCase()
}

// ToPascalCase will return a pascal case formatted string of the identifier.
// Example:
// - example_resource_thing -> ExampleResourceThing
Expand All @@ -51,9 +71,44 @@ func (identifier FrameworkIdentifier) ToPascalCase() string {
})
}

// ToPrefixPascalCase will return a pascal case formatted string of the identifier,
// prefixed with a pascal-cased version of the supplied name if the identifier is
// a generated custom value method name.
// Example:
// - equal(something) -> SomethingEqual
// - type(something) -> SomethingType
func (identifier FrameworkIdentifier) ToPrefixPascalCase(name string) string {
pascalCase := identifier.ToPascalCase()

methodNames := identifier.methodNames()

for _, v := range methodNames {
if pascalCase == v {
return FrameworkIdentifier(name).ToPascalCase() + pascalCase
}
}

return pascalCase
}

// ToString returns the FrameworkIdentifier as a string without any formatting.
// Example:
// - example_resource_thing -> example_resource_thing
func (identifier FrameworkIdentifier) ToString() string {
return string(identifier)
}

// methodNames returns a slice containing generated method names for custom value
// types.
func (identifier FrameworkIdentifier) methodNames() []string {
return []string{
"AttributeTypes",
"Equal",
"IsNull",
"IsUnknown",
"String",
"ToObjectValue",
"ToTerraformValue",
"Type",
}
}
146 changes: 146 additions & 0 deletions internal/schema/framework_identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,79 @@ func TestFrameworkIdentifier_ToCamelCase(t *testing.T) {
}
}

func TestFrameworkIdentifier_ToPrefixCamelCase(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
identifier schema.FrameworkIdentifier
want string
}{
"lowercase alphabet": {
identifier: "thing",
want: "thing",
},
"leading underscore": {
identifier: "_thing",
want: "thing",
},
"middle underscore": {
identifier: "fake_thing",
want: "fakeThing",
},
"alphanumeric": {
identifier: "thing123",
want: "thing123",
},
"valid - alphanumeric with underscores": {
identifier: "fake_thing_123",
want: "fakeThing123",
},
"attribute_types": {
identifier: "attribute_types",
want: "nameAttributeTypes",
},
"equal": {
identifier: "Equal",
want: "nameEqual",
},
"is_null": {
identifier: "is_null",
want: "nameIsNull",
},
"is_unknown": {
identifier: "is_unknown",
want: "nameIsUnknown",
},
"string": {
identifier: "string",
want: "nameString",
},
"to_object_value": {
identifier: "to_object_value",
want: "nameToObjectValue",
},
"to_terraform_value": {
identifier: "to_terraform_value",
want: "nameToTerraformValue",
},
"type": {
identifier: "type",
want: "nameType",
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.identifier.ToPrefixCamelCase("name")
if got != testCase.want {
t.Fatalf("expected ToCamelCase() to return %s, got %s", testCase.want, got)
}
})
}
}

func TestFrameworkIdentifier_ToPascalCase(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -147,3 +220,76 @@ func TestFrameworkIdentifier_ToPascalCase(t *testing.T) {
})
}
}

func TestFrameworkIdentifier_ToPrefixPascalCase(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
identifier schema.FrameworkIdentifier
want string
}{
"lowercase alphabet": {
identifier: "thing",
want: "Thing",
},
"leading underscore": {
identifier: "_thing",
want: "Thing",
},
"middle underscore": {
identifier: "fake_thing",
want: "FakeThing",
},
"alphanumeric": {
identifier: "thing123",
want: "Thing123",
},
"valid - alphanumeric with underscores": {
identifier: "fake_thing_123",
want: "FakeThing123",
},
"attribute_types": {
identifier: "attribute_types",
want: "NameAttributeTypes",
},
"equal": {
identifier: "Equal",
want: "NameEqual",
},
"is_null": {
identifier: "is_null",
want: "NameIsNull",
},
"is_unknown": {
identifier: "is_unknown",
want: "NameIsUnknown",
},
"string": {
identifier: "string",
want: "NameString",
},
"to_object_value": {
identifier: "to_object_value",
want: "NameToObjectValue",
},
"to_terraform_value": {
identifier: "to_terraform_value",
want: "NameToTerraformValue",
},
"type": {
identifier: "type",
want: "NameType",
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.identifier.ToPrefixPascalCase("name")
if got != testCase.want {
t.Fatalf("expected ToPascalCase() to return %s, got %s", testCase.want, got)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/schema/templates/nested_object_type_value.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ return New{{.Name}}ValueUnknown(), diags

return {{.Name}}Value{
{{- range $key, $value := .AttrValues }}
{{$key.ToPascalCase}}: {{$key.ToCamelCase}}Val,
{{$key.ToPrefixPascalCase $.Name}}: {{$key.ToCamelCase}}Val,
{{- end}}
state: attr.ValueStateKnown,
}, diags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ return nil, diags

return {{.Name}}Value{
{{- range $key, $value := .AttrValues }}
{{$key.ToPascalCase}}: {{$key.ToCamelCase}}Val,
{{$key.ToPrefixPascalCase $.Name}}: {{$key.ToCamelCase}}Val,
{{- end}}
state: attr.ValueStateKnown,
}, diags
Expand Down
8 changes: 1 addition & 7 deletions internal/schema/templates/nested_object_value_equal.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ return true
}

{{range $key, $value := .AttrValues }}
{{- if eq $value "baseTypes.BoolValue" "baseTypes.Float64Value" "baseTypes.Int64Value" "baseTypes.NumberValue" "baseTypes.StringValue"}}
if v.{{$key.ToPascalCase}} != other.{{$key.ToPascalCase}} {
if !v.{{$key.ToPrefixPascalCase $.Name}}.Equal(other.{{$key.ToPrefixPascalCase $.Name}}) {
return false
}
{{- else}}
if !v.{{$key.ToPascalCase}}.Equal(other.{{$key.ToPascalCase}}) {
return false
}
{{- end}}
{{end}}

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ func (v {{.Name}}Value) ToObjectValue(ctx context.Context) (basetypes.ObjectValu
{{- else if eq $value "SetNested"}}
{{- $typesType := "Set"}}
{{- end}}
{{$key.ToCamelCase}} := types.{{$typesType}}ValueMust(
{{$key.ToPrefixCamelCase $.Name}} := types.{{$typesType}}ValueMust(
{{$key.ToPascalCase}}Type{
basetypes.ObjectType{
AttrTypes: {{$key.ToPascalCase}}Value{}.AttributeTypes(ctx),
},
},
v.{{$key.ToPascalCase}}.Elements(),
v.{{$key.ToPrefixPascalCase $.Name}}.Elements(),
)

if v.{{$key.ToPascalCase}}.IsNull() {
{{$key.ToCamelCase}} = types.{{$typesType}}Null(
if v.{{$key.ToPrefixPascalCase $.Name}}.IsNull() {
{{$key.ToPrefixCamelCase $.Name}} = types.{{$typesType}}Null(
{{$key.ToPascalCase}}Type{
basetypes.ObjectType{
AttrTypes: {{$key.ToPascalCase}}Value{}.AttributeTypes(ctx),
Expand All @@ -27,8 +27,8 @@ AttrTypes: {{$key.ToPascalCase}}Value{}.AttributeTypes(ctx),
)
}

if v.{{$key.ToPascalCase}}.IsUnknown() {
{{$key.ToCamelCase}} = types.{{$typesType}}Unknown(
if v.{{$key.ToPrefixPascalCase $.Name}}.IsUnknown() {
{{$key.ToPrefixCamelCase $.Name}} = types.{{$typesType}}Unknown(
{{$key.ToPascalCase}}Type{
basetypes.ObjectType{
AttrTypes: {{$key.ToPascalCase}}Value{}.AttributeTypes(ctx),
Expand Down Expand Up @@ -68,9 +68,9 @@ map[string]attr.Type{
map[string]attr.Value{
{{- range $key, $value := .AttributeTypes }}
{{- if eq $value "ListNested" "MapNested" "SetNested" "SingleNested"}}
"{{$key}}": {{$key.ToCamelCase}},
"{{$key}}": {{$key.ToPrefixCamelCase $.Name}},
{{- else}}
"{{$key}}": v.{{$key.ToPascalCase}},
"{{$key}}": v.{{$key.ToPrefixPascalCase $.Name}},
{{- end}}
{{- end}}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, {{len .AttrTypes}})

{{range $key, $value := .AttrTypes }}
val, err = v.{{$key.ToPascalCase}}.ToTerraformValue(ctx)
val, err = v.{{$key.ToPrefixPascalCase $.Name}}.ToTerraformValue(ctx)

if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
Expand Down
2 changes: 1 addition & 1 deletion internal/schema/templates/nested_object_value_value.gotmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type {{.Name}}Value struct {
{{- range $key, $value := .AttrValues }}
{{$key.ToPascalCase}} {{$value}} `tfsdk:"{{$key}}"`
{{$key.ToPrefixPascalCase $.Name}} {{$value}} `tfsdk:"{{$key}}"`
{{- end}}
state attr.ValueState
}

0 comments on commit bdee822

Please sign in to comment.