Skip to content

Commit

Permalink
Generate custom type and value types and to/from methods for primitiv…
Browse files Browse the repository at this point in the history
…es (#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)
  • Loading branch information
bendbennett authored Oct 19, 2023
1 parent c2ed5fc commit 99e04d2
Show file tree
Hide file tree
Showing 156 changed files with 8,560 additions and 192 deletions.
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

0 comments on commit 99e04d2

Please sign in to comment.