6631d7cd0a
Previously we were incorrectly passing down the original forEachCtx down to nested child blocks for recursive expansion. Instead, we must use the iteration-specific constructed EvalContext, which then allows any nested dynamic blocks to use the parent's iterator variable in their for_each or labels expressions, and thus unpack nested data structures into corresponding nested block structures: dynamic "parent" { for_each = [["a", "b"], []] content { dynamic "child" { for_each = parent.value content {} } } }
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package dynblock
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
type iteration struct {
|
|
IteratorName string
|
|
Key cty.Value
|
|
Value cty.Value
|
|
Inherited map[string]*iteration
|
|
}
|
|
|
|
func (s *expandSpec) MakeIteration(key, value cty.Value) *iteration {
|
|
return &iteration{
|
|
IteratorName: s.iteratorName,
|
|
Key: key,
|
|
Value: value,
|
|
Inherited: s.inherited,
|
|
}
|
|
}
|
|
|
|
func (i *iteration) Object() cty.Value {
|
|
return cty.ObjectVal(map[string]cty.Value{
|
|
"key": i.Key,
|
|
"value": i.Value,
|
|
})
|
|
}
|
|
|
|
func (i *iteration) EvalContext(base *hcl.EvalContext) *hcl.EvalContext {
|
|
new := base.NewChild()
|
|
|
|
if i != nil {
|
|
new.Variables = map[string]cty.Value{}
|
|
for name, otherIt := range i.Inherited {
|
|
new.Variables[name] = otherIt.Object()
|
|
}
|
|
new.Variables[i.IteratorName] = i.Object()
|
|
}
|
|
|
|
return new
|
|
}
|
|
|
|
func (i *iteration) MakeChild(iteratorName string, key, value cty.Value) *iteration {
|
|
if i == nil {
|
|
// Create entirely new root iteration, then
|
|
return &iteration{
|
|
IteratorName: iteratorName,
|
|
Key: key,
|
|
Value: value,
|
|
}
|
|
}
|
|
|
|
inherited := map[string]*iteration{}
|
|
for name, otherIt := range i.Inherited {
|
|
inherited[name] = otherIt
|
|
}
|
|
inherited[i.IteratorName] = i
|
|
return &iteration{
|
|
IteratorName: iteratorName,
|
|
Key: key,
|
|
Value: value,
|
|
Inherited: inherited,
|
|
}
|
|
}
|