Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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-20231023-180006.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: Adds code generation for List, Map, Object, and Set attributes that have an
associated external type
time: 2023-10-23T18:00:06.752758+01:00
custom:
Issue: "75"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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.20231024091233-c659ac8a54fc
github.com/hashicorp/terraform-plugin-framework v1.4.0
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/mattn/go-colorable v0.1.12
github.com/mitchellh/cli v1.1.5
)
Expand All @@ -21,7 +22,6 @@ require (
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
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.20231024091233-c659ac8a54fc h1:VmMk5vOSJgpWOuBsI4ZBZkcsrkLq0fKoyKvKFnbBuxk=
github.com/hashicorp/terraform-plugin-codegen-spec v0.1.1-0.20231024091233-c659ac8a54fc/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
}
8 changes: 5 additions & 3 deletions internal/datasource_convert/map_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 convertMapAttribute(a *datasource.MapAttribute) (datasource_generate.GeneratorMapAttribute, error) {
Expand All @@ -28,8 +29,9 @@ func convertMapAttribute(a *datasource.MapAttribute) (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
}
26 changes: 14 additions & 12 deletions internal/datasource_convert/object_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@ 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 convertObjectAttribute(o *datasource.ObjectAttribute) (datasource_generate.GeneratorObjectAttribute, error) {
if o == nil {
func convertObjectAttribute(a *datasource.ObjectAttribute) (datasource_generate.GeneratorObjectAttribute, error) {
if a == nil {
return datasource_generate.GeneratorObjectAttribute{}, fmt.Errorf("*datasource.ObjectAttribute is nil")
}

return datasource_generate.GeneratorObjectAttribute{
ObjectAttribute: schema.ObjectAttribute{
Required: isRequired(o.ComputedOptionalRequired),
Optional: isOptional(o.ComputedOptionalRequired),
Computed: isComputed(o.ComputedOptionalRequired),
Sensitive: isSensitive(o.Sensitive),
Description: description(o.Description),
MarkdownDescription: description(o.Description),
DeprecationMessage: deprecationMessage(o.DeprecationMessage),
Required: isRequired(a.ComputedOptionalRequired),
Optional: isOptional(a.ComputedOptionalRequired),
Computed: isComputed(a.ComputedOptionalRequired),
Sensitive: isSensitive(a.Sensitive),
Description: description(a.Description),
MarkdownDescription: description(a.Description),
DeprecationMessage: deprecationMessage(a.DeprecationMessage),
},

AttributeTypes: o.AttributeTypes,
CustomType: o.CustomType,
Validators: o.Validators,
AssociatedExternalType: generatorschema.NewAssocExtType(a.AssociatedExternalType),
AttributeTypes: a.AttributeTypes,
CustomType: a.CustomType,
Validators: a.Validators,
}, nil
}
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
}
8 changes: 4 additions & 4 deletions internal/datasource_generate/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ 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

//go:embed templates/map_attribute.gotmpl
var mapAttributeGoTemplate string
var mapAttributeTemplate string

//go:embed templates/map_nested_attribute.gotmpl
var mapNestedAttributeGoTemplate string
Expand All @@ -34,10 +34,10 @@ var mapNestedAttributeGoTemplate string
var numberAttributeTemplate string

//go:embed templates/object_attribute.gotmpl
var objectAttributeGoTemplate string
var objectAttributeTemplate 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/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
}
Loading