Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate custom type and value types and to/from methods for primitives #59

Merged
merged 23 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8d8a351
Refactoring to move generation of to/from functions to attributes and…
bendbennett Oct 4, 2023
9120230
Moving schema.gotmpl
bendbennett Oct 4, 2023
5eb6acb
Renaming interface method
bendbennett Oct 4, 2023
c75a95c
Removing unneeded template variable
bendbennett Oct 4, 2023
5ab1e98
Renaming method
bendbennett Oct 4, 2023
e70ff49
Renaming methods
bendbennett Oct 4, 2023
2c7e56c
Inline function
bendbennett Oct 4, 2023
c94153c
Renaming
bendbennett Oct 4, 2023
6993ad3
Adding tests for to/from method generated code
bendbennett Oct 4, 2023
67961ef
Merge branch 'main' into to-from-refactor
bendbennett Oct 4, 2023
29e5374
Adding initial implementation of custom type and value types for data…
bendbennett Oct 6, 2023
670b7cb
Merge remote-tracking branch 'origin/main' into assoc-ext-type-primit…
bendbennett Oct 6, 2023
251261d
Merge remote-tracking branch 'origin/main' into assoc-ext-type-primit…
bendbennett Oct 16, 2023
c0e4690
Updating data source attributes to use generated type and value type …
bendbennett Oct 17, 2023
1cee417
Populating data source associated external type
bendbennett Oct 17, 2023
48c9613
Setting up generation of custom type and value types and to/from func…
bendbennett Oct 17, 2023
ac34c1a
Setting up generation of custom type and value types and to/from func…
bendbennett Oct 17, 2023
422cfb4
Adding test coverage for associated external type
bendbennett Oct 17, 2023
e8dff65
Merge remote-tracking branch 'origin/main' into assoc-ext-type-primit…
bendbennett Oct 17, 2023
cc6db53
Temporarily using sha for codegen-spec
bendbennett Oct 17, 2023
82297af
Adding changelog (#70)
bendbennett Oct 18, 2023
71f0603
Adding tests for custom string type and value, and to/from methods (#70)
bendbennett Oct 19, 2023
480283b
Using latest codegen-spec SHA from main (#70)
bendbennett Oct 19, 2023
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
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20231018-094607.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: Adds code generation for Bool, Float64, Int64, Number, and String attributes
that have an associated external type
time: 2023-10-18T09:46:07.467187+01:00
custom:
Issue: "59"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.0
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231019064449-867ccf6fb279
github.com/hashicorp/terraform-plugin-framework v1.4.0
github.com/mattn/go-colorable v0.1.12
github.com/mitchellh/cli v1.1.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.0 h1:flL5dprli2h54RxewQi6po02am0zXDRq6nsV6c4WQ/I=
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.0/go.mod h1:PQn6bDD8UWoAVJoHXqFk2i/RmLbeQBjbiP38i+E+YIw=
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231019064449-867ccf6fb279 h1:9D8ydY5s8Fw76pqCFtwlm9X7wVJdFLMK2FAqyQoOZT0=
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231019064449-867ccf6fb279/go.mod h1:PQn6bDD8UWoAVJoHXqFk2i/RmLbeQBjbiP38i+E+YIw=
github.com/hashicorp/terraform-plugin-framework v1.4.0 h1:WKbtCRtNrjsh10eA7NZvC/Qyr7zp77j+D21aDO5th9c=
github.com/hashicorp/terraform-plugin-framework v1.4.0/go.mod h1:XC0hPcQbBvlbxwmjxuV/8sn8SbZRg4XwGMs22f+kqV0=
github.com/hashicorp/terraform-plugin-go v0.19.0 h1:BuZx/6Cp+lkmiG0cOBk6Zps0Cb2tmqQpDM3iAtnhDQU=
Expand Down
6 changes: 4 additions & 2 deletions internal/datasource_convert/bool_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

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

CustomType: a.CustomType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}
6 changes: 4 additions & 2 deletions internal/datasource_convert/float64_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

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

CustomType: a.CustomType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}
6 changes: 4 additions & 2 deletions internal/datasource_convert/int64_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

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

CustomType: a.CustomType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}
6 changes: 4 additions & 2 deletions internal/datasource_convert/number_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

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

CustomType: a.CustomType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}
6 changes: 4 additions & 2 deletions internal/datasource_convert/string_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/datasource_generate"
generatorschema "github.com/hashicorp/terraform-plugin-codegen-framework/internal/schema"
)

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

CustomType: a.CustomType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}
73 changes: 70 additions & 3 deletions internal/datasource_generate/bool_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package datasource_generate

import (
"bytes"
"fmt"
"strings"
"text/template"

Expand All @@ -17,6 +19,7 @@ import (
type GeneratorBoolAttribute struct {
schema.BoolAttribute

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

if g.AssociatedExternalType != nil {
imports.Append(generatorschema.AssociatedExternalTypeImports())
}

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

return imports
}

Expand All @@ -61,6 +70,7 @@ func (g GeneratorBoolAttribute) Equal(ga generatorschema.GeneratorAttribute) boo
func (g GeneratorBoolAttribute) Schema(name generatorschema.FrameworkIdentifier) (string, error) {
type attribute struct {
Name string
CustomType string
GeneratorBoolAttribute GeneratorBoolAttribute
}

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

t, err := template.New("bool_attribute").Parse(boolAttributeGoTemplate)
switch {
case g.CustomType != nil:
a.CustomType = g.CustomType.Type
case g.AssociatedExternalType != nil:
a.CustomType = fmt.Sprintf("%sType{}", name.ToPascalCase())
}

t, err := template.New("bool_attribute").Parse(boolAttributeTemplate)

if err != nil {
return "", err
}

if _, err = addCommonAttributeTemplate(t); err != nil {
if _, err = addAttributeTemplate(t); err != nil {
return "", err
}

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

if g.CustomType != nil {
switch {
case g.CustomType != nil:
field.ValueType = g.CustomType.ValueType
case g.AssociatedExternalType != nil:
field.ValueType = fmt.Sprintf("%sValue", name.ToPascalCase())
}

return field, nil
}

func (g GeneratorBoolAttribute) CustomTypeAndValue(name string) ([]byte, error) {
if g.AssociatedExternalType == nil {
return nil, nil
}

var buf bytes.Buffer

boolType := generatorschema.NewCustomBoolType(name)

b, err := boolType.Render()

if err != nil {
return nil, err
}

buf.Write(b)

boolValue := generatorschema.NewCustomBoolValue(name)

b, err = boolValue.Render()

if err != nil {
return nil, err
}

buf.Write(b)

return buf.Bytes(), nil
}

func (g GeneratorBoolAttribute) ToFromFunctions(name string) ([]byte, error) {
if g.AssociatedExternalType == nil {
return nil, nil
}

toFrom := generatorschema.NewToFromBool(name, g.AssociatedExternalType)

b, err := toFrom.Render()

if err != nil {
return nil, err
}

return b, nil
}
Loading