Add coalescing operator

This commit is contained in:
2023-10-30 08:52:02 -07:00
parent c1a2e56f4f
commit 900de82d6a
6 changed files with 301 additions and 165 deletions

View File

@@ -218,6 +218,8 @@ func (t *Template) getValue(node ast.Node, local map[string]any) (any, error) {
return t.execMethodCall(node, local)
case ast.Ternary:
return t.evalTernary(node, local)
case ast.VariableOr:
return t.evalVariableOr(node, local)
default:
return nil, nil
}
@@ -448,6 +450,14 @@ func (t *Template) evalTernary(tr ast.Ternary, local map[string]any) (any, error
}
}
func (t *Template) evalVariableOr(vo ast.VariableOr, local map[string]any) (any, error) {
val, err := t.getVar(vo.Variable, local)
if err != nil {
return t.getValue(vo.Or, local)
}
return val.Interface(), nil
}
func (t *Template) posError(n ast.Node, format string, v ...any) error {
return ast.PosError(n, t.name, format, v...)
}