File: variables.go

package info (click to toggle)
golang-github-hashicorp-hcl-v2 2.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,120 kB
  • sloc: ruby: 205; makefile: 72; python: 43; sh: 11
file content (36 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package hcldec

import (
	"github.com/hashicorp/hcl/v2"
)

// 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 scope is insufficient.
//
// If the given body is not compliant with the given schema, the result may
// be incomplete, but that's assumed to be okay because the eventual call
// to Decode will produce error diagnostics anyway.
func Variables(body hcl.Body, spec Spec) []hcl.Traversal {
	var vars []hcl.Traversal
	schema := ImpliedSchema(spec)
	content, _, _ := body.PartialContent(schema)

	if vs, ok := spec.(specNeedingVariables); ok {
		vars = append(vars, vs.variablesNeeded(content)...)
	}

	var visitFn visitFunc
	visitFn = func(s Spec) {
		if vs, ok := s.(specNeedingVariables); ok {
			vars = append(vars, vs.variablesNeeded(content)...)
		}
		s.visitSameBodyChildren(visitFn)
	}
	spec.visitSameBodyChildren(visitFn)

	return vars
}