-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
resolve_var.go
35 lines (31 loc) · 943 Bytes
/
resolve_var.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
package template
import (
"strings"
"github.com/expr-lang/expr"
"github.com/argoproj/argo-workflows/v3/errors"
)
func ResolveVar(s string, m map[string]interface{}) (interface{}, error) {
tag := strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(s, prefix), suffix))
kind, expression := parseTag(tag)
switch kind {
case kindExpression:
program, err := expr.Compile(expression, expr.Env(m))
if err != nil {
return nil, errors.Errorf(errors.CodeBadRequest, "Unable to compile: %q", expression)
}
result, err := expr.Run(program, m)
if err != nil {
return nil, errors.Errorf(errors.CodeBadRequest, "Invalid expression: %q", expression)
}
if result == nil {
return nil, errors.Errorf(errors.CodeBadRequest, "Unable to resolve: %q", tag)
}
return result, nil
default:
v, ok := m[tag]
if !ok {
return nil, errors.Errorf(errors.CodeBadRequest, "Unable to resolve: %q", tag)
}
return v, nil
}
}