Skip to content

Commit 0b1c795

Browse files
authored
Fix nested attribute name and generated custom value method name conflicts (#81)
* Prefixing nested attribute names with the name of the parent attribute when the attribute name matches any of the generated custom value method names (#76) * Adding changelog (#76) * Renaming input arg (#76) * Updating tests (#76) * Add naming collision handling for collection and object attributes (#76)
1 parent 27c48b2 commit 0b1c795

20 files changed

+1306
-38
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: Fix nested attribute name and generated custom value method name conflicts
3+
time: 2023-10-26T13:30:48.63762+01:00
4+
custom:
5+
Issue: "81"

internal/datasource_generate/object_attribute.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"text/template"
1111

12+
"github.com/hashicorp/terraform-plugin-codegen-spec/code"
1213
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
1314
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1415

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

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

59+
for _, v := range g.AttrTypes() {
60+
if v.Number != nil && g.AssociatedExternalType == nil {
61+
imports.Add(code.Import{
62+
Path: generatorschema.MathBigImport,
63+
})
64+
}
65+
}
66+
5867
return imports
5968
}
6069

internal/datasource_generate/object_attribute_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
9595
},
9696
},
9797
},
98+
// verifies that math/big is imported when object has
99+
// attribute type that is number type.
100+
"object-with-attr-type-number": {
101+
input: GeneratorObjectAttribute{
102+
AttributeTypes: specschema.ObjectAttributeTypes{
103+
{
104+
Name: "number",
105+
Number: &specschema.NumberType{},
106+
},
107+
},
108+
},
109+
expected: []code.Import{
110+
{
111+
Path: generatorschema.TypesImport,
112+
},
113+
{
114+
Path: generatorschema.AttrImport,
115+
},
116+
{
117+
Path: generatorschema.MathBigImport,
118+
},
119+
},
120+
},
98121
"object-with-attr-type-bool-with-import": {
99122
input: GeneratorObjectAttribute{
100123
AttributeTypes: specschema.ObjectAttributeTypes{
@@ -361,6 +384,43 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
361384
},
362385
},
363386
},
387+
// verifies that math/big is not imported when associated external type
388+
// is specified.
389+
"associated-external-type-with-number-attribute": {
390+
input: GeneratorObjectAttribute{
391+
AssociatedExternalType: &generatorschema.AssocExtType{
392+
AssociatedExternalType: &specschema.AssociatedExternalType{
393+
Type: "*api.ObjectAttribute",
394+
},
395+
},
396+
AttributeTypes: specschema.ObjectAttributeTypes{
397+
{
398+
Name: "number",
399+
Number: &specschema.NumberType{},
400+
},
401+
},
402+
},
403+
expected: []code.Import{
404+
{
405+
Path: "github.com/hashicorp/terraform-plugin-framework/types",
406+
},
407+
{
408+
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
409+
},
410+
{
411+
Path: "fmt",
412+
},
413+
{
414+
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
415+
},
416+
{
417+
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
418+
},
419+
{
420+
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
421+
},
422+
},
423+
},
364424
}
365425

366426
for name, testCase := range testCases {

internal/provider_generate/object_attribute.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"text/template"
1111

12+
"github.com/hashicorp/terraform-plugin-codegen-spec/code"
1213
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
1314
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
1415

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

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

59+
for _, v := range g.AttrTypes() {
60+
if v.Number != nil && g.AssociatedExternalType == nil {
61+
imports.Add(code.Import{
62+
Path: generatorschema.MathBigImport,
63+
})
64+
}
65+
}
66+
5867
return imports
5968
}
6069

internal/provider_generate/object_attribute_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
9595
},
9696
},
9797
},
98+
// verifies that math/big is imported when object has
99+
// attribute type that is number type.
100+
"object-with-attr-type-number": {
101+
input: GeneratorObjectAttribute{
102+
AttributeTypes: specschema.ObjectAttributeTypes{
103+
{
104+
Name: "number",
105+
Number: &specschema.NumberType{},
106+
},
107+
},
108+
},
109+
expected: []code.Import{
110+
{
111+
Path: generatorschema.TypesImport,
112+
},
113+
{
114+
Path: generatorschema.AttrImport,
115+
},
116+
{
117+
Path: generatorschema.MathBigImport,
118+
},
119+
},
120+
},
98121
"object-with-attr-type-bool-with-import": {
99122
input: GeneratorObjectAttribute{
100123
AttributeTypes: specschema.ObjectAttributeTypes{
@@ -361,6 +384,43 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
361384
},
362385
},
363386
},
387+
// verifies that math/big is not imported when associated external type
388+
// is specified.
389+
"associated-external-type-with-number-attribute": {
390+
input: GeneratorObjectAttribute{
391+
AssociatedExternalType: &generatorschema.AssocExtType{
392+
AssociatedExternalType: &specschema.AssociatedExternalType{
393+
Type: "*api.ObjectAttribute",
394+
},
395+
},
396+
AttributeTypes: specschema.ObjectAttributeTypes{
397+
{
398+
Name: "number",
399+
Number: &specschema.NumberType{},
400+
},
401+
},
402+
},
403+
expected: []code.Import{
404+
{
405+
Path: "github.com/hashicorp/terraform-plugin-framework/types",
406+
},
407+
{
408+
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
409+
},
410+
{
411+
Path: "fmt",
412+
},
413+
{
414+
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
415+
},
416+
{
417+
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
418+
},
419+
{
420+
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
421+
},
422+
},
423+
},
364424
}
365425

366426
for name, testCase := range testCases {

internal/resource_generate/object_attribute.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"text/template"
1111

12+
"github.com/hashicorp/terraform-plugin-codegen-spec/code"
1213
specschema "github.com/hashicorp/terraform-plugin-codegen-spec/schema"
1314
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1415

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

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

71+
for _, v := range g.AttrTypes() {
72+
if v.Number != nil && g.AssociatedExternalType == nil {
73+
imports.Add(code.Import{
74+
Path: generatorschema.MathBigImport,
75+
})
76+
}
77+
}
78+
7079
return imports
7180
}
7281

internal/resource_generate/object_attribute_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
9595
},
9696
},
9797
},
98+
// verifies that math/big is imported when object has
99+
// attribute type that is number type.
100+
"object-with-attr-type-number": {
101+
input: GeneratorObjectAttribute{
102+
AttributeTypes: specschema.ObjectAttributeTypes{
103+
{
104+
Name: "number",
105+
Number: &specschema.NumberType{},
106+
},
107+
},
108+
},
109+
expected: []code.Import{
110+
{
111+
Path: generatorschema.TypesImport,
112+
},
113+
{
114+
Path: generatorschema.AttrImport,
115+
},
116+
{
117+
Path: generatorschema.MathBigImport,
118+
},
119+
},
120+
},
98121
"object-with-attr-type-bool-with-import": {
99122
input: GeneratorObjectAttribute{
100123
AttributeTypes: specschema.ObjectAttributeTypes{
@@ -514,6 +537,43 @@ func TestGeneratorObjectAttribute_Imports(t *testing.T) {
514537
},
515538
},
516539
},
540+
// verifies that math/big is not imported when associated external type
541+
// is specified.
542+
"associated-external-type-with-number-attribute": {
543+
input: GeneratorObjectAttribute{
544+
AssociatedExternalType: &generatorschema.AssocExtType{
545+
AssociatedExternalType: &specschema.AssociatedExternalType{
546+
Type: "*api.ObjectAttribute",
547+
},
548+
},
549+
AttributeTypes: specschema.ObjectAttributeTypes{
550+
{
551+
Name: "number",
552+
Number: &specschema.NumberType{},
553+
},
554+
},
555+
},
556+
expected: []code.Import{
557+
{
558+
Path: "github.com/hashicorp/terraform-plugin-framework/types",
559+
},
560+
{
561+
Path: "github.com/hashicorp/terraform-plugin-framework/attr",
562+
},
563+
{
564+
Path: "fmt",
565+
},
566+
{
567+
Path: "github.com/hashicorp/terraform-plugin-framework/diag",
568+
},
569+
{
570+
Path: "github.com/hashicorp/terraform-plugin-go/tftypes",
571+
},
572+
{
573+
Path: "github.com/hashicorp/terraform-plugin-framework/types/basetypes",
574+
},
575+
},
576+
},
517577
}
518578

519579
for name, testCase := range testCases {

0 commit comments

Comments
 (0)