From 5eabde2304d8a05f16c69d98228772f963cf752d Mon Sep 17 00:00:00 2001 From: i4k Date: Thu, 24 Oct 2024 17:54:38 +0100 Subject: [PATCH] chore: use hasUnknowns from partialEval. Signed-off-by: i4k --- hcl/eval/eval.go | 15 +++++++++++---- stdlib/ternary.go | 21 ++------------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/hcl/eval/eval.go b/hcl/eval/eval.go index e58537de0..5835e808e 100644 --- a/hcl/eval/eval.go +++ b/hcl/eval/eval.go @@ -87,8 +87,15 @@ func (c *Context) DeleteNamespace(name string) { // HasNamespace returns true the evaluation context knows this namespace, false otherwise. func (c *Context) HasNamespace(name string) bool { - _, has := c.hclctx.Variables[name] - return has + ctx := c.hclctx + for ctx != nil { + _, has := ctx.Variables[name] + if has { + return true + } + ctx = ctx.Parent() + } + return false } // Eval will evaluate an expression given its context. @@ -121,7 +128,7 @@ func (c *Context) PartialEval(expr hhcl.Expression) (hhcl.Expression, bool, erro // NOTE(i4k): Template*Expr are also kept because we support an special // HEREDOC handling detection and then if evaluated it will be converted // to plain quoted strings. - return newexpr, hasUnknowns, nil + return newexpr, false, nil } var evaluated cty.Value evaluated, err = c.Eval(newexpr) @@ -132,7 +139,7 @@ func (c *Context) PartialEval(expr hhcl.Expression) (hhcl.Expression, bool, erro Val: evaluated, SrcRange: newexpr.Range(), } - return newexpr, hasUnknowns, nil + return newexpr, false, nil } // Copy the eval context. diff --git a/stdlib/ternary.go b/stdlib/ternary.go index e86bcae87..0b926782d 100644 --- a/stdlib/ternary.go +++ b/stdlib/ternary.go @@ -4,7 +4,6 @@ package stdlib import ( - "github.com/terramate-io/hcl/v2" "github.com/terramate-io/hcl/v2/ext/customdecode" "github.com/terramate-io/hcl/v2/hclsyntax" "github.com/terramate-io/terramate/errors" @@ -50,13 +49,12 @@ func evalTernaryBranch(arg cty.Value) (cty.Value, error) { closure := customdecode.ExpressionClosureFromVal(arg) ctx := eval.NewContextFrom(closure.EvalContext) - newexpr, _, err := ctx.PartialEval(closure.Expression.(hclsyntax.Expression)) + newexpr, hasUnknowns, err := ctx.PartialEval(closure.Expression.(hclsyntax.Expression)) if err != nil { return cty.NilVal, errors.E(err, "evaluating tm_ternary branch") } - // TODO(i4k): use our own hasUnknowns here. - if dependsOnUnknowns(newexpr, closure.EvalContext) { + if hasUnknowns { return customdecode.ExpressionVal(newexpr), nil } @@ -67,18 +65,3 @@ func evalTernaryBranch(arg cty.Value) (cty.Value, error) { return v, nil } - -// dependsOnUnknowns returns true if any of the variables that the given -// expression might access are unknown values or contain unknown values. -func dependsOnUnknowns(expr hcl.Expression, ctx *hcl.EvalContext) bool { - for _, traversal := range expr.Variables() { - val, diags := traversal.TraverseAbs(ctx) - if diags.HasErrors() { - return true - } - if !val.IsWhollyKnown() { - return true - } - } - return false -}