Skip to content

Commit

Permalink
feat: allow overriding existing blocks and attributes for dependent b…
Browse files Browse the repository at this point in the history
…ody schemas

We couldn't find any cases where the existing tests (and some example codebase) would access the else statement while testing. However, this change could allow e.g. Terraform providers to override for example the lifecycle block if they define such a block in their schema. We have yet to see such cases though :)
  • Loading branch information
ansgarm committed Jul 18, 2024
1 parent 30b21ca commit 7ddc33a
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions decoder/internal/schemahelper/block_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,18 @@ func MergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*
depSchema, _, result := NewBlockSchema(blockSchema).DependentBodySchema(block)
if result == LookupSuccessful || result == LookupPartiallySuccessful {
for name, attr := range depSchema.Attributes {
if _, exists := mergedSchema.Attributes[name]; !exists {
mergedSchema.Attributes[name] = attr
} else {
// Skip duplicate attribute
continue
}
mergedSchema.Attributes[name] = attr
}
for bType, block := range depSchema.Blocks {
if _, exists := mergedSchema.Blocks[bType]; !exists {
copiedBlock := block.Copy()
// propagate DynamicBlocks extension to any nested blocks
if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks {
if copiedBlock.Body.Extensions == nil {
copiedBlock.Body.Extensions = &schema.BodyExtensions{}
}
copiedBlock.Body.Extensions.DynamicBlocks = true
copiedBlock := block.Copy()
// propagate DynamicBlocks extension to any nested blocks
if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks {
if copiedBlock.Body.Extensions == nil {
copiedBlock.Body.Extensions = &schema.BodyExtensions{}
}

mergedSchema.Blocks[bType] = copiedBlock
} else {
// Skip duplicate block type
continue
copiedBlock.Body.Extensions.DynamicBlocks = true
}
mergedSchema.Blocks[bType] = copiedBlock
}

if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks && len(depSchema.Blocks) > 0 {
Expand Down

0 comments on commit 7ddc33a

Please sign in to comment.