Skip to content

Commit

Permalink
Adding generation of custom type and value type with to/from methods …
Browse files Browse the repository at this point in the history
…for data source list attribute (#74)
  • Loading branch information
bendbennett committed Oct 23, 2023
1 parent 292ce20 commit 5595d81
Show file tree
Hide file tree
Showing 26 changed files with 1,432 additions and 11 deletions.
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.1-0.20231019064449-867ccf6fb279
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231020114651-05c8a44239ca
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.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-codegen-spec v0.1.1-0.20231020114651-05c8a44239ca h1:HOURkk+e71xtn3nEizi8rOd7aWawoGQ/dZ4kh+ko82M=
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231020114651-05c8a44239ca/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
8 changes: 5 additions & 3 deletions internal/datasource_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/datasource/schema"

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

func convertListAttribute(a *datasource.ListAttribute) (datasource_generate.GeneratorListAttribute, error) {
Expand All @@ -28,8 +29,9 @@ func convertListAttribute(a *datasource.ListAttribute) (datasource_generate.Gene
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 @@ -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 Down
78 changes: 75 additions & 3 deletions internal/datasource_generate/list_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 GeneratorListAttribute struct {
schema.ListAttribute

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 GeneratorListAttribute) 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 GeneratorListAttribute) Equal(ga generatorschema.GeneratorAttribute) boo
func (g GeneratorListAttribute) Schema(name generatorschema.FrameworkIdentifier) (string, error) {
type attribute struct {
Name string
CustomType string
ElementType string
GeneratorListAttribute GeneratorListAttribute
}
Expand All @@ -113,12 +123,19 @@ func (g GeneratorListAttribute) Schema(name generatorschema.FrameworkIdentifier)
GeneratorListAttribute: g,
}

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

t, err := template.New("list_attribute").Parse(listAttributeTemplate)
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 GeneratorListAttribute) ModelField(name generatorschema.FrameworkIdentif
ValueType: model.ListValueType,
}

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

var buf bytes.Buffer

listType := generatorschema.NewCustomListType(name)

b, err := listType.Render()

if err != nil {
return nil, err
}

buf.Write(b)

elemType := generatorschema.GetElementType(g.ElementType)

listValue := generatorschema.NewCustomListValue(name, elemType)

b, err = listValue.Render()

if err != nil {
return nil, err
}

buf.Write(b)

return buf.Bytes(), nil
}

func (g GeneratorListAttribute) 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.NewToFromList(name, g.AssociatedExternalType, elementTypeType, elementTypeValue, elementFrom)

b, err := toFrom.Render()

if err != nil {
return nil, err
}

return b, nil
}
1 change: 0 additions & 1 deletion internal/datasource_generate/list_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,6 @@ ElementType: types.StringType,
},
expected: `
"list_attribute": schema.ListAttribute{
ElementType: types.StringType,
CustomType: my_custom_type,
},`,
},
Expand Down
4 changes: 4 additions & 0 deletions internal/datasource_generate/templates/list_attribute.gotmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

"{{.Name}}": schema.ListAttribute{
{{- if .CustomType}}
CustomType: {{.CustomType}},
{{- else}}
ElementType: {{.ElementType}},
{{- end}}
{{- template "common_attribute" .GeneratorListAttribute }}
{{- if gt (len .GeneratorListAttribute.Validators) 0 }}
Validators: []validator.List{
Expand Down
Loading

0 comments on commit 5595d81

Please sign in to comment.