]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
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 { | |
107c1cdb | 18 | var vars []hcl.Traversal |
15c0b25d | 19 | schema := ImpliedSchema(spec) |
15c0b25d AP |
20 | content, _, _ := body.PartialContent(schema) |
21 | ||
15c0b25d AP |
22 | if vs, ok := spec.(specNeedingVariables); ok { |
23 | vars = append(vars, vs.variablesNeeded(content)...) | |
24 | } | |
107c1cdb ND |
25 | |
26 | var visitFn visitFunc | |
27 | visitFn = func(s Spec) { | |
15c0b25d AP |
28 | if vs, ok := s.(specNeedingVariables); ok { |
29 | vars = append(vars, vs.variablesNeeded(content)...) | |
30 | } | |
107c1cdb ND |
31 | s.visitSameBodyChildren(visitFn) |
32 | } | |
33 | spec.visitSameBodyChildren(visitFn) | |
15c0b25d AP |
34 | |
35 | return vars | |
36 | } |