-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
expr.go
92 lines (76 loc) · 1.72 KB
/
expr.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package expr
import (
"encoding/json"
"strings"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
expr_proto "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
const missingVariableMessage = "undeclared reference to '"
type Expr struct {
expr string
program cel.Program
}
func (e *Expr) DecodeString(value string) (err error) {
var ast *cel.Ast
var env *cel.Env
variables := make([]*expr_proto.Decl, 0, 10)
found := make(map[string]struct{}, 10)
for {
env, err = cel.NewEnv(cel.Declarations(variables...))
if err != nil {
return err
}
var iss *cel.Issues
ast, iss = env.Compile(value)
if iss.Err() != nil {
for _, e := range iss.Errors() {
if strings.HasPrefix(e.Message, missingVariableMessage) {
msg := e.Message[len(missingVariableMessage):]
msg = msg[0:strings.IndexRune(msg, '\'')]
if _, exists := found[msg]; exists {
continue
}
variables = append(variables, decls.NewVar(msg, decls.Any))
found[msg] = struct{}{}
} else {
return iss.Err()
}
}
} else {
break
}
}
prg, err := env.Program(ast)
if err != nil {
return err
}
*e = Expr{
expr: value,
program: prg,
}
return nil
}
func (e *Expr) Eval(variables map[string]interface{}) (interface{}, error) {
out, _, err := e.program.Eval(variables)
if err != nil {
return nil, err
}
return out.Value(), nil
}
func (e *Expr) Expr() string {
return e.expr
}
func (e *Expr) String() string {
return e.expr
}
func (e *Expr) MarshalJSON() ([]byte, error) {
return json.Marshal(e.expr)
}
func (e *Expr) UnmarshalJSON(b []byte) error {
var code string
if err := json.Unmarshal(b, &code); err != nil {
return err
}
return e.DecodeString(code)
}