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 map attribute for data source, provider and resource (#74)
  • Loading branch information
bendbennett committed Oct 24, 2023
1 parent b78f02c commit 2ad5b49
Show file tree
Hide file tree
Showing 37 changed files with 1,742 additions and 30 deletions.
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
}
2 changes: 1 addition & 1 deletion internal/datasource_generate/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var listAttributeTemplate string
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 Down
78 changes: 75 additions & 3 deletions internal/datasource_generate/map_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 GeneratorMapAttribute struct {
schema.MapAttribute

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

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

t, err := template.New("map_attribute").Parse(mapAttributeTemplate)
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 GeneratorMapAttribute) ModelField(name generatorschema.FrameworkIdentifi
ValueType: model.MapValueType,
}

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

var buf bytes.Buffer

listType := generatorschema.NewCustomMapType(name)

b, err := listType.Render()

if err != nil {
return nil, err
}

buf.Write(b)

elemType := generatorschema.GetElementType(g.ElementType)

listValue := generatorschema.NewCustomMapValue(name, elemType)

b, err = listValue.Render()

if err != nil {
return nil, err
}

buf.Write(b)

return buf.Bytes(), nil
}

func (g GeneratorMapAttribute) 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.NewToFromMap(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/map_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 TestGeneratorMapAttribute_Schema(t *testing.T) {
Expand Down Expand Up @@ -284,7 +285,44 @@ ElementType: types.StringType,
},
expected: `
"map_attribute": schema.MapAttribute{
ElementType: types.StringType,
CustomType: my_custom_type,
},`,
},

"associated-external-type": {
input: GeneratorMapAttribute{
AssociatedExternalType: &generatorschema.AssocExtType{
AssociatedExternalType: &specschema.AssociatedExternalType{
Type: "*api.MapAttribute",
},
},
ElementType: specschema.ElementType{
String: &specschema.StringType{},
},
},
expected: `
"map_attribute": schema.MapAttribute{
CustomType: MapAttributeType{
types.MapType{
ElemType: types.StringType,
},
},
},`,
},

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

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

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

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

func convertMapAttribute(a *provider.MapAttribute) (provider_generate.GeneratorMapAttribute, error) {
Expand All @@ -27,8 +28,9 @@ func convertMapAttribute(a *provider.MapAttribute) (provider_generate.GeneratorM
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/provider_generate/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var listAttributeTemplate string
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 Down
Loading

0 comments on commit 2ad5b49

Please sign in to comment.