Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Dec 3, 2024
1 parent 7ba55dd commit f67b637
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
6 changes: 6 additions & 0 deletions internal/providers/pluginfw/tfschema/attribute_converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tfschema

type BlockToAttributeConverter interface {
// ConvertBlockToAttribute converts a contained block to its corresponding attribute type.
ConvertBlockToAttribute(string) BaseSchemaBuilder
}
30 changes: 2 additions & 28 deletions internal/providers/pluginfw/tfschema/customizable_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,34 +211,8 @@ func (s *CustomizableSchema) ConvertToAttribute(path ...string) *CustomizableSch

cb := func(attr BaseSchemaBuilder) BaseSchemaBuilder {
switch a := attr.(type) {
case ListNestedBlockBuilder:
elem, ok := a.NestedObject.Blocks[field]
if !ok {
panic(fmt.Errorf("field %s does not exist in nested block", field))
}
if a.NestedObject.Attributes == nil {
a.NestedObject.Attributes = make(map[string]AttributeBuilder)
}
a.NestedObject.Attributes[field] = elem.ToAttribute()
delete(a.NestedObject.Blocks, field)
if len(a.NestedObject.Blocks) == 0 {
a.NestedObject.Blocks = nil
}
return a
case SingleNestedBlockBuilder:
elem, ok := a.NestedObject.Blocks[field]
if !ok {
panic(fmt.Errorf("field %s does not exist in nested block", field))
}
if a.NestedObject.Attributes == nil {
a.NestedObject.Attributes = make(map[string]AttributeBuilder)
}
a.NestedObject.Attributes[field] = elem.ToAttribute()
delete(a.NestedObject.Blocks, field)
if len(a.NestedObject.Blocks) == 0 {
a.NestedObject.Blocks = nil
}
return a
case BlockToAttributeConverter:
return a.ConvertBlockToAttribute(field)
default:
panic(fmt.Errorf("ConvertToAttribute called on invalid attribute type: %s. %s", reflect.TypeOf(attr).String(), common.TerraformBugErrorMessage))
}
Expand Down
18 changes: 18 additions & 0 deletions internal/providers/pluginfw/tfschema/list_nested_block.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tfschema

import (
"fmt"

dataschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
Expand Down Expand Up @@ -56,3 +58,19 @@ func (a ListNestedBlockBuilder) AddPlanModifier(v planmodifier.List) BaseSchemaB
a.PlanModifiers = append(a.PlanModifiers, v)
return a
}

func (a ListNestedBlockBuilder) ConvertBlockToAttribute(field string) BaseSchemaBuilder {
elem, ok := a.NestedObject.Blocks[field]
if !ok {
panic(fmt.Errorf("field %s does not exist in nested block", field))
}
if a.NestedObject.Attributes == nil {
a.NestedObject.Attributes = make(map[string]AttributeBuilder)
}
a.NestedObject.Attributes[field] = elem.ToAttribute()
delete(a.NestedObject.Blocks, field)
if len(a.NestedObject.Blocks) == 0 {
a.NestedObject.Blocks = nil
}
return a
}
16 changes: 16 additions & 0 deletions internal/providers/pluginfw/tfschema/single_nested_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ func (a SingleNestedBlockBuilder) AddPlanModifier(v planmodifier.Object) BaseSch
a.PlanModifiers = append(a.PlanModifiers, v)
return a
}

func (a SingleNestedBlockBuilder) ConvertBlockToAttribute(field string) BaseSchemaBuilder {
elem, ok := a.NestedObject.Blocks[field]
if !ok {
panic(fmt.Errorf("field %s does not exist in nested block", field))
}
if a.NestedObject.Attributes == nil {
a.NestedObject.Attributes = make(map[string]AttributeBuilder)
}
a.NestedObject.Attributes[field] = elem.ToAttribute()
delete(a.NestedObject.Blocks, field)
if len(a.NestedObject.Blocks) == 0 {
a.NestedObject.Blocks = nil
}
return a
}

0 comments on commit f67b637

Please sign in to comment.