f9f92da699
Our API previously had a function only for retrieving the variables used in the for_each and labels arguments used during an Expand call, and expected callers to then interrogate the resulting expanded block to find the other variables required to fully decode the content. That approach is insufficient for any application that needs to know the full set of required variables before any evaluation begins, such as when a dependency graph will be constructed to allow a topological traversal through blocks while evaluating. Now we have WalkVariables, which finds both the variables used to expand _and_ the variables within any blocks. This also renames WalkForEachVariables to WalkExpandVariables since that name is more accurate with the addition of the "label" argument into the expand-time dependency set. There is also a hcldec-based helper wrapper for each of those, allowing single-shot analysis of blocks for applications that use hcldec. This is a breaking change to the dynblock package API, because the old WalkForEachVariables and ForEachVariablesHCLDec functions are no longer present.
44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package dynblock
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/hashicorp/hcl2/hcldec"
|
|
)
|
|
|
|
// VariablesHCLDec is a wrapper around WalkVariables that uses the given hcldec
|
|
// specification to automatically drive the recursive walk through nested
|
|
// blocks in the given body.
|
|
//
|
|
// This is a drop-in replacement for hcldec.Variables which is able to treat
|
|
// blocks of type "dynamic" in the same special way that dynblock.Expand would,
|
|
// exposing both the variables referenced in the "for_each" and "labels"
|
|
// arguments and variables used in the nested "content" block.
|
|
func VariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal {
|
|
rootNode := WalkVariables(body)
|
|
return walkVariablesWithHCLDec(rootNode, spec)
|
|
}
|
|
|
|
// ExpandVariablesHCLDec is like VariablesHCLDec but it includes only the
|
|
// minimal set of variables required to call Expand, ignoring variables that
|
|
// are referenced only inside normal block contents. See WalkExpandVariables
|
|
// for more information.
|
|
func ExpandVariablesHCLDec(body hcl.Body, spec hcldec.Spec) []hcl.Traversal {
|
|
rootNode := WalkExpandVariables(body)
|
|
return walkVariablesWithHCLDec(rootNode, spec)
|
|
}
|
|
|
|
func walkVariablesWithHCLDec(node WalkVariablesNode, spec hcldec.Spec) []hcl.Traversal {
|
|
vars, children := node.Visit(hcldec.ImpliedSchema(spec))
|
|
|
|
if len(children) > 0 {
|
|
childSpecs := hcldec.ChildBlockTypes(spec)
|
|
for _, child := range children {
|
|
if childSpec, exists := childSpecs[child.BlockTypeName]; exists {
|
|
vars = append(vars, walkVariablesWithHCLDec(child.Node, childSpec)...)
|
|
}
|
|
}
|
|
}
|
|
|
|
return vars
|
|
}
|