summaryrefslogtreecommitdiff
path: root/hcldec
diff options
context:
space:
mode:
authorMartin Atkins <mart@degeneration.co.uk>2018-05-24 12:11:53 -0700
committerMartin Atkins <mart@degeneration.co.uk>2018-05-24 12:11:53 -0700
commit36446359d27574bf110611001414da561731b62d (patch)
treeef619c655c3a904938e4e3aa3cc048e488bf624d /hcldec
parent81d22773002de532651a19acb91b4e1fe2b44cb2 (diff)
hcldec: Variables must visit deeply-nested specifications
Previously this implementation was doing only one level of recursion in its walk, which gave the appearance of working until the transform/container-type specs (DefaultSpec, TransformSpec, ...) were introduced, creating the possibility of "same body children" being more than one level away from the initial spec. It's still correct to only process the schema and content once, because ImpliedSchema is already collecting all of the requirements from the "same body children", and so our content object will include everything that the nested specs should need to analyze needed variables.
Diffstat (limited to 'hcldec')
-rw-r--r--hcldec/variables.go12
-rw-r--r--hcldec/variables_test.go33
2 files changed, 39 insertions, 6 deletions
diff --git a/hcldec/variables.go b/hcldec/variables.go
index 427b0d0..7662516 100644
--- a/hcldec/variables.go
+++ b/hcldec/variables.go
@@ -15,20 +15,22 @@ import (
// 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)
- var vars []hcl.Traversal
-
if vs, ok := spec.(specNeedingVariables); ok {
vars = append(vars, vs.variablesNeeded(content)...)
}
- spec.visitSameBodyChildren(func(s Spec) {
+
+ 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
}
diff --git a/hcldec/variables_test.go b/hcldec/variables_test.go
index ee1eb18..5869e8d 100644
--- a/hcldec/variables_test.go
+++ b/hcldec/variables_test.go
@@ -5,8 +5,8 @@ import (
"reflect"
"testing"
- "github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/hcl2/hcl"
+ "github.com/hashicorp/hcl2/hcl/hclsyntax"
)
func TestVariables(t *testing.T) {
@@ -43,6 +43,37 @@ func TestVariables(t *testing.T) {
},
},
{
+ "a = foo\nb = bar\n",
+ &DefaultSpec{
+ Primary: &AttrSpec{
+ Name: "a",
+ },
+ Default: &AttrSpec{
+ Name: "b",
+ },
+ },
+ []hcl.Traversal{
+ {
+ hcl.TraverseRoot{
+ Name: "foo",
+ SrcRange: hcl.Range{
+ Start: hcl.Pos{Line: 1, Column: 5, Byte: 4},
+ End: hcl.Pos{Line: 1, Column: 8, Byte: 7},
+ },
+ },
+ },
+ {
+ hcl.TraverseRoot{
+ Name: "bar",
+ SrcRange: hcl.Range{
+ Start: hcl.Pos{Line: 2, Column: 5, Byte: 12},
+ End: hcl.Pos{Line: 2, Column: 8, Byte: 15},
+ },
+ },
+ },
+ },
+ },
+ {
"a = foo\n",
&ObjectSpec{
"a": &AttrSpec{