From 4e18e3a8a8fcd9a1fbaa8e7e2022c314701c3ace Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 16 Jun 2017 07:15:14 -0700 Subject: [PATCH] zcl: allow nil Variables in EvalContext it's acceptable to have "Variables" set to nil in an EvalContext, if a particular scope has no variables at all. If _no_ contexts in the chain have non-nil variables, this is considered to mean that variables are not allowed at all, which produces a different error message. --- zcl/traversal.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/zcl/traversal.go b/zcl/traversal.go index 474420d..0a247da 100644 --- a/zcl/traversal.go +++ b/zcl/traversal.go @@ -68,7 +68,22 @@ func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) { root := split.Abs[0].(TraverseRoot) name := root.Name - if ctx == nil || ctx.Variables == nil { + thisCtx := ctx + hasNonNil := false + for thisCtx != nil { + if thisCtx.Variables == nil { + thisCtx = thisCtx.parent + continue + } + hasNonNil = true + val, exists := thisCtx.Variables[name] + if exists { + return split.Rel.TraverseRel(val) + } + thisCtx = thisCtx.parent + } + + if !hasNonNil { return cty.DynamicVal, Diagnostics{ { Severity: DiagError, @@ -79,15 +94,6 @@ func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) { } } - thisCtx := ctx - for thisCtx != nil { - val, exists := thisCtx.Variables[name] - if exists { - return split.Rel.TraverseRel(val) - } - thisCtx = thisCtx.parent - } - suggestions := make([]string, 0, len(ctx.Variables)) thisCtx = ctx for thisCtx != nil {