forked from Knetic/govaluate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluationStage.go
466 lines (393 loc) · 13.2 KB
/
evaluationStage.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package govaluate
import (
"errors"
"fmt"
"math"
"reflect"
"regexp"
"strings"
)
const (
logicalErrorFormat string = "Value '%v' cannot be used with the logical operator '%v', it is not a bool"
modifierErrorFormat string = "Value '%v' cannot be used with the modifier '%v', it is not a number"
comparatorErrorFormat string = "Value '%v' cannot be used with the comparator '%v', it is not a number"
ternaryErrorFormat string = "Value '%v' cannot be used with the ternary operator '%v', it is not a bool"
prefixErrorFormat string = "Value '%v' cannot be used with the prefix '%v'"
)
type evaluationOperator func(left, right interface{}, parameters Parameters) (interface{}, error)
type stageTypeCheck func(value interface{}) bool
type stageCombinedTypeCheck func(left, right interface{}) bool
type evaluationStage struct {
symbol OperatorSymbol
leftStage, rightStage *evaluationStage
// the operation that will be used to evaluate this stage (such as adding [left] to [right] and return the result)
operator evaluationOperator
// ensures that both left and right values are appropriate for this stage. Returns an error if they aren't operable.
leftTypeCheck, rightTypeCheck stageTypeCheck
// if specified, will override whatever is used in "leftTypeCheck" and "rightTypeCheck".
// primarily used for specific operators that don't care which side a given type is on, but still requires one side to be of a given type
// (like string concat)
typeCheck stageCombinedTypeCheck
// regardless of which type check is used, this string format will be used as the error message for type errors
typeErrorFormat string
}
var (
_true = interface{}(true)
_false = interface{}(false)
)
func (s *evaluationStage) swapWith(other *evaluationStage) {
temp := *other
other.setToNonStage(*s)
s.setToNonStage(temp)
}
func (s *evaluationStage) setToNonStage(other evaluationStage) {
s.symbol = other.symbol
s.operator = other.operator
s.leftTypeCheck = other.leftTypeCheck
s.rightTypeCheck = other.rightTypeCheck
s.typeCheck = other.typeCheck
s.typeErrorFormat = other.typeErrorFormat
}
func (s *evaluationStage) isShortCircuitable() bool {
switch s.symbol {
case AND, OR, TERNARY_TRUE, TERNARY_FALSE, COALESCE:
return true
}
return false
}
func noopStageRight(left, right interface{}, parameters Parameters) (interface{}, error) {
return right, nil
}
func addStage(left, right interface{}, parameters Parameters) (interface{}, error) {
// string concat if either are strings
if isString(left) || isString(right) {
return fmt.Sprintf("%v%v", left, right), nil
}
return left.(float64) + right.(float64), nil
}
func subtractStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return left.(float64) - right.(float64), nil
}
func multiplyStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return left.(float64) * right.(float64), nil
}
func divideStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return left.(float64) / right.(float64), nil
}
func exponentStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return math.Pow(left.(float64), right.(float64)), nil
}
func modulusStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return math.Mod(left.(float64), right.(float64)), nil
}
func gteStage(left, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) >= right.(string)), nil
}
return boolIface(left.(float64) >= right.(float64)), nil
}
func gtStage(left, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) > right.(string)), nil
}
return boolIface(left.(float64) > right.(float64)), nil
}
func lteStage(left, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) <= right.(string)), nil
}
return boolIface(left.(float64) <= right.(float64)), nil
}
func ltStage(left, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) < right.(string)), nil
}
return boolIface(left.(float64) < right.(float64)), nil
}
func equalStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(reflect.DeepEqual(left, right)), nil
}
func notEqualStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(!reflect.DeepEqual(left, right)), nil
}
func andStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(left.(bool) && right.(bool)), nil
}
func orStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(left.(bool) || right.(bool)), nil
}
func negateStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return -right.(float64), nil
}
func invertStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(!right.(bool)), nil
}
func bitwiseNotStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return float64(^int64(right.(float64))), nil
}
func ternaryIfStage(left, right interface{}, parameters Parameters) (interface{}, error) {
if left.(bool) {
return right, nil
}
return nil, nil
}
func ternaryElseStage(left, right interface{}, parameters Parameters) (interface{}, error) {
if left != nil {
return left, nil
}
return right, nil
}
func regexStage(left, right interface{}, parameters Parameters) (interface{}, error) {
var pattern *regexp.Regexp
var err error
switch right := right.(type) {
case string:
pattern, err = regexp.Compile(right)
if err != nil {
return nil, fmt.Errorf("Unable to compile regexp pattern '%v': %v", right, err)
}
case *regexp.Regexp:
pattern = right
}
return pattern.Match([]byte(left.(string))), nil
}
func notRegexStage(left, right interface{}, parameters Parameters) (interface{}, error) {
ret, err := regexStage(left, right, parameters)
if err != nil {
return nil, err
}
return !(ret.(bool)), nil
}
func bitwiseOrStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return float64(int64(left.(float64)) | int64(right.(float64))), nil
}
func bitwiseAndStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return float64(int64(left.(float64)) & int64(right.(float64))), nil
}
func bitwiseXORStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return float64(int64(left.(float64)) ^ int64(right.(float64))), nil
}
func leftShiftStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return float64(uint64(left.(float64)) << uint64(right.(float64))), nil
}
func rightShiftStage(left, right interface{}, parameters Parameters) (interface{}, error) {
return float64(uint64(left.(float64)) >> uint64(right.(float64))), nil
}
func makeParameterStage(parameterName string) evaluationOperator {
return func(left, right interface{}, parameters Parameters) (interface{}, error) {
value, err := parameters.Get(parameterName)
if err != nil {
return nil, err
}
return value, nil
}
}
func makeLiteralStage(literal interface{}) evaluationOperator {
return func(left, right interface{}, parameters Parameters) (interface{}, error) {
return literal, nil
}
}
func makeFunctionStage(function ExpressionFunction) evaluationOperator {
return func(left, right interface{}, parameters Parameters) (interface{}, error) {
if right == nil {
return function()
}
switch right := right.(type) {
case []interface{}:
return function(right...)
default:
return function(right)
}
}
}
func typeConvertParam(p reflect.Value, t reflect.Type) (ret reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Argument type conversion failed: failed to convert '%s' to '%s'", p.Kind().String(), t.Kind().String())
ret = p
}
}()
return p.Convert(t), nil
}
func typeConvertParams(method reflect.Value, params []reflect.Value) ([]reflect.Value, error) {
methodType := method.Type()
numIn := methodType.NumIn()
numParams := len(params)
if numIn != numParams {
if numIn > numParams {
return nil, fmt.Errorf("Too few arguments to parameter call: got %d arguments, expected %d", len(params), numIn)
}
return nil, fmt.Errorf("Too many arguments to parameter call: got %d arguments, expected %d", len(params), numIn)
}
for i := 0; i < numIn; i++ {
t := methodType.In(i)
p := params[i]
pt := p.Type()
if t.Kind() != pt.Kind() {
np, err := typeConvertParam(p, t)
if err != nil {
return nil, err
}
params[i] = np
}
}
return params, nil
}
//nolint: gocognit
func makeAccessorStage(pair []string) evaluationOperator {
reconstructed := strings.Join(pair, ".")
return func(left interface{}, right interface{}, parameters Parameters) (ret interface{}, err error) {
var params []reflect.Value
value, err := parameters.Get(pair[0])
if err != nil {
return nil, err
}
// while this library generally tries to handle panic-inducing cases on its own,
// accessors are a sticky case which have a lot of possible ways to fail.
// therefore every call to an accessor sets up a defer that tries to recover from panics, converting them to errors.
defer func() {
if r := recover(); r != nil {
errorMsg := fmt.Sprintf("Failed to access '%s': %v", reconstructed, r.(string))
err = errors.New(errorMsg)
ret = nil
}
}()
for i := 1; i < len(pair); i++ {
coreValue := reflect.ValueOf(value)
var corePtrVal reflect.Value
// if this is a pointer, resolve it.
if coreValue.Kind() == reflect.Ptr {
corePtrVal = coreValue
coreValue = coreValue.Elem()
}
if coreValue.Kind() != reflect.Struct {
return nil, errors.New("Unable to access '" + pair[i] + "', '" + pair[i-1] + "' is not a struct")
}
field := coreValue.FieldByName(pair[i])
if field != (reflect.Value{}) {
value = field.Interface()
continue
}
method := coreValue.MethodByName(pair[i])
if method == (reflect.Value{}) {
if corePtrVal.IsValid() {
method = corePtrVal.MethodByName(pair[i])
}
if method == (reflect.Value{}) {
return nil, errors.New("No method or field '" + pair[i] + "' present on parameter '" + pair[i-1] + "'")
}
}
switch right := right.(type) {
case []interface{}:
givenParams := right
params = make([]reflect.Value, len(givenParams))
for idx := range givenParams {
params[idx] = reflect.ValueOf(givenParams[idx])
}
default:
if right == nil {
params = []reflect.Value{}
break
}
params = []reflect.Value{reflect.ValueOf(right)}
}
params, err = typeConvertParams(method, params)
if err != nil {
return nil, errors.New("Method call failed - '" + pair[0] + "." + pair[1] + "': " + err.Error())
}
returned := method.Call(params)
retLength := len(returned)
if retLength == 0 {
return nil, errors.New("Method call '" + pair[i-1] + "." + pair[i] + "' did not return any values.")
}
if retLength == 1 {
value = returned[0].Interface()
continue
}
if retLength == 2 {
errIface := returned[1].Interface()
err, validType := errIface.(error)
if validType && errIface != nil {
return returned[0].Interface(), err
}
value = returned[0].Interface()
continue
}
return nil, errors.New("Method call '" + pair[0] + "." + pair[1] + "' did not return either one value, or a value and an error. Cannot interpret meaning.")
}
value = castToFloat64(value)
return value, nil
}
}
func separatorStage(left, right interface{}, parameters Parameters) (interface{}, error) {
switch left := left.(type) {
case []interface{}:
return append(left, right), nil
default:
return []interface{}{left, right}, nil
}
}
func inStage(left, right interface{}, parameters Parameters) (interface{}, error) {
for _, value := range right.([]interface{}) {
if left == value {
return true, nil
}
}
return false, nil
}
//
func isString(value interface{}) bool {
_, ok := value.(string)
return ok
}
func isRegexOrString(value interface{}) bool {
switch value.(type) {
case string:
return true
case *regexp.Regexp:
return true
}
return false
}
func isBool(value interface{}) bool {
_, ok := value.(bool)
return ok
}
func isFloat64(value interface{}) bool {
_, ok := value.(float64)
return ok
}
/*
Addition usually means between numbers, but can also mean string concat.
String concat needs one (or both) of the sides to be a string.
*/
func additionTypeCheck(left, right interface{}) bool {
if isFloat64(left) && isFloat64(right) {
return true
}
return isString(left) || isString(right)
}
/*
Comparison can either be between numbers, or lexicographic between two strings,
but never between the two.
*/
func comparatorTypeCheck(left, right interface{}) bool {
if isFloat64(left) && isFloat64(right) {
return true
}
return isString(left) && isString(right)
}
func isArray(value interface{}) bool {
_, ok := value.([]interface{})
return ok
}
/*
Converting a boolean to an interface{} requires an allocation.
We can use interned bools to avoid this cost.
*/
func boolIface(b bool) interface{} {
if b {
return _true
}
return _false
}