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

Commit

Permalink
feat: add NestedObjectValueOf
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Oct 31, 2023
1 parent 106c469 commit ae10b07
Show file tree
Hide file tree
Showing 8 changed files with 878 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/FrangipaneTeam/terraform-plugin-framework-superschema
go 1.20

require (
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.2.0
github.com/hashicorp/terraform-plugin-framework v1.4.1
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
github.com/iancoleman/strcase v0.3.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0 h1:QHcnFyAQy/yWcBargdCt8Eprnh7G7vOYOw7z/4LOlM8=
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0/go.mod h1:GGgBv/UI8efmEok0VJWSxp3m6GdkETqzJf5SWH83aDs=
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.2.0 h1:lcJY8AEbpbYp/M/jPUdYZArsZKAkM2LECSYDfKOiIiQ=
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.2.0/go.mod h1:Aux7edspqsudNKr9YFgCkAgUwFj44RyOV123XolOzjY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/terraform-plugin-framework v1.4.1 h1:ZC29MoB3Nbov6axHdgPbMz7799pT5H8kIrM8YAsaVrs=
Expand Down Expand Up @@ -34,8 +34,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
Expand Down
146 changes: 145 additions & 1 deletion superlist_nested_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package superschema
import (
"context"

"github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
supertypes "github.com/FrangipaneTeam/terraform-plugin-framework-supertypes"
schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand Down Expand Up @@ -160,3 +160,147 @@ func (s SuperListNestedAttribute) GetDataSource(ctx context.Context) schemaD.Att
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
return a
}

// * SuperTypeOf

var _ Attribute = SuperListNestedAttributeOf[struct{}]{}

type SuperListNestedAttributeOf[T any] struct {
Deprecated *Deprecated
Common *schemaR.ListNestedAttribute
Resource *schemaR.ListNestedAttribute
DataSource *schemaD.ListNestedAttribute
Attributes Attributes
}

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

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

//nolint:dupl
func (s SuperListNestedAttributeOf[T]) GetResource(ctx context.Context) schemaR.Attribute {
var (
common schemaR.ListNestedAttribute
resource schemaR.ListNestedAttribute
)

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

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

a := schemaR.ListNestedAttribute{
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.Common != nil {
if s.Common.CustomType != nil {
a.CustomType = s.Common.CustomType
}
}

if s.Resource != nil {
if s.Resource.Default != nil {
a.Default = s.Resource.Default
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
}
if s.Resource.CustomType != nil {
a.CustomType = s.Resource.CustomType
}
}
// * If user has not provided a custom type, we will use the default supertypes
if a.CustomType == nil {
a.CustomType = supertypes.NewListNestedObjectTypeOf[T](ctx)
}

deprecationMessage := ""
if s.Deprecated != nil {
a.DeprecationMessage = s.Deprecated.DeprecationMessage
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
}

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

//nolint:dupl
func (s SuperListNestedAttributeOf[T]) GetDataSource(ctx context.Context) schemaD.Attribute {
var (
common schemaR.ListNestedAttribute
dataSource schemaD.ListNestedAttribute
)

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

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

a := schemaD.ListNestedAttribute{
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...)

if s.Common != nil {
if s.Common.CustomType != nil {
a.CustomType = s.Common.CustomType
}
}

if s.DataSource != nil {
if s.DataSource.CustomType != nil {
a.CustomType = s.DataSource.CustomType
}
}
// * If user has not provided a custom type, we will use the default supertypes
if a.CustomType == nil {
a.CustomType = supertypes.NewListNestedObjectTypeOf[T](ctx)
}

deprecationMessage := ""
if s.Deprecated != nil {
a.DeprecationMessage = s.Deprecated.DeprecationMessage
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
}

a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
return a
}
144 changes: 144 additions & 0 deletions supermap_nested_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,147 @@ func (s SuperMapNestedAttribute) GetDataSource(ctx context.Context) schemaD.Attr
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
return a
}

// * SuperTypeOf

var _ Attribute = SuperMapNestedAttributeOf[struct{}]{}

type SuperMapNestedAttributeOf[T any] struct {
Deprecated *Deprecated
Common *schemaR.MapNestedAttribute
Resource *schemaR.MapNestedAttribute
DataSource *schemaD.MapNestedAttribute
Attributes Attributes
}

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

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

//nolint:dupl
func (s SuperMapNestedAttributeOf[T]) 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.Common != nil {
if s.Common.CustomType != nil {
a.CustomType = s.Common.CustomType
}
}

if s.Resource != nil {
if s.Resource.Default != nil {
a.Default = s.Resource.Default
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
}
if s.Resource.CustomType != nil {
a.CustomType = s.Resource.CustomType
}
}
// * If user has not provided a custom type, we will use the default supertypes
if a.CustomType == nil {
a.CustomType = supertypes.MapNestedObjectTypeOf[T]{}.MapType
}

deprecationMessage := ""
if s.Deprecated != nil {
a.DeprecationMessage = s.Deprecated.DeprecationMessage
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
}

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

//nolint:dupl
func (s SuperMapNestedAttributeOf[T]) 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...)

if s.Common != nil {
if s.Common.CustomType != nil {
a.CustomType = s.Common.CustomType
}
}

if s.DataSource != nil {
if s.DataSource.CustomType != nil {
a.CustomType = s.DataSource.CustomType
}
}
// * If user has not provided a custom type, we will use the default supertypes
if a.CustomType == nil {
a.CustomType = supertypes.MapNestedObjectTypeOf[T]{}.MapType
}

deprecationMessage := ""
if s.Deprecated != nil {
a.DeprecationMessage = s.Deprecated.DeprecationMessage
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
}

a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
return a
}
Loading

0 comments on commit ae10b07

Please sign in to comment.