Skip to content
Draft
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
1 change: 1 addition & 0 deletions internal/convert/plan_modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
PlanModifierTypeBool PlanModifierType = "Bool"
PlanModifierTypeDynamic PlanModifierType = "Dynamic"
PlanModifierTypeFloat64 PlanModifierType = "Float64"
PlanModifierTypeInt64 PlanModifierType = "Int64"
PlanModifierTypeList PlanModifierType = "List"
Expand Down
1 change: 1 addition & 0 deletions internal/convert/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
ValidatorTypeBool ValidatorType = "Bool"
ValidatorTypeDynamic ValidatorType = "Dynamic"
ValidatorTypeFloat64 ValidatorType = "Float64"
ValidatorTypeInt64 ValidatorType = "Int64"
ValidatorTypeList ValidatorType = "List"
Expand Down
2 changes: 2 additions & 0 deletions internal/datasource/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func NewAttribute(a datasource.Attribute) (generatorschema.GeneratorAttribute, e
switch {
case a.Bool != nil:
return NewGeneratorBoolAttribute(a.Name, a.Bool)
case a.Dynamic != nil:
return NewGeneratorDynamicAttribute(a.Name, a.Dynamic)
case a.Float64 != nil:
return NewGeneratorFloat64Attribute(a.Name, a.Float64)
case a.Int64 != nil:
Expand Down
156 changes: 156 additions & 0 deletions internal/datasource/dynamic_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package datasource

import (
"bytes"
"fmt"

"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"

"github.com/starburstdata/terraform-plugin-codegen-framework/internal/convert"
"github.com/starburstdata/terraform-plugin-codegen-framework/internal/model"
"github.com/starburstdata/terraform-plugin-codegen-framework/internal/schema"
)

type GeneratorDynamicAttribute struct {
AssociatedExternalType *schema.AssocExtType
ComputedOptionalRequired convert.ComputedOptionalRequired
CustomType convert.CustomTypePrimitive
DeprecationMessage convert.DeprecationMessage
Description convert.Description
Sensitive convert.Sensitive
Validators convert.Validators
}

func NewGeneratorDynamicAttribute(name string, a *datasource.DynamicAttribute) (GeneratorDynamicAttribute, error) {
if a == nil {
return GeneratorDynamicAttribute{}, fmt.Errorf("*datasource.DynamicAttribute is nil")
}

c := convert.NewComputedOptionalRequired(a.ComputedOptionalRequired)

ctp := convert.NewCustomTypePrimitive(a.CustomType, a.AssociatedExternalType, name)

d := convert.NewDescription(a.Description)

dm := convert.NewDeprecationMessage(a.DeprecationMessage)

s := convert.NewSensitive(a.Sensitive)

v := convert.NewValidators(convert.ValidatorTypeDynamic, a.Validators.CustomValidators())

return GeneratorDynamicAttribute{
AssociatedExternalType: schema.NewAssocExtType(a.AssociatedExternalType),
ComputedOptionalRequired: c,
CustomType: ctp,
DeprecationMessage: dm,
Description: d,
Sensitive: s,
Validators: v,
}, nil
}

func (g GeneratorDynamicAttribute) GeneratorSchemaType() schema.Type {
return schema.GeneratorDynamicAttribute
}

func (g GeneratorDynamicAttribute) Imports() *schema.Imports {
imports := schema.NewImports()

imports.Append(g.CustomType.Imports())

imports.Append(g.Validators.Imports())

if g.AssociatedExternalType != nil {
imports.Append(schema.AssociatedExternalTypeImports())
}

imports.Append(g.AssociatedExternalType.Imports())

return imports
}

func (g GeneratorDynamicAttribute) Equal(ga schema.GeneratorAttribute) bool {
h, ok := ga.(GeneratorDynamicAttribute)

if !ok {
return false
}

if !g.AssociatedExternalType.Equal(h.AssociatedExternalType) {
return false
}

if !g.ComputedOptionalRequired.Equal(h.ComputedOptionalRequired) {
return false
}

if !g.CustomType.Equal(h.CustomType) {
return false
}

if !g.DeprecationMessage.Equal(h.DeprecationMessage) {
return false
}

if !g.Description.Equal(h.Description) {
return false
}

if !g.Sensitive.Equal(h.Sensitive) {
return false
}

return g.Validators.Equal(h.Validators)
}

func (g GeneratorDynamicAttribute) Schema(name schema.FrameworkIdentifier) (string, error) {
var b bytes.Buffer

b.WriteString(fmt.Sprintf("%q: schema.DynamicAttribute{\n", name))
b.Write(g.CustomType.Schema())
b.Write(g.ComputedOptionalRequired.Schema())
b.Write(g.Sensitive.Schema())
b.Write(g.Description.Schema())
b.Write(g.DeprecationMessage.Schema())
b.Write(g.Validators.Schema())
b.WriteString("},")

return b.String(), nil
}

func (g GeneratorDynamicAttribute) ModelField(name schema.FrameworkIdentifier) (model.Field, error) {
field := model.Field{
Name: name.ToPascalCase(),
TfsdkName: name.ToString(),
ValueType: model.DynamicValueType,
}

customValueType := g.CustomType.ValueType()

if customValueType != "" {
field.ValueType = customValueType
}

return field, nil
}

// AttrType returns a string representation of a basetypes.DynamicTypable type.
func (g GeneratorDynamicAttribute) AttrType(name schema.FrameworkIdentifier) (string, error) {
if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sType{}", name.ToPascalCase()), nil
}

return "basetypes.DynamicType{}", nil
}

// AttrValue returns a string representation of a basetypes.DynamicValuable type.
func (g GeneratorDynamicAttribute) AttrValue(name schema.FrameworkIdentifier) string {
if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sValue", name.ToPascalCase())
}

return "basetypes.DynamicValue"
}
1 change: 1 addition & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

const (
BoolValueType = "types.Bool"
DynamicValueType = "types.Dynamic"
Float64ValueType = "types.Float64"
Int64ValueType = "types.Int64"
ListValueType = "types.List"
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func NewAttribute(a provider.Attribute) (generatorschema.GeneratorAttribute, err
switch {
case a.Bool != nil:
return NewGeneratorBoolAttribute(a.Name, a.Bool)
case a.Dynamic != nil:
return NewGeneratorDynamicAttribute(a.Name, a.Dynamic)
case a.Float64 != nil:
return NewGeneratorFloat64Attribute(a.Name, a.Float64)
case a.Int64 != nil:
Expand Down
156 changes: 156 additions & 0 deletions internal/provider/dynamic_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"bytes"
"fmt"

"github.com/hashicorp/terraform-plugin-codegen-spec/provider"

"github.com/starburstdata/terraform-plugin-codegen-framework/internal/convert"
"github.com/starburstdata/terraform-plugin-codegen-framework/internal/model"
"github.com/starburstdata/terraform-plugin-codegen-framework/internal/schema"
)

type GeneratorDynamicAttribute struct {
AssociatedExternalType *schema.AssocExtType
OptionalRequired convert.OptionalRequired
CustomType convert.CustomTypePrimitive
DeprecationMessage convert.DeprecationMessage
Description convert.Description
Sensitive convert.Sensitive
Validators convert.Validators
}

func NewGeneratorDynamicAttribute(name string, a *provider.DynamicAttribute) (GeneratorDynamicAttribute, error) {
if a == nil {
return GeneratorDynamicAttribute{}, fmt.Errorf("*provider.DynamicAttribute is nil")
}

c := convert.NewOptionalRequired(a.OptionalRequired)

ctp := convert.NewCustomTypePrimitive(a.CustomType, a.AssociatedExternalType, name)

d := convert.NewDescription(a.Description)

dm := convert.NewDeprecationMessage(a.DeprecationMessage)

s := convert.NewSensitive(a.Sensitive)

v := convert.NewValidators(convert.ValidatorTypeDynamic, a.Validators.CustomValidators())

return GeneratorDynamicAttribute{
AssociatedExternalType: schema.NewAssocExtType(a.AssociatedExternalType),
OptionalRequired: c,
CustomType: ctp,
DeprecationMessage: dm,
Description: d,
Sensitive: s,
Validators: v,
}, nil
}

func (g GeneratorDynamicAttribute) GeneratorSchemaType() schema.Type {
return schema.GeneratorDynamicAttribute
}

func (g GeneratorDynamicAttribute) Imports() *schema.Imports {
imports := schema.NewImports()

imports.Append(g.CustomType.Imports())

imports.Append(g.Validators.Imports())

if g.AssociatedExternalType != nil {
imports.Append(schema.AssociatedExternalTypeImports())
}

imports.Append(g.AssociatedExternalType.Imports())

return imports
}

func (g GeneratorDynamicAttribute) Equal(ga schema.GeneratorAttribute) bool {
h, ok := ga.(GeneratorDynamicAttribute)

if !ok {
return false
}

if !g.AssociatedExternalType.Equal(h.AssociatedExternalType) {
return false
}

if !g.OptionalRequired.Equal(h.OptionalRequired) {
return false
}

if !g.CustomType.Equal(h.CustomType) {
return false
}

if !g.DeprecationMessage.Equal(h.DeprecationMessage) {
return false
}

if !g.Description.Equal(h.Description) {
return false
}

if !g.Sensitive.Equal(h.Sensitive) {
return false
}

return g.Validators.Equal(h.Validators)
}

func (g GeneratorDynamicAttribute) Schema(name schema.FrameworkIdentifier) (string, error) {
var b bytes.Buffer

b.WriteString(fmt.Sprintf("%q: schema.DynamicAttribute{\n", name))
b.Write(g.CustomType.Schema())
b.Write(g.OptionalRequired.Schema())
b.Write(g.Sensitive.Schema())
b.Write(g.Description.Schema())
b.Write(g.DeprecationMessage.Schema())
b.Write(g.Validators.Schema())
b.WriteString("},")

return b.String(), nil
}

func (g GeneratorDynamicAttribute) ModelField(name schema.FrameworkIdentifier) (model.Field, error) {
field := model.Field{
Name: name.ToPascalCase(),
TfsdkName: name.ToString(),
ValueType: model.DynamicValueType,
}

customValueType := g.CustomType.ValueType()

if customValueType != "" {
field.ValueType = customValueType
}

return field, nil
}

// AttrType returns a string representation of a basetypes.DynamicTypable type.
func (g GeneratorDynamicAttribute) AttrType(name schema.FrameworkIdentifier) (string, error) {
if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sType{}", name.ToPascalCase()), nil
}

return "basetypes.DynamicType{}", nil
}

// AttrValue returns a string representation of a basetypes.DynamicValuable type.
func (g GeneratorDynamicAttribute) AttrValue(name schema.FrameworkIdentifier) string {
if g.AssociatedExternalType != nil {
return fmt.Sprintf("%sValue", name.ToPascalCase())
}

return "basetypes.DynamicValue"
}
2 changes: 2 additions & 0 deletions internal/resource/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func NewAttribute(a resource.Attribute) (generatorschema.GeneratorAttribute, err
switch {
case a.Bool != nil:
return NewGeneratorBoolAttribute(a.Name, a.Bool)
case a.Dynamic != nil:
return NewGeneratorDynamicAttribute(a.Name, a.Dynamic)
case a.Float64 != nil:
return NewGeneratorFloat64Attribute(a.Name, a.Float64)
case a.Int64 != nil:
Expand Down
Loading