diff options
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcldec/variables.go')
-rw-r--r-- | vendor/github.com/hashicorp/hcl2/hcldec/variables.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcldec/variables.go b/vendor/github.com/hashicorp/hcl2/hcldec/variables.go new file mode 100644 index 0000000..427b0d0 --- /dev/null +++ b/vendor/github.com/hashicorp/hcl2/hcldec/variables.go | |||
@@ -0,0 +1,34 @@ | |||
1 | package hcldec | ||
2 | |||
3 | import ( | ||
4 | "github.com/hashicorp/hcl2/hcl" | ||
5 | ) | ||
6 | |||
7 | // Variables processes the given body with the given spec and returns a | ||
8 | // list of the variable traversals that would be required to decode | ||
9 | // the same pairing of body and spec. | ||
10 | // | ||
11 | // This can be used to conditionally populate the variables in the EvalContext | ||
12 | // passed to Decode, for applications where a static scope is insufficient. | ||
13 | // | ||
14 | // If the given body is not compliant with the given schema, the result may | ||
15 | // be incomplete, but that's assumed to be okay because the eventual call | ||
16 | // to Decode will produce error diagnostics anyway. | ||
17 | func Variables(body hcl.Body, spec Spec) []hcl.Traversal { | ||
18 | schema := ImpliedSchema(spec) | ||
19 | |||
20 | content, _, _ := body.PartialContent(schema) | ||
21 | |||
22 | var vars []hcl.Traversal | ||
23 | |||
24 | if vs, ok := spec.(specNeedingVariables); ok { | ||
25 | vars = append(vars, vs.variablesNeeded(content)...) | ||
26 | } | ||
27 | spec.visitSameBodyChildren(func(s Spec) { | ||
28 | if vs, ok := s.(specNeedingVariables); ok { | ||
29 | vars = append(vars, vs.variablesNeeded(content)...) | ||
30 | } | ||
31 | }) | ||
32 | |||
33 | return vars | ||
34 | } | ||