Expression evaluator fixes (#1009)
* refactor: remove debug error output Errors should always be logged with an error level and not debug level. Since the error is returned here, it will be logged later as an error. Presumably this was a leftover from debugging the executor chain in: PR: #971 * refactor: debug log wich expression is going to be evaluated * fix: handle nil in EvalBool We've seen this issue when the env map is not set-up properly, i.e. when the env map is nil, EvalBool might return nil, which should be handled as a falsy value. * fix: fail on error in if expression and return the evaluation error Stop running the workflow in case an expression cannot be evaluated. Fixes: #1008 * fix: remove quotes from inside expression syntax in test It looks like having an expression inside double quotes inside the expression syntax is not valid: https://github.com/ZauberNerd/act-test/actions/runs/1881986429 The workflow is not valid. .github/workflows/test.yml (Line: 10, Col: 13): Unexpected symbol: '"endsWith'. Located at position 1 within expression: "endsWith('Hello world', 'ld')" * refactor: export IsTruthy function from exprparser package * refactor: use IsTruthy function in EvalBool * refactor: move debug log for expression rewrite to rewrite function Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package runner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -128,7 +127,9 @@ type expressionEvaluator struct {
|
||||
}
|
||||
|
||||
func (ee expressionEvaluator) evaluate(in string, isIfExpression bool) (interface{}, error) {
|
||||
log.Debugf("evaluating expression '%s'", in)
|
||||
evaluated, err := ee.interpreter.Evaluate(in, isIfExpression)
|
||||
log.Debugf("expression '%s' evaluated to '%t'", in, evaluated)
|
||||
return evaluated, err
|
||||
}
|
||||
|
||||
@@ -141,9 +142,6 @@ func (ee expressionEvaluator) evaluateScalarYamlNode(node *yaml.Node) error {
|
||||
return nil
|
||||
}
|
||||
expr, _ := rewriteSubExpression(in, false)
|
||||
if in != expr {
|
||||
log.Debugf("expression '%s' rewritten to '%s'", in, expr)
|
||||
}
|
||||
res, err := ee.evaluate(expr, false)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -214,18 +212,12 @@ func (ee expressionEvaluator) Interpolate(in string) string {
|
||||
}
|
||||
|
||||
expr, _ := rewriteSubExpression(in, true)
|
||||
if in != expr {
|
||||
log.Debugf("expression '%s' rewritten to '%s'", in, expr)
|
||||
}
|
||||
|
||||
evaluated, err := ee.evaluate(expr, false)
|
||||
if err != nil {
|
||||
log.Errorf("Unable to interpolate expression '%s': %s", expr, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
log.Debugf("expression '%s' evaluated to '%s'", expr, evaluated)
|
||||
|
||||
value, ok := evaluated.(string)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Expression %s did not evaluate to a string", expr))
|
||||
@@ -237,37 +229,13 @@ func (ee expressionEvaluator) Interpolate(in string) string {
|
||||
// EvalBool evaluates an expression against given evaluator
|
||||
func EvalBool(evaluator ExpressionEvaluator, expr string) (bool, error) {
|
||||
nextExpr, _ := rewriteSubExpression(expr, false)
|
||||
if expr != nextExpr {
|
||||
log.Debugf("expression '%s' rewritten to '%s'", expr, nextExpr)
|
||||
}
|
||||
|
||||
evaluated, err := evaluator.evaluate(nextExpr, true)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var result bool
|
||||
|
||||
switch t := evaluated.(type) {
|
||||
case bool:
|
||||
result = t
|
||||
case string:
|
||||
result = t != ""
|
||||
case int:
|
||||
result = t != 0
|
||||
case float64:
|
||||
if math.IsNaN(t) {
|
||||
result = false
|
||||
} else {
|
||||
result = t != 0
|
||||
}
|
||||
default:
|
||||
return false, fmt.Errorf("Unable to map return type to boolean for '%s'", expr)
|
||||
}
|
||||
|
||||
log.Debugf("expression '%s' evaluated to '%t'", nextExpr, result)
|
||||
|
||||
return result, nil
|
||||
return exprparser.IsTruthy(evaluated), nil
|
||||
}
|
||||
|
||||
func escapeFormatString(in string) string {
|
||||
@@ -334,5 +302,9 @@ func rewriteSubExpression(in string, forceFormat bool) (string, error) {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
return fmt.Sprintf("format('%s', %s)", strings.ReplaceAll(formatOut, "'", "''"), strings.Join(results, ", ")), nil
|
||||
out := fmt.Sprintf("format('%s', %s)", strings.ReplaceAll(formatOut, "'", "''"), strings.Join(results, ", "))
|
||||
if in != out {
|
||||
log.Debugf("expression '%s' rewritten to '%s'", in, out)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user