Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
feat: add map and mapNested (#20)
Browse files Browse the repository at this point in the history
* feat: add map and mapNested

* ci(generate): changes by go generate

* ci: remove unused CI to avoid error

---------

Co-authored-by: gaetanars <gaetanars@users.noreply.github.com>
  • Loading branch information
gaetanars and gaetanars authored Apr 5, 2023
1 parent 467a001 commit f84e65e
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 8 deletions.
116 changes: 116 additions & 0 deletions map_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// code generated by go generate - look at type_attribute.go.tmpl for source file
package superschema

import (
"context"

schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"
)

var _ Attribute = MapAttribute{}

type MapAttribute struct {
Common *schemaR.MapAttribute
Resource *schemaR.MapAttribute
DataSource *schemaD.MapAttribute
}

// IsResource returns true if the attribute is a resource attribute.
func (s MapAttribute) IsResource() bool {
return s.Resource != nil || s.Common != nil
}

// IsDataSource returns true if the attribute is a data source attribute.
func (s MapAttribute) IsDataSource() bool {
return s.DataSource != nil || s.Common != nil
}

//nolint:dupl
func (s MapAttribute) GetResource(ctx context.Context) schemaR.Attribute {
var (
common schemaR.MapAttribute
resource schemaR.MapAttribute
)

if s.Common != nil {
common = *s.Common
}

if s.Resource != nil {
resource = *s.Resource
}

a := schemaR.MapAttribute{
Required: computeIsRequired(common, resource),
Optional: computeIsOptional(common, resource),
Computed: computeIsComputed(common, resource),
Sensitive: computeIsSensitive(common, resource),
MarkdownDescription: computeMarkdownDescription(common, resource),
Description: computeDescription(common, resource),
DeprecationMessage: computeDeprecationMessage(common, resource),
ElementType: common.ElementType,
}

if s.Resource != nil {
if s.Resource.ElementType != nil {
a.ElementType = s.Resource.ElementType
}
}

a.Validators = append(a.Validators, common.Validators...)
a.Validators = append(a.Validators, resource.Validators...)
a.PlanModifiers = append(a.PlanModifiers, common.PlanModifiers...)
a.PlanModifiers = append(a.PlanModifiers, resource.PlanModifiers...)

defaultVDescription := ""
if s.Resource != nil {
if s.Resource.Default != nil {
a.Default = s.Resource.Default
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
}
}

a.MarkdownDescription = genResourceAttrDescription(ctx, a.MarkdownDescription, defaultVDescription, a.Validators, a.PlanModifiers)
return a
}

//nolint:dupl
func (s MapAttribute) GetDataSource(ctx context.Context) schemaD.Attribute {
var (
common schemaR.MapAttribute
dataSource schemaD.MapAttribute
)

if s.Common != nil {
common = *s.Common
}

if s.DataSource != nil {
dataSource = *s.DataSource
}

a := schemaD.MapAttribute{
Required: computeIsRequired(common, dataSource),
Optional: computeIsOptional(common, dataSource),
Computed: computeIsComputed(common, dataSource),
Sensitive: computeIsSensitive(common, dataSource),
MarkdownDescription: computeMarkdownDescription(common, dataSource),
Description: computeDescription(common, dataSource),
DeprecationMessage: computeDeprecationMessage(common, dataSource),
ElementType: common.ElementType,
}

if s.DataSource != nil {
if s.DataSource.ElementType != nil {
a.ElementType = s.DataSource.ElementType
}
}

a.Validators = append(a.Validators, common.Validators...)
a.Validators = append(a.Validators, dataSource.Validators...)

a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, a.Validators)

return a
}
109 changes: 109 additions & 0 deletions map_nested_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// code generated by go generate - look at type_attribute.go.tmpl for source file
package superschema

import (
"context"

schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"
)

var _ Attribute = MapNestedAttribute{}

type MapNestedAttribute struct {
Common *schemaR.MapNestedAttribute
Resource *schemaR.MapNestedAttribute
DataSource *schemaD.MapNestedAttribute
Attributes Attributes
}

// IsResource returns true if the attribute is a resource attribute.
func (s MapNestedAttribute) IsResource() bool {
return s.Resource != nil || s.Common != nil
}

// IsDataSource returns true if the attribute is a data source attribute.
func (s MapNestedAttribute) IsDataSource() bool {
return s.DataSource != nil || s.Common != nil
}

//nolint:dupl
func (s MapNestedAttribute) GetResource(ctx context.Context) schemaR.Attribute {
var (
common schemaR.MapNestedAttribute
resource schemaR.MapNestedAttribute
)

if s.Common != nil {
common = *s.Common
}

if s.Resource != nil {
resource = *s.Resource
}

a := schemaR.MapNestedAttribute{
Required: computeIsRequired(common, resource),
Optional: computeIsOptional(common, resource),
Computed: computeIsComputed(common, resource),
Sensitive: computeIsSensitive(common, resource),
MarkdownDescription: computeMarkdownDescription(common, resource),
Description: computeDescription(common, resource),
DeprecationMessage: computeDeprecationMessage(common, resource),
NestedObject: schemaR.NestedAttributeObject{
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
},
}

a.Validators = append(a.Validators, common.Validators...)
a.Validators = append(a.Validators, resource.Validators...)
a.PlanModifiers = append(a.PlanModifiers, common.PlanModifiers...)
a.PlanModifiers = append(a.PlanModifiers, resource.PlanModifiers...)

defaultVDescription := ""
if s.Resource != nil {
if s.Resource.Default != nil {
a.Default = s.Resource.Default
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
}
}

a.MarkdownDescription = genResourceAttrDescription(ctx, a.MarkdownDescription, defaultVDescription, a.Validators, a.PlanModifiers)
return a
}

//nolint:dupl
func (s MapNestedAttribute) GetDataSource(ctx context.Context) schemaD.Attribute {
var (
common schemaR.MapNestedAttribute
dataSource schemaD.MapNestedAttribute
)

if s.Common != nil {
common = *s.Common
}

if s.DataSource != nil {
dataSource = *s.DataSource
}

a := schemaD.MapNestedAttribute{
Required: computeIsRequired(common, dataSource),
Optional: computeIsOptional(common, dataSource),
Computed: computeIsComputed(common, dataSource),
Sensitive: computeIsSensitive(common, dataSource),
MarkdownDescription: computeMarkdownDescription(common, dataSource),
Description: computeDescription(common, dataSource),
DeprecationMessage: computeDeprecationMessage(common, dataSource),
NestedObject: schemaD.NestedAttributeObject{
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
},
}

a.Validators = append(a.Validators, common.Validators...)
a.Validators = append(a.Validators, dataSource.Validators...)

a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, a.Validators)

return a
}
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var templateTypeAttribute string

func main() {
fmt.Println("generating types files...")
tA := []string{"string", "bool", "float64", "int64", "list", "list_nested", "object", "set", "set_nested", "number", "single_nested"}
tA := []string{"string", "bool", "float64", "int64", "list", "list_nested", "object", "single_nested", "set", "set_nested", "number", "map", "map_nested"}

for _, t := range tA {
infos := templateInfos{
Expand Down
14 changes: 7 additions & 7 deletions type_attribute.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type {{ .TypeName }}Attribute struct {
Common *schemaR.{{ .TypeName }}Attribute
Resource *schemaR.{{ .TypeName }}Attribute
DataSource *schemaD.{{ .TypeName }}Attribute
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "SingleNested") }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "SingleNested") (eq .TypeName "MapNested") }}
Attributes Attributes
{{- end }}
}
Expand Down Expand Up @@ -52,10 +52,10 @@ func (s {{ .TypeName }}Attribute) GetResource(ctx context.Context) schemaR.Attri
MarkdownDescription: computeMarkdownDescription(common, resource),
Description: computeDescription(common, resource),
DeprecationMessage: computeDeprecationMessage(common, resource),
{{- if or (eq .TypeName "List") (eq .TypeName "Set") }}
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}
ElementType: common.ElementType,
{{- end }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "MapNested") }}
NestedObject: schemaR.NestedAttributeObject{
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
},
Expand All @@ -65,7 +65,7 @@ func (s {{ .TypeName }}Attribute) GetResource(ctx context.Context) schemaR.Attri
{{- end }}
}

{{- if or (eq .TypeName "List") (eq .TypeName "Set") }}
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}

if s.Resource != nil {
if s.Resource.ElementType != nil {
Expand Down Expand Up @@ -114,10 +114,10 @@ func (s {{ .TypeName }}Attribute) GetDataSource(ctx context.Context) schemaD.Att
MarkdownDescription: computeMarkdownDescription(common, dataSource),
Description: computeDescription(common, dataSource),
DeprecationMessage: computeDeprecationMessage(common, dataSource),
{{- if or (eq .TypeName "List") (eq .TypeName "Set") }}
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}
ElementType: common.ElementType,
{{- end }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "MapNested") }}
NestedObject: schemaD.NestedAttributeObject{
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
},
Expand All @@ -127,7 +127,7 @@ func (s {{ .TypeName }}Attribute) GetDataSource(ctx context.Context) schemaD.Att
{{- end }}
}

{{- if or (eq .TypeName "List") (eq .TypeName "Set") }}
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}

if s.DataSource != nil {
if s.DataSource.ElementType != nil {
Expand Down

0 comments on commit f84e65e

Please sign in to comment.