Variables processes the given body with the given spec and returns a list of the variable traversals that would be required to decode the same pairing of body and spec. This can be used to conditionally populate the variables in the EvalContext passed to Decode, for applications where a static scop
(body hcl.Body, spec Spec)
| 18 | // be incomplete, but that's assumed to be okay because the eventual call |
| 19 | // to Decode will produce error diagnostics anyway. |
| 20 | func Variables(body hcl.Body, spec Spec) []hcl.Traversal { |
| 21 | var vars []hcl.Traversal |
| 22 | schema := ImpliedSchema(spec) |
| 23 | content, _, _ := body.PartialContent(schema) |
| 24 | |
| 25 | if vs, ok := spec.(specNeedingVariables); ok { |
| 26 | vars = append(vars, vs.variablesNeeded(content)...) |
| 27 | } |
| 28 | |
| 29 | var visitFn visitFunc |
| 30 | visitFn = func(s Spec) { |
| 31 | if vs, ok := s.(specNeedingVariables); ok { |
| 32 | vars = append(vars, vs.variablesNeeded(content)...) |
| 33 | } |
| 34 | s.visitSameBodyChildren(visitFn) |
| 35 | } |
| 36 | spec.visitSameBodyChildren(visitFn) |
| 37 | |
| 38 | return vars |
| 39 | } |