-
Notifications
You must be signed in to change notification settings - Fork 17
/
parse.go
301 lines (250 loc) · 6.23 KB
/
parse.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package predicate
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"reflect"
"strconv"
"strings"
"github.com/gravitational/trace"
)
func NewParser(d Def) (Parser, error) {
return &predicateParser{d: d}, nil
}
type predicateParser struct {
d Def
}
func (p *predicateParser) Parse(in string) (interface{}, error) {
expr, err := parser.ParseExpr(in)
if err != nil {
return nil, err
}
return p.parse(expr)
}
func (p *predicateParser) parse(expr ast.Expr) (interface{}, error) {
switch n := expr.(type) {
case *ast.BinaryExpr:
x, err := p.parse(n.X)
if err != nil {
return nil, err
}
y, err := p.parse(n.Y)
if err != nil {
return nil, err
}
return p.joinPredicates(n.Op, x, y)
case *ast.ParenExpr:
return p.parse(n.X)
case *ast.UnaryExpr:
joinFn, err := p.getJoinFunction(n.Op)
if err != nil {
return nil, err
}
node, err := p.parse(n.X)
if err != nil {
return nil, err
}
return callFunction(joinFn, []interface{}{node})
default:
return p.evaluateExpr(n)
}
}
func (p *predicateParser) evaluateArguments(nodes []ast.Expr) ([]interface{}, error) {
out := make([]interface{}, len(nodes))
for i, n := range nodes {
val, err := p.evaluateExpr(n)
if err != nil {
return nil, trace.Wrap(err)
}
out[i] = val
}
return out, nil
}
func (p *predicateParser) evaluateExpr(n ast.Expr) (interface{}, error) {
switch l := n.(type) {
case *ast.BasicLit:
val, err := literalToValue(l)
if err != nil {
return nil, err
}
return val, nil
case *ast.IndexExpr:
if p.d.GetProperty == nil {
return nil, trace.NotFound("properties are not supported")
}
mapVal, err := p.evaluateExpr(l.X)
if err != nil {
return nil, trace.Wrap(err)
}
keyVal, err := p.evaluateExpr(l.Index)
if err != nil {
return nil, trace.Wrap(err)
}
val, err := p.d.GetProperty(mapVal, keyVal)
if err != nil {
return nil, trace.Wrap(err)
}
return val, nil
case *ast.SelectorExpr:
fields, err := evaluateSelector(l, []string{})
if err != nil {
return nil, trace.Wrap(err)
}
if p.d.GetIdentifier == nil {
return nil, trace.NotFound("%v is not defined", strings.Join(fields, "."))
}
val, err := p.d.GetIdentifier(fields)
if err != nil {
return nil, trace.Wrap(err)
}
return val, nil
case *ast.Ident:
if p.d.GetIdentifier == nil {
return nil, trace.NotFound("%v is not defined", l.Name)
}
val, err := p.d.GetIdentifier([]string{l.Name})
if err != nil {
return nil, trace.Wrap(err)
}
return val, nil
case *ast.CallExpr:
name, err := getIdentifier(l.Fun)
if err != nil {
return nil, err
}
fn, err := p.getFunction(name)
if err != nil {
return nil, err
}
arguments, err := p.evaluateArguments(l.Args)
if err != nil {
return nil, err
}
return callFunction(fn, arguments)
default:
return nil, trace.BadParameter("%T is not supported", n)
}
}
// evaluateSelector recursively evaluates the selector field and returns a list
// of properties at the end.
func evaluateSelector(sel *ast.SelectorExpr, fields []string) ([]string, error) {
fields = append([]string{sel.Sel.Name}, fields...)
switch l := sel.X.(type) {
case *ast.SelectorExpr:
return evaluateSelector(l, fields)
case *ast.Ident:
fields = append([]string{l.Name}, fields...)
return fields, nil
default:
return nil, trace.BadParameter("unsupported selector type: %T", l)
}
}
func (p *predicateParser) getFunction(name string) (interface{}, error) {
v, ok := p.d.Functions[name]
if !ok {
return nil, trace.BadParameter("unsupported function: %s", name)
}
return v, nil
}
func (p *predicateParser) joinPredicates(op token.Token, a, b interface{}) (interface{}, error) {
joinFn, err := p.getJoinFunction(op)
if err != nil {
return nil, err
}
return callFunction(joinFn, []interface{}{a, b})
}
func (p *predicateParser) getJoinFunction(op token.Token) (interface{}, error) {
var fn interface{}
switch op {
case token.NOT:
fn = p.d.Operators.NOT
case token.LAND:
fn = p.d.Operators.AND
case token.LOR:
fn = p.d.Operators.OR
case token.GTR:
fn = p.d.Operators.GT
case token.GEQ:
fn = p.d.Operators.GE
case token.LSS:
fn = p.d.Operators.LT
case token.LEQ:
fn = p.d.Operators.LE
case token.EQL:
fn = p.d.Operators.EQ
case token.NEQ:
fn = p.d.Operators.NEQ
}
if fn == nil {
return nil, trace.BadParameter("%v is not supported", op)
}
return fn, nil
}
func getIdentifier(node ast.Node) (string, error) {
sexpr, ok := node.(*ast.SelectorExpr)
if ok {
id, okIdent := sexpr.X.(*ast.Ident)
if !okIdent {
return "", trace.BadParameter("expected selector identifier, got: %T", sexpr.X)
}
return fmt.Sprintf("%s.%s", id.Name, sexpr.Sel.Name), nil
}
id, ok := node.(*ast.Ident)
if !ok {
return "", trace.BadParameter("expected identifier, got: %T", node)
}
return id.Name, nil
}
func literalToValue(a *ast.BasicLit) (interface{}, error) {
switch a.Kind {
case token.FLOAT:
value, err := strconv.ParseFloat(a.Value, 64)
if err != nil {
return nil, trace.BadParameter("failed to parse argument: %s, error: %s", a.Value, err)
}
return value, nil
case token.INT:
value, err := strconv.Atoi(a.Value)
if err != nil {
return nil, trace.BadParameter("failed to parse argument: %s, error: %s", a.Value, err)
}
return value, nil
case token.STRING:
value, err := strconv.Unquote(a.Value)
if err != nil {
return nil, trace.BadParameter("failed to parse argument: %s, error: %s", a.Value, err)
}
return value, nil
}
return nil, trace.BadParameter("unsupported function argument type: '%v'", a.Kind)
}
func callFunction(f interface{}, args []interface{}) (v interface{}, err error) {
defer func() {
if r := recover(); r != nil {
err = trace.BadParameter("%s", r)
}
}()
arguments := make([]reflect.Value, len(args))
for i, a := range args {
arguments[i] = reflect.ValueOf(a)
}
fn := reflect.ValueOf(f)
ret := fn.Call(arguments)
switch len(ret) {
case 1:
return ret[0].Interface(), nil
case 2:
v, e := ret[0].Interface(), ret[1].Interface()
if e == nil {
return v, nil
}
err, ok := e.(error)
if !ok {
return nil, trace.BadParameter("expected error as a second return value, got %T", e)
}
return v, err
default:
return nil, trace.BadParameter("expected at least one return argument for '%v'", fn)
}
}