Skip to content
Merged
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20231026-133048.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: Fix nested attribute name and generated custom value method name conflicts
time: 2023-10-26T13:30:48.63762+01:00
custom:
Issue: "81"
9 changes: 9 additions & 0 deletions internal/datasource_generate/object_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"text/template"

"github.com/hashicorp/terraform-plugin-codegen-spec/code"
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

Expand Down Expand Up @@ -55,6 +56,14 @@ func (g GeneratorObjectAttribute) Imports() *generatorschema.Imports {

imports.Append(g.AssociatedExternalType.Imports())

for _, v := range g.AttrTypes() {
if v.Number != nil && g.AssociatedExternalType == nil {
imports.Add(code.Import{
Path: generatorschema.MathBigImport,
})
}
}

return imports
}

Expand Down
60 changes: 60 additions & 0 deletions internal/datasource_generate/object_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
},
},
},
// verifies that math/big is imported when object has
// attribute type that is number type.
"object-with-attr-type-number": {
input: GeneratorObjectAttribute{
AttributeTypes: specschema.ObjectAttributeTypes{
{
Name: "number",
Number: &specschema.NumberType{},
},
},
},
expected: []code.Import{
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
{
Path: generatorschema.MathBigImport,
},
},
},
"object-with-attr-type-bool-with-import": {
input: GeneratorObjectAttribute{
AttributeTypes: specschema.ObjectAttributeTypes{
Expand Down Expand Up @@ -361,6 +384,43 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
},
},
},
// verifies that math/big is not imported when associated external type
// is specified.
"associated-external-type-with-number-attribute": {
input: GeneratorObjectAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ObjectAttribute",
},
},
AttributeTypes: specschema.ObjectAttributeTypes{
{
Name: "number",
Number: &specschema.NumberType{},
},
},
},
expected: []code.Import{
{
Path: "github.com/hashicorp/terraform-plugin-framework/types",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
},
{
Path: "fmt",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
},
{
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
},
},
},
}

for name, testCase := range testCases {
Expand Down
9 changes: 9 additions & 0 deletions internal/provider_generate/object_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"text/template"

"github.com/hashicorp/terraform-plugin-codegen-spec/code"
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"

Expand Down Expand Up @@ -55,6 +56,14 @@ func (g GeneratorObjectAttribute) Imports() *generatorschema.Imports {

imports.Append(g.AssociatedExternalType.Imports())

for _, v := range g.AttrTypes() {
if v.Number != nil && g.AssociatedExternalType == nil {
imports.Add(code.Import{
Path: generatorschema.MathBigImport,
})
}
}

return imports
}

Expand Down
60 changes: 60 additions & 0 deletions internal/provider_generate/object_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
},
},
},
// verifies that math/big is imported when object has
// attribute type that is number type.
"object-with-attr-type-number": {
input: GeneratorObjectAttribute{
AttributeTypes: specschema.ObjectAttributeTypes{
{
Name: "number",
Number: &specschema.NumberType{},
},
},
},
expected: []code.Import{
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
{
Path: generatorschema.MathBigImport,
},
},
},
"object-with-attr-type-bool-with-import": {
input: GeneratorObjectAttribute{
AttributeTypes: specschema.ObjectAttributeTypes{
Expand Down Expand Up @@ -361,6 +384,43 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
},
},
},
// verifies that math/big is not imported when associated external type
// is specified.
"associated-external-type-with-number-attribute": {
input: GeneratorObjectAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ObjectAttribute",
},
},
AttributeTypes: specschema.ObjectAttributeTypes{
{
Name: "number",
Number: &specschema.NumberType{},
},
},
},
expected: []code.Import{
{
Path: "github.com/hashicorp/terraform-plugin-framework/types",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
},
{
Path: "fmt",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
},
{
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
},
},
},
}

for name, testCase := range testCases {
Expand Down
9 changes: 9 additions & 0 deletions internal/resource_generate/object_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"text/template"

"github.com/hashicorp/terraform-plugin-codegen-spec/code"
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"

Expand Down Expand Up @@ -67,6 +68,14 @@ func (g GeneratorObjectAttribute) Imports() *generatorschema.Imports {

imports.Append(g.AssociatedExternalType.Imports())

for _, v := range g.AttrTypes() {
if v.Number != nil && g.AssociatedExternalType == nil {
imports.Add(code.Import{
Path: generatorschema.MathBigImport,
})
}
}

return imports
}

Expand Down
60 changes: 60 additions & 0 deletions internal/resource_generate/object_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
},
},
},
// verifies that math/big is imported when object has
// attribute type that is number type.
"object-with-attr-type-number": {
input: GeneratorObjectAttribute{
AttributeTypes: specschema.ObjectAttributeTypes{
{
Name: "number",
Number: &specschema.NumberType{},
},
},
},
expected: []code.Import{
{
Path: generatorschema.TypesImport,
},
{
Path: generatorschema.AttrImport,
},
{
Path: generatorschema.MathBigImport,
},
},
},
"object-with-attr-type-bool-with-import": {
input: GeneratorObjectAttribute{
AttributeTypes: specschema.ObjectAttributeTypes{
Expand Down Expand Up @@ -514,6 +537,43 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
},
},
},
// verifies that math/big is not imported when associated external type
// is specified.
"associated-external-type-with-number-attribute": {
input: GeneratorObjectAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ObjectAttribute",
},
},
AttributeTypes: specschema.ObjectAttributeTypes{
{
Name: "number",
Number: &specschema.NumberType{},
},
},
},
expected: []code.Import{
{
Path: "github.com/hashicorp/terraform-plugin-framework/types",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
},
{
Path: "fmt",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
},
{
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
},
{
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
},
},
},
}

for name, testCase := range testCases {
Expand Down
Loading