Skip to content

Commit

Permalink
Adding generation of custom type and value type, and to/from methods …
Browse files Browse the repository at this point in the history
…for set attribute for data source, and list, and set attribute for provider and resource (#74)
  • Loading branch information
bendbennett committed Oct 23, 2023
1 parent 5d47c5d commit 8cc80fd
Show file tree
Hide file tree
Showing 41 changed files with 2,258 additions and 48 deletions.
8 changes: 5 additions & 3 deletions internal/datasource_convert/set_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 convertSetAttribute(a *datasource.SetAttribute) (datasource_generate.GeneratorSetAttribute, error) {
Expand All @@ -28,8 +29,9 @@ func convertSetAttribute(a *datasource.SetAttribute) (datasource_generate.Genera
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
},

CustomType: a.CustomType,
ElementType: a.ElementType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
ElementType: a.ElementType,
Validators: a.Validators,
}, nil
}
2 changes: 1 addition & 1 deletion internal/datasource_generate/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var numberAttributeTemplate string
var objectAttributeGoTemplate string

//go:embed templates/set_attribute.gotmpl
var setAttributeGoTemplate string
var setAttributeTemplate string

//go:embed templates/set_nested_attribute.gotmpl
var setNestedAttributeGoTemplate string
Expand Down
78 changes: 75 additions & 3 deletions internal/datasource_generate/set_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 GeneratorSetAttribute struct {
schema.SetAttribute

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 Down Expand Up @@ -46,6 +49,12 @@ func (g GeneratorSetAttribute) Imports() *generatorschema.Imports {
imports.Append(customValidatorImports)
}

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

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

return imports
}

Expand Down Expand Up @@ -103,6 +112,7 @@ func (g GeneratorSetAttribute) Equal(ga generatorschema.GeneratorAttribute) bool
func (g GeneratorSetAttribute) Schema(name generatorschema.FrameworkIdentifier) (string, error) {
type attribute struct {
Name string
CustomType string
ElementType string
GeneratorSetAttribute GeneratorSetAttribute
}
Expand All @@ -113,12 +123,19 @@ func (g GeneratorSetAttribute) Schema(name generatorschema.FrameworkIdentifier)
GeneratorSetAttribute: g,
}

t, err := template.New("set_attribute").Parse(setAttributeGoTemplate)
switch {
case g.CustomType != nil:
a.CustomType = g.CustomType.Type
case g.AssociatedExternalType != nil:
a.CustomType = fmt.Sprintf("%sType{\ntypes.SetType{\nElemType: %s,\n},\n}", name.ToPascalCase(), generatorschema.GetElementType(g.ElementType))
}

t, err := template.New("Set_attribute").Parse(setAttributeTemplate)
if err != nil {
return "", err
}

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

Expand All @@ -139,9 +156,64 @@ func (g GeneratorSetAttribute) ModelField(name generatorschema.FrameworkIdentifi
ValueType: model.SetValueType,
}

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 GeneratorSetAttribute) CustomTypeAndValue(name string) ([]byte, error) {
if g.AssociatedExternalType == nil {
return nil, nil
}

var buf bytes.Buffer

listType := generatorschema.NewCustomSetType(name)

b, err := listType.Render()

if err != nil {
return nil, err
}

buf.Write(b)

elemType := generatorschema.GetElementType(g.ElementType)

listValue := generatorschema.NewCustomSetValue(name, elemType)

b, err = listValue.Render()

if err != nil {
return nil, err
}

buf.Write(b)

return buf.Bytes(), nil
}

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

elementTypeType := generatorschema.GetElementType(g.ElementType)
elementTypeValue := generatorschema.GetElementValueType(g.ElementType)
elementFrom := generatorschema.GetElementFromFunc(g.ElementType)

toFrom := generatorschema.NewToFromSet(name, g.AssociatedExternalType, elementTypeType, elementTypeValue, elementFrom)

b, err := toFrom.Render()

if err != nil {
return nil, err
}

return b, nil
}
71 changes: 70 additions & 1 deletion internal/datasource_generate/set_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

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

func TestGeneratorSetAttribute_Schema(t *testing.T) {
Expand Down Expand Up @@ -284,7 +285,44 @@ ElementType: types.StringType,
},
expected: `
"set_attribute": schema.SetAttribute{
ElementType: types.StringType,
CustomType: my_custom_type,
},`,
},

"associated-external-type": {
input: GeneratorSetAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ListAttribute",
},
},
ElementType: specschema.ElementType{
String: &specschema.StringType{},
},
},
expected: `
"set_attribute": schema.SetAttribute{
CustomType: SetAttributeType{
types.SetType{
ElemType: types.StringType,
},
},
},`,
},

"custom-type-overriding-associated-external-type": {
input: GeneratorSetAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.ListAttribute",
},
},
CustomType: &specschema.CustomType{
Type: "my_custom_type",
},
},
expected: `
"set_attribute": schema.SetAttribute{
CustomType: my_custom_type,
},`,
},
Expand Down Expand Up @@ -644,6 +682,37 @@ func TestGeneratorSetAttribute_ModelField(t *testing.T) {
TfsdkName: "set_attribute",
},
},
"associated-external-type": {
input: GeneratorSetAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.BoolAttribute",
},
},
},
expected: model.Field{
Name: "SetAttribute",
ValueType: "SetAttributeValue",
TfsdkName: "set_attribute",
},
},
"custom-type-overriding-associated-external-type": {
input: GeneratorSetAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.BoolAttribute",
},
},
CustomType: &specschema.CustomType{
ValueType: "my_custom_value_type",
},
},
expected: model.Field{
Name: "SetAttribute",
ValueType: "my_custom_value_type",
TfsdkName: "set_attribute",
},
},
}

for name, testCase := range testCases {
Expand Down
4 changes: 4 additions & 0 deletions internal/datasource_generate/templates/set_attribute.gotmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

"{{.Name}}": schema.SetAttribute{
{{- if .CustomType}}
CustomType: {{.CustomType}},
{{- else}}
ElementType: {{.ElementType}},
{{- end}}
{{- template "common_attribute" .GeneratorSetAttribute }}
{{- if gt (len .GeneratorSetAttribute.Validators) 0 }}
Validators: []validator.Set{
Expand Down
8 changes: 5 additions & 3 deletions internal/provider_convert/list_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/provider/schema"

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

func convertListAttribute(a *provider.ListAttribute) (provider_generate.GeneratorListAttribute, error) {
Expand All @@ -27,8 +28,9 @@ func convertListAttribute(a *provider.ListAttribute) (provider_generate.Generato
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
},

CustomType: a.CustomType,
ElementType: a.ElementType,
Validators: a.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
ElementType: a.ElementType,
Validators: a.Validators,
}, nil
}
9 changes: 6 additions & 3 deletions internal/provider_convert/set_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/provider/schema"

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

func convertSetAttribute(a *provider.SetAttribute) (provider_generate.GeneratorSetAttribute, error) {
Expand All @@ -26,8 +27,10 @@ func convertSetAttribute(a *provider.SetAttribute) (provider_generate.GeneratorS
MarkdownDescription: description(a.Description),
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
},
CustomType: a.CustomType,
ElementType: a.ElementType,
Validators: a.Validators,

AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
CustomType: a.CustomType,
ElementType: a.ElementType,
Validators: a.Validators,
}, nil
}
4 changes: 2 additions & 2 deletions internal/provider_generate/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var float64AttributeTemplate string
var int64AttributeTemplate string

//go:embed templates/list_attribute.gotmpl
var listAttributeGoTemplate string
var listAttributeTemplate string

//go:embed templates/list_nested_attribute.gotmpl
var listNestedAttributeGoTemplate string
Expand All @@ -37,7 +37,7 @@ var numberAttributeTemplate string
var objectAttributeGoTemplate string

//go:embed templates/set_attribute.gotmpl
var setAttributeGoTemplate string
var setAttributeTemplate string

//go:embed templates/set_nested_attribute.gotmpl
var setNestedAttributeGoTemplate string
Expand Down
Loading

0 comments on commit 8cc80fd

Please sign in to comment.