Skip to content

Commit

Permalink
fix: improve dynamic block schema merging logic to not modify in place
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Jun 5, 2024
1 parent 9dbd8b9 commit a1758a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 7 additions & 6 deletions decoder/internal/schemahelper/block_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,24 @@ func MergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
}
for bType, block := range depSchema.Blocks {
if _, exists := mergedSchema.Blocks[bType]; !exists {
b := block.Copy()
// propagate DynamicBlocks extension to any nested blocks
if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks {
if block.Body.Extensions == nil {
block.Body.Extensions = &schema.BodyExtensions{}
if b.Body.Extensions == nil {
b.Body.Extensions = &schema.BodyExtensions{}
}
block.Body.Extensions.DynamicBlocks = true
b.Body.Extensions.DynamicBlocks = true
}

mergedSchema.Blocks[bType] = block
mergedSchema.Blocks[bType] = b
} else {
// Skip duplicate block type
continue
}
}

if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks && len(depSchema.Blocks) > 0 {
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(depSchema)
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(depSchema, mergedSchema)
}

mergedSchema.TargetableAs = append(mergedSchema.TargetableAs, depSchema.TargetableAs...)
Expand Down Expand Up @@ -87,7 +88,7 @@ func MergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
}
}

mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(mergedSchema)
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(mergedSchema, mergedSchema)
}

return mergedSchema, result
Expand Down
9 changes: 7 additions & 2 deletions decoder/internal/schemahelper/dynamic_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import (
"github.com/zclconf/go-cty/cty"
)

func buildDynamicBlockSchema(inputSchema *schema.BodySchema) *schema.BlockSchema {
// buildDynamicBlockSchema
// inputSchema schema used to figure out which blocks can be dynamic (commonly the dependent schema in order to NOT include static blocks like e.g. lifecycle)
// sourceSchema schema used to copy the body of the block (commonly the merged schema in order to include changes that were made to the schema)
func buildDynamicBlockSchema(inputSchema *schema.BodySchema, sourceSchema *schema.BodySchema) *schema.BlockSchema {
dependentBody := make(map[schema.SchemaKey]*schema.BodySchema)
for blockName, block := range inputSchema.Blocks {
for blockName := range inputSchema.Blocks {
block := sourceSchema.Blocks[blockName]

dependentBody[schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: blockName},
Expand Down

0 comments on commit a1758a4

Please sign in to comment.