2018-01-21 23:29:43 +00:00
|
|
|
package dynblock
|
|
|
|
|
|
|
|
import (
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-01-21 23:29:43 +00:00
|
|
|
"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()
|
|
|
|
|
2018-12-20 01:20:50 +00:00
|
|
|
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()
|
2018-01-21 23:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|