/
expression_template.go
58 lines (52 loc) · 1.69 KB
/
expression_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package template
import (
"fmt"
"io"
"os"
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/file"
"github.com/antonmedv/expr/parser/lexer"
)
func init() {
if os.Getenv("EXPRESSION_TEMPLATES") != "false" {
registerKind(kindExpression)
}
}
func expressionReplace(w io.Writer, expression string, env map[string]interface{}, allowUnresolved bool) (int, error) {
if _, ok := env["retries"]; !ok && hasRetries(expression) && allowUnresolved {
// this is to make sure expressions like `sprig.int(retries)` don't get resolved to 0 when `retries` don't exist in the env
// See https://github.com/argoproj/argo-workflows/issues/5388
return w.Write([]byte(fmt.Sprintf("{{%s%s}}", kindExpression, expression)))
}
result, err := expr.Eval(expression, env)
if (err != nil || result == nil) && allowUnresolved { // <nil> result is also un-resolved, and any error can be unresolved
return w.Write([]byte(fmt.Sprintf("{{%s%s}}", kindExpression, expression)))
}
if err != nil {
return 0, fmt.Errorf("failed to evaluate expression: %w", err)
}
if result == nil {
return 0, fmt.Errorf("failed to evaluate expression %q", expression)
}
return w.Write([]byte(fmt.Sprintf("%v", result)))
}
func envMap(replaceMap map[string]string) map[string]interface{} {
envMap := make(map[string]interface{})
for k, v := range replaceMap {
envMap[k] = v
}
return envMap
}
// hasRetries checks if the variable `retries` exists in the expression template
func hasRetries(expression string) bool {
tokens, err := lexer.Lex(file.NewSource(expression))
if err != nil {
return false
}
for _, token := range tokens {
if token.Kind == lexer.Identifier && token.Value == "retries" {
return true
}
}
return false
}