Skip to content
Open
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
26 changes: 26 additions & 0 deletions ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,20 @@ func Validate(schemaExpr, valueExpr Expr) *ValidateExpr {
}
}

type TemplateExpr struct {
builtinNode

//TemplateDef Expr
}

type EvalExpr struct {
builtinNode

//TemplateValue Expr
// either a symbol or a template expr?
// or just allow anything but behave as pass-thru for non-templates?
}

func tryParseFunction(node *syntax.ObjectNode) (Expr, syntax.Diagnostics, bool) {
var diags syntax.Diagnostics
if node.Len() != 1 {
Expand Down Expand Up @@ -760,6 +774,18 @@ func tryParseFunction(node *syntax.ObjectNode) (Expr, syntax.Diagnostics, bool)
parse = parseToJSON
case "fn::toString":
parse = parseToString
case "fn::template":
parse = func(node *syntax.ObjectNode, name *StringExpr, args Expr) (Expr, syntax.Diagnostics) {
return &TemplateExpr{
builtinNode: builtin(node, name, args),
}, nil
}
case "fn::eval":
parse = func(node *syntax.ObjectNode, name *StringExpr, args Expr) (Expr, syntax.Diagnostics) {
return &EvalExpr{
builtinNode: builtin(node, name, args),
}, nil
}
default:
if strings.HasPrefix(kvp.Key.Value(), "fn::open::") {
parse = parseShortOpen
Expand Down
12 changes: 12 additions & 0 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ func declare[Expr exprNode](e *evalContext, path string, x Expr, base *value) *e
case *ast.ToStringExpr:
repr := &toStringExpr{node: x, value: declare(e, "", x.Value, nil)}
return newExpr(path, repr, schema.String().Schema(), base)
case *ast.TemplateExpr:
repr := &templateExpr{node: x, template: declare(e, "", x.Args(), nil)}
return newExpr(path, repr, schema.Never().Schema(), base)
case *ast.EvalExpr:
repr := &evalExpr{node: x, value: declare(e, "", x.Args(), base)}
return newExpr(path, repr, schema.Always().Schema(), base)
case *ast.ArrayExpr:
elements := make([]*expr, len(x.Elements))
for i, x := range x.Elements {
Expand Down Expand Up @@ -648,6 +654,12 @@ func (e *evalContext) evaluateExpr(x *expr, accept *schema.Schema) *value {
val = e.evaluateBuiltinToJSON(x, repr)
case *toStringExpr:
val = e.evaluateBuiltinToString(x, repr)
case *templateExpr:
val = &value{def: x, schema: x.schema, repr: repr, unknown: true} // templates defer evaluation
case *evalExpr:
defn := e.evaluateExpr(repr.value, schema.Always()) // deref template expr
expr := declare(e, "", defn.repr.(*templateExpr).node.Args(), nil) // clone its ast node into a new expr
val = e.evaluateExpr(expr, schema.Always()) // evaluate it
Comment on lines +660 to +662
case *arrayExpr:
val = e.evaluateArray(x, repr, accept)
case *objectExpr:
Expand Down
3 changes: 3 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ func TestEval(t *testing.T) {

path := filepath.Join("testdata", "eval")
entries, err := os.ReadDir(path)
entries = slices.DeleteFunc(entries, func(entry os.DirEntry) bool {
return entry.Name() != "late-binding"
})
Comment on lines +367 to +369
require.NoError(t, err)
for _, e := range entries {
if e.Name() == "bench" {
Expand Down
24 changes: 24 additions & 0 deletions eval/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ func (x *expr) export(environment string) esc.Expr {
for k, v := range repr.properties {
ex.Object[k] = v.export(environment)
}
case *templateExpr:
// not evaluated
case *evalExpr:
return repr.value.export(environment)
Comment on lines +348 to +350
default:
panic(fmt.Sprintf("fatal: invalid expr type %T", repr))
}
Expand Down Expand Up @@ -599,3 +603,23 @@ type validateExpr struct {
func (x *validateExpr) syntax() ast.Expr {
return x.node
}

type templateExpr struct {
node *ast.TemplateExpr

template *expr
}

func (x *templateExpr) syntax() ast.Expr {
return x.node
}

type evalExpr struct {
node *ast.EvalExpr

value *expr
}

func (x *evalExpr) syntax() ast.Expr {
return x.node
}
3 changes: 3 additions & 0 deletions eval/testdata/eval/late-binding/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
values:
a: ${environments.example.a}
b: ${environments.example.b}
4 changes: 4 additions & 0 deletions eval/testdata/eval/late-binding/example/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
values:
name: "foo"
example:
fn::eval: ${environments.example.defn.hello}
4 changes: 4 additions & 0 deletions eval/testdata/eval/late-binding/example/b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
values:
name: "bar"
example:
fn::eval: ${environments.example.defn.hello}
3 changes: 3 additions & 0 deletions eval/testdata/eval/late-binding/example/defn.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
values:
hello:
fn::template: Hello ${name}!
Loading
Loading