Skip to content
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
10 changes: 10 additions & 0 deletions internal/convert/custom_type_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ func (c CustomTypePrimitive) ValueType() string {

return ""
}

// TypeString returns the custom type string (e.g., "jsontypes.NormalizedType{}")
// when a custom type is specified. Returns empty string otherwise.
func (c CustomTypePrimitive) TypeString() string {
if c.customType != nil {
return c.customType.Type
}

return ""
}
116 changes: 116 additions & 0 deletions internal/convert/custom_type_primitive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package convert

import (
"testing"

specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
)

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

testCases := map[string]struct {
input CustomTypePrimitive
expected string
}{
"no-custom-type": {
input: NewCustomTypePrimitive(nil, nil, "attr"),
expected: "",
},
"custom-type": {
input: NewCustomTypePrimitive(
&specschema.CustomType{
Type: "jsontypes.NormalizedType{}",
ValueType: "jsontypes.Normalized",
},
nil,
"attr",
),
expected: "jsontypes.NormalizedType{}",
},
"associated-external-type-only": {
input: NewCustomTypePrimitive(
nil,
&specschema.AssociatedExternalType{
Type: "*api.ExtString",
},
"attr",
),
expected: "",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.TypeString()

if got != testCase.expected {
t.Errorf("expected %q, got %q", testCase.expected, got)
}
})
}
}

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

testCases := map[string]struct {
input CustomTypePrimitive
expected string
}{
"no-custom-type": {
input: NewCustomTypePrimitive(nil, nil, "attr"),
expected: "",
},
"custom-type": {
input: NewCustomTypePrimitive(
&specschema.CustomType{
Type: "jsontypes.NormalizedType{}",
ValueType: "jsontypes.Normalized",
},
nil,
"attr",
),
expected: "jsontypes.Normalized",
},
"associated-external-type-only": {
input: NewCustomTypePrimitive(
nil,
&specschema.AssociatedExternalType{
Type: "*api.ExtString",
},
"attr",
),
expected: "AttrValue",
},
"custom-type-overrides-associated": {
input: NewCustomTypePrimitive(
&specschema.CustomType{
ValueType: "my_custom_value",
},
&specschema.AssociatedExternalType{
Type: "*api.ExtString",
},
"attr",
),
expected: "my_custom_value",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.ValueType()

if got != testCase.expected {
t.Errorf("expected %q, got %q", testCase.expected, got)
}
})
}
}
8 changes: 8 additions & 0 deletions internal/datasource/string_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (g GeneratorStringAttribute) ToFromFunctions(name string) ([]byte, error) {

// AttrType returns a string representation of a basetypes.StringTypable type.
func (g GeneratorStringAttribute) AttrType(name schema.FrameworkIdentifier) (string, error) {
if ct := g.CustomType.TypeString(); ct != "" {
return ct, nil
}

if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sType{}", name.ToPascalCase()), nil
}
Expand All @@ -196,6 +200,10 @@ func (g GeneratorStringAttribute) AttrType(name schema.FrameworkIdentifier) (str

// AttrValue returns a string representation of a basetypes.StringValuable type.
func (g GeneratorStringAttribute) AttrValue(name schema.FrameworkIdentifier) string {
if cv := g.CustomType.ValueType(); cv != "" {
return cv
}

if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sValue", name.ToPascalCase())
}
Expand Down
126 changes: 126 additions & 0 deletions internal/datasource/string_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/doitintl/terraform-plugin-codegen-framework/internal/convert"
"github.com/doitintl/terraform-plugin-codegen-framework/internal/model"
"github.com/doitintl/terraform-plugin-codegen-framework/internal/schema"
)

func TestGeneratorStringAttribute_New(t *testing.T) {
Expand Down Expand Up @@ -406,3 +407,128 @@ func TestGeneratorStringAttribute_ModelField(t *testing.T) {
})
}
}

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

testCases := map[string]struct {
input GeneratorStringAttribute
expected string
}{
"default": {
expected: "basetypes.StringType{}",
},
"custom-type": {
input: GeneratorStringAttribute{
CustomType: convert.NewCustomTypePrimitive(
&specschema.CustomType{
Type: "jsontypes.NormalizedType{}",
ValueType: "jsontypes.Normalized",
},
nil,
"string_attribute",
),
},
expected: "jsontypes.NormalizedType{}",
},
"associated-external-type": {
input: GeneratorStringAttribute{
AssociatedExternalType: &schema.AssocExtType{},
},
expected: "StringAttributeType{}",
},
Comment thread
hanneshayashi marked this conversation as resolved.
"custom-type-overriding-associated-external-type": {
input: GeneratorStringAttribute{
CustomType: convert.NewCustomTypePrimitive(
&specschema.CustomType{
Type: "jsontypes.NormalizedType{}",
ValueType: "jsontypes.Normalized",
},
&specschema.AssociatedExternalType{
Type: "*api.ExtString",
},
"string_attribute",
),
AssociatedExternalType: &schema.AssocExtType{},
},
expected: "jsontypes.NormalizedType{}",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got, err := testCase.input.AttrType("string_attribute")

if err != nil {
t.Errorf("unexpected error: %s", err)
}

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

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

testCases := map[string]struct {
input GeneratorStringAttribute
expected string
}{
"default": {
expected: "basetypes.StringValue",
},
"custom-type": {
input: GeneratorStringAttribute{
CustomType: convert.NewCustomTypePrimitive(
&specschema.CustomType{
Type: "jsontypes.NormalizedType{}",
ValueType: "jsontypes.Normalized",
},
nil,
"string_attribute",
),
},
expected: "jsontypes.Normalized",
},
"associated-external-type": {
input: GeneratorStringAttribute{
AssociatedExternalType: &schema.AssocExtType{},
},
expected: "StringAttributeValue",
},
Comment thread
hanneshayashi marked this conversation as resolved.
"custom-type-overriding-associated-external-type": {
input: GeneratorStringAttribute{
CustomType: convert.NewCustomTypePrimitive(
&specschema.CustomType{
Type: "jsontypes.NormalizedType{}",
ValueType: "jsontypes.Normalized",
},
&specschema.AssociatedExternalType{
Type: "*api.ExtString",
},
"string_attribute",
),
AssociatedExternalType: &schema.AssocExtType{},
},
expected: "jsontypes.Normalized",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

got := testCase.input.AttrValue("string_attribute")

if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

8 changes: 8 additions & 0 deletions internal/provider/string_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (g GeneratorStringAttribute) ToFromFunctions(name string) ([]byte, error) {

// AttrType returns a string representation of a basetypes.StringTypable type.
func (g GeneratorStringAttribute) AttrType(name schema.FrameworkIdentifier) (string, error) {
if ct := g.CustomType.TypeString(); ct != "" {
return ct, nil
}

if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sType{}", name.ToPascalCase()), nil
}
Expand All @@ -196,6 +200,10 @@ func (g GeneratorStringAttribute) AttrType(name schema.FrameworkIdentifier) (str

// AttrValue returns a string representation of a basetypes.StringValuable type.
func (g GeneratorStringAttribute) AttrValue(name schema.FrameworkIdentifier) string {
if cv := g.CustomType.ValueType(); cv != "" {
return cv
}

if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sValue", name.ToPascalCase())
}
Expand Down
Loading