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

Refactor eval functions #2201

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
14 changes: 9 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/agext/levenshtein"
"github.com/hashicorp/hcl/v2"
"github.com/pkg/errors"
"github.com/zclconf/go-cty/cty"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -720,14 +721,17 @@ func (bp *Blueprint) evalVars() (Dict, error) {
return Dict{}, err
}

res := Dict{}
res := map[string]cty.Value{}
ctx := hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: functions()}
for _, n := range order {
v := bp.Vars.Get(n)
ev, err := evalValue(v, Blueprint{Vars: res})
ctx.Variables["var"] = cty.ObjectVal(res)
ev, err := eval(bp.Vars.Get(n), &ctx)
if err != nil {
return Dict{}, BpError{Root.Vars.Dot(n), err}
}
res.Set(n, ev)
res[n] = ev
}
return res, nil
return NewDict(res), nil
}
2 changes: 1 addition & 1 deletion pkg/config/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (d Dict) IsZero() bool {
func (d Dict) Eval(bp Blueprint) (Dict, error) {
var res Dict
for k, v := range d.Items() {
r, err := evalValue(v, bp)
r, err := bp.Eval(v)
if err != nil {
return Dict{}, fmt.Errorf("error while trying to evaluate %#v: %w", k, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func validateModuleInputs(mp ModulePath, m Module, bp Blueprint) error {
}

func attemptEvalModuleInput(val cty.Value, bp Blueprint) (cty.Value, bool) {
v, err := evalValue(val, bp)
v, err := bp.Eval(val)
// there could be a legitimate reasons for it.
// e.g. use of modules output or unsupported (by ghpc) functions
// TODO:
Expand Down
24 changes: 14 additions & 10 deletions pkg/config/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func TraversalToReference(t hcl.Traversal) (Reference, error) {

// Expression is a representation of expressions in Blueprint
type Expression interface {
// Eval evaluates the expression in the context of Blueprint
Eval(bp Blueprint) (cty.Value, error)
// Eval evaluates the expression in the given context
Eval(ctx *hcl.EvalContext) (cty.Value, error)
// Tokenize returns Tokens to be used for marshalling HCL
Tokenize() hclwrite.Tokens
// References return Reference for all variables used in the expression
Expand Down Expand Up @@ -213,12 +213,8 @@ type BaseExpression struct {
}

// Eval evaluates the expression in the context of Blueprint
func (e BaseExpression) Eval(bp Blueprint) (cty.Value, error) {
ctx := hcl.EvalContext{
Variables: map[string]cty.Value{"var": bp.Vars.AsObject()},
Functions: functions(),
}
v, diag := e.e.Value(&ctx)
func (e BaseExpression) Eval(ctx *hcl.EvalContext) (cty.Value, error) {
v, diag := e.e.Value(ctx)
if diag.HasErrors() {
return cty.NilVal, diag
}
Expand Down Expand Up @@ -374,10 +370,18 @@ func valueReferences(v cty.Value) map[Reference]cty.Path {
return r
}

func evalValue(v cty.Value, bp Blueprint) (cty.Value, error) {
func (bp *Blueprint) Eval(v cty.Value) (cty.Value, error) {
ctx := hcl.EvalContext{
Variables: map[string]cty.Value{
"var": bp.Vars.AsObject()},
Functions: functions()}
return eval(v, &ctx)
}

func eval(v cty.Value, ctx *hcl.EvalContext) (cty.Value, error) {
return cty.Transform(v, func(p cty.Path, v cty.Value) (cty.Value, error) {
if e, is := IsExpressionValue(v); is {
return e.Eval(bp)
return e.Eval(ctx)
}
return v, nil
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestFlattenFunctionCallExpression(t *testing.T) {
cty.NumberIntVal(2),
cty.NumberIntVal(3)})

got, err := expr.Eval(bp)
got, err := bp.Eval(expr.AsValue())
if err != nil {
t.Errorf("got unexpected error: %s", err)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestMergeFunctionCallExpression(t *testing.T) {
"two": cty.NumberIntVal(2),
})

got, err := expr.Eval(bp)
got, err := bp.Eval(expr.AsValue())
if err != nil {
t.Errorf("got unexpected error: %s", err)
}
Expand Down
Loading