Skip to content

Commit 99e04d2

Browse files
authored
Generate custom type and value types and to/from methods for primitives (#59)
* Refactoring to move generation of to/from functions to attributes and blocks * Moving schema.gotmpl * Renaming interface method * Removing unneeded template variable * Renaming method * Renaming methods * Inline function * Renaming * Adding tests for to/from method generated code * Adding initial implementation of custom type and value types for data source bool attribute along with To/From method generation * Updating data source attributes to use generated type and value type in schema and models if associated external type is defined * Populating data source associated external type * Setting up generation of custom type and value types and to/from functions for data source float64, int64, number, and string attributes * Setting up generation of custom type and value types and to/from functions for provider and resource bool, float64, int64, number, and string attributes * Adding test coverage for associated external type * Temporarily using sha for codegen-spec * Adding changelog (#70) * Adding tests for custom string type and value, and to/from methods (#70) * Using latest codegen-spec SHA from main (#70)
1 parent c2ed5fc commit 99e04d2

File tree

156 files changed

+8560
-192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+8560
-192
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: Adds code generation for Bool, Float64, Int64, Number, and String attributes
3+
that have an associated external type
4+
time: 2023-10-18T09:46:07.467187+01:00
5+
custom:
6+
Issue: "59"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/google/go-cmp v0.6.0
7-
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.0
7+
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231019064449-867ccf6fb279
88
github.com/hashicorp/terraform-plugin-framework v1.4.0
99
github.com/mattn/go-colorable v0.1.12
1010
github.com/mitchellh/cli v1.1.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+
2525
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
2626
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
2727
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
28-
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.0 h1:flL5dprli2h54RxewQi6po02am0zXDRq6nsV6c4WQ/I=
29-
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.0/go.mod h1:PQn6bDD8UWoAVJoHXqFk2i/RmLbeQBjbiP38i+E+YIw=
28+
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231019064449-867ccf6fb279 h1:9D8ydY5s8Fw76pqCFtwlm9X7wVJdFLMK2FAqyQoOZT0=
29+
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231019064449-867ccf6fb279/go.mod h1:PQn6bDD8UWoAVJoHXqFk2i/RmLbeQBjbiP38i+E+YIw=
3030
github.com/hashicorp/terraform-plugin-framework v1.4.0 h1:WKbtCRtNrjsh10eA7NZvC/Qyr7zp77j+D21aDO5th9c=
3131
github.com/hashicorp/terraform-plugin-framework v1.4.0/go.mod h1:XC0hPcQbBvlbxwmjxuV/8sn8SbZRg4XwGMs22f+kqV0=
3232
github.com/hashicorp/terraform-plugin-go v0.19.0 h1:BuZx/6Cp+lkmiG0cOBk6Zps0Cb2tmqQpDM3iAtnhDQU=

internal/datasource_convert/bool_attribute.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1111

1212
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
13+
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
1314
)
1415

1516
func convertBoolAttribute(a *datasource.BoolAttribute) (datasource_generate.GeneratorBoolAttribute, error) {
@@ -28,7 +29,8 @@ func convertBoolAttribute(a *datasource.BoolAttribute) (datasource_generate.Gene
2829
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
2930
},
3031

31-
CustomType: a.CustomType,
32-
Validators: a.Validators,
32+
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
33+
CustomType: a.CustomType,
34+
Validators: a.Validators,
3335
}, nil
3436
}

internal/datasource_convert/float64_attribute.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1111

1212
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
13+
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
1314
)
1415

1516
func convertFloat64Attribute(a *datasource.Float64Attribute) (datasource_generate.GeneratorFloat64Attribute, error) {
@@ -28,7 +29,8 @@ func convertFloat64Attribute(a *datasource.Float64Attribute) (datasource_generat
2829
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
2930
},
3031

31-
CustomType: a.CustomType,
32-
Validators: a.Validators,
32+
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
33+
CustomType: a.CustomType,
34+
Validators: a.Validators,
3335
}, nil
3436
}

internal/datasource_convert/int64_attribute.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1111

1212
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
13+
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
1314
)
1415

1516
func convertInt64Attribute(a *datasource.Int64Attribute) (datasource_generate.GeneratorInt64Attribute, error) {
@@ -28,7 +29,8 @@ func convertInt64Attribute(a *datasource.Int64Attribute) (datasource_generate.Ge
2829
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
2930
},
3031

31-
CustomType: a.CustomType,
32-
Validators: a.Validators,
32+
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
33+
CustomType: a.CustomType,
34+
Validators: a.Validators,
3335
}, nil
3436
}

internal/datasource_convert/number_attribute.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1111

1212
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
13+
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
1314
)
1415

1516
func convertNumberAttribute(a *datasource.NumberAttribute) (datasource_generate.GeneratorNumberAttribute, error) {
@@ -28,7 +29,8 @@ func convertNumberAttribute(a *datasource.NumberAttribute) (datasource_generate.
2829
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
2930
},
3031

31-
CustomType: a.CustomType,
32-
Validators: a.Validators,
32+
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
33+
CustomType: a.CustomType,
34+
Validators: a.Validators,
3335
}, nil
3436
}

internal/datasource_convert/string_attribute.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1111

1212
"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
13+
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
1314
)
1415

1516
func convertStringAttribute(a *datasource.StringAttribute) (datasource_generate.GeneratorStringAttribute, error) {
@@ -28,7 +29,8 @@ func convertStringAttribute(a *datasource.StringAttribute) (datasource_generate.
2829
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
2930
},
3031

31-
CustomType: a.CustomType,
32-
Validators: a.Validators,
32+
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
33+
CustomType: a.CustomType,
34+
Validators: a.Validators,
3335
}, nil
3436
}

internal/datasource_generate/bool_attribute.go

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package datasource_generate
55

66
import (
7+
"bytes"
8+
"fmt"
79
"strings"
810
"text/template"
911

@@ -17,6 +19,7 @@ import (
1719
type GeneratorBoolAttribute struct {
1820
schema.BoolAttribute
1921

22+
AssociatedExternalType *generatorschema.AssocExtType
2023
// The "specschema" types are used instead of the types within the attribute
2124
// because support for extracting custom import information is required.
2225
CustomType *specschema.CustomType
@@ -38,6 +41,12 @@ func (g GeneratorBoolAttribute) Imports() *generatorschema.Imports {
3841
imports.Append(customValidatorImports)
3942
}
4043

44+
if g.AssociatedExternalType != nil {
45+
imports.Append(generatorschema.AssociatedExternalTypeImports())
46+
}
47+
48+
imports.Append(g.AssociatedExternalType.Imports())
49+
4150
return imports
4251
}
4352

@@ -61,6 +70,7 @@ func (g GeneratorBoolAttribute) Equal(ga generatorschema.GeneratorAttribute) boo
6170
func (g GeneratorBoolAttribute) Schema(name generatorschema.FrameworkIdentifier) (string, error) {
6271
type attribute struct {
6372
Name string
73+
CustomType string
6474
GeneratorBoolAttribute GeneratorBoolAttribute
6575
}
6676

@@ -69,12 +79,20 @@ func (g GeneratorBoolAttribute) Schema(name generatorschema.FrameworkIdentifier)
6979
GeneratorBoolAttribute: g,
7080
}
7181

72-
t, err := template.New("bool_attribute").Parse(boolAttributeGoTemplate)
82+
switch {
83+
case g.CustomType != nil:
84+
a.CustomType = g.CustomType.Type
85+
case g.AssociatedExternalType != nil:
86+
a.CustomType = fmt.Sprintf("%sType{}", name.ToPascalCase())
87+
}
88+
89+
t, err := template.New("bool_attribute").Parse(boolAttributeTemplate)
90+
7391
if err != nil {
7492
return "", err
7593
}
7694

77-
if _, err = addCommonAttributeTemplate(t); err != nil {
95+
if _, err = addAttributeTemplate(t); err != nil {
7896
return "", err
7997
}
8098

@@ -95,9 +113,58 @@ func (g GeneratorBoolAttribute) ModelField(name generatorschema.FrameworkIdentif
95113
ValueType: model.BoolValueType,
96114
}
97115

98-
if g.CustomType != nil {
116+
switch {
117+
case g.CustomType != nil:
99118
field.ValueType = g.CustomType.ValueType
119+
case g.AssociatedExternalType != nil:
120+
field.ValueType = fmt.Sprintf("%sValue", name.ToPascalCase())
100121
}
101122

102123
return field, nil
103124
}
125+
126+
func (g GeneratorBoolAttribute) CustomTypeAndValue(name string) ([]byte, error) {
127+
if g.AssociatedExternalType == nil {
128+
return nil, nil
129+
}
130+
131+
var buf bytes.Buffer
132+
133+
boolType := generatorschema.NewCustomBoolType(name)
134+
135+
b, err := boolType.Render()
136+
137+
if err != nil {
138+
return nil, err
139+
}
140+
141+
buf.Write(b)
142+
143+
boolValue := generatorschema.NewCustomBoolValue(name)
144+
145+
b, err = boolValue.Render()
146+
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
buf.Write(b)
152+
153+
return buf.Bytes(), nil
154+
}
155+
156+
func (g GeneratorBoolAttribute) ToFromFunctions(name string) ([]byte, error) {
157+
if g.AssociatedExternalType == nil {
158+
return nil, nil
159+
}
160+
161+
toFrom := generatorschema.NewToFromBool(name, g.AssociatedExternalType)
162+
163+
b, err := toFrom.Render()
164+
165+
if err != nil {
166+
return nil, err
167+
}
168+
169+
return b, nil
170+
}

0 commit comments

Comments
 (0)