Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use hasUnknowns from partialEval. #1948

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions hcl/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down
21 changes: 2 additions & 19 deletions stdlib/ternary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Loading