forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.go
759 lines (716 loc) · 19.8 KB
/
eval.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
package tick
import (
"errors"
"fmt"
"go/ast"
"log"
"os"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
)
var mu sync.Mutex
var logger = log.New(os.Stderr, "[tick] ", log.LstdFlags)
func getLogger() *log.Logger {
mu.Lock()
defer mu.Unlock()
return logger
}
func SetLogger(l *log.Logger) {
mu.Lock()
defer mu.Unlock()
logger = l
}
// Interface for interacting with objects.
// If an object does not self describe via this interface
// than a reflection based implemenation will be used.
type SelfDescriber interface {
//A description the object
Desc() string
HasChainMethod(name string) bool
CallChainMethod(name string, args ...interface{}) (interface{}, error)
HasProperty(name string) bool
Property(name string) interface{}
SetProperty(name string, args ...interface{}) (interface{}, error)
}
// PartialDescriber can provide a description
// of its chain methods that hide embedded property methods.
type PartialDescriber interface {
ChainMethods() map[string]reflect.Value
}
// Parse and evaluate a given script for the scope.
// This evaluation method uses reflection to call
// methods on objects within the scope.
func Evaluate(script string, scope *Scope) (err error) {
defer func(errP *error) {
r := recover()
if r == ErrEmptyStack {
trace := make([]byte, 1024)
n := runtime.Stack(trace, false)
*errP = fmt.Errorf("evaluation caused stack error: %v Go Trace: %s", r, string(trace[:n]))
} else if r != nil {
panic(r)
}
}(&err)
root, err := parse(script)
if err != nil {
return err
}
// Use a stack machine to evaluate the AST
stck := &stack{}
return eval(root, scope, stck)
}
func errorf(p Position, fmtStr string, args ...interface{}) error {
lineStr := fmt.Sprintf("line %d char %d: %s", p.Line(), p.Char(), fmtStr)
return fmt.Errorf(lineStr, args...)
}
func wrapError(p Position, err error) error {
if err == nil {
return nil
}
return fmt.Errorf("line %d char %d: %s", p.Line(), p.Char(), err.Error())
}
// Evaluate a node using a stack machine in a given scope
func eval(n Node, scope *Scope, stck *stack) (err error) {
switch node := n.(type) {
case *BoolNode:
stck.Push(node.Bool)
case *NumberNode:
if node.IsInt {
stck.Push(node.Int64)
} else {
stck.Push(node.Float64)
}
case *DurationNode:
stck.Push(node.Dur)
case *StringNode:
stck.Push(node.Literal)
case *RegexNode:
stck.Push(node.Regex)
case *UnaryNode:
err = eval(node.Node, scope, stck)
if err != nil {
return
}
err := evalUnary(node, node.Operator, scope, stck)
if err != nil {
return err
}
case *LambdaNode:
// Catch panic from resolveIdents and return as error.
err = func() (e error) {
defer func(ep *error) {
err := recover()
if err != nil {
*ep = err.(error)
}
}(&e)
node.Node = resolveIdents(node.Node, scope)
return e
}()
if err != nil {
return
}
stck.Push(node.Node)
case *DeclarationNode:
err = eval(node.Left, scope, stck)
if err != nil {
return
}
err = eval(node.Right, scope, stck)
if err != nil {
return
}
err = evalDeclaration(scope, stck)
if err != nil {
return
}
case *ChainNode:
err = eval(node.Left, scope, stck)
if err != nil {
return
}
err = eval(node.Right, scope, stck)
if err != nil {
return
}
err = evalChain(node, scope, stck)
if err != nil {
return
}
case *FunctionNode:
args := make([]interface{}, len(node.Args))
for i, arg := range node.Args {
err = eval(arg, scope, stck)
if err != nil {
return
}
a := stck.Pop()
switch typed := a.(type) {
case *IdentifierNode:
// Resolve identifier
a, err = scope.Get(typed.Ident)
if err != nil {
return err
}
case unboundFunc:
// Call global func
a, err = typed(nil)
if err != nil {
return err
}
}
args[i] = a
}
err = evalFunc(node, scope, stck, args)
if err != nil {
return
}
case *ListNode:
for _, n := range node.Nodes {
err = eval(n, scope, stck)
if err != nil {
return
}
// Pop unused result
if stck.Len() > 0 {
stck.Pop()
}
}
default:
stck.Push(node)
}
return nil
}
func evalUnary(p Position, op TokenType, scope *Scope, stck *stack) error {
v := stck.Pop()
switch op {
case TokenMinus:
if ident, ok := v.(*IdentifierNode); ok {
value, err := scope.Get(ident.Ident)
if err != nil {
return err
}
v = value
}
switch num := v.(type) {
case float64:
stck.Push(-1 * num)
case int64:
stck.Push(-1 * num)
case time.Duration:
stck.Push(-1 * num)
default:
return errorf(p, "invalid arugument to '-' %v", v)
}
case TokenNot:
if ident, ok := v.(*IdentifierNode); ok {
value, err := scope.Get(ident.Ident)
if err != nil {
return err
}
v = value
}
if b, ok := v.(bool); ok {
stck.Push(!b)
} else {
return errorf(p, "invalid arugument to '!' %v", v)
}
}
return nil
}
func evalDeclaration(scope *Scope, stck *stack) error {
r := stck.Pop()
l := stck.Pop()
i := l.(*IdentifierNode)
scope.Set(i.Ident, r)
return nil
}
func evalChain(p Position, scope *Scope, stck *stack) error {
r := stck.Pop()
l := stck.Pop()
// Resolve identifier
if left, ok := l.(*IdentifierNode); ok {
var err error
l, err = scope.Get(left.Ident)
if err != nil {
return err
}
}
switch right := r.(type) {
case unboundFunc:
ret, err := right(l)
if err != nil {
return err
}
stck.Push(ret)
case *IdentifierNode:
name := right.Ident
//Lookup field by name of left object
var describer SelfDescriber
if d, ok := l.(SelfDescriber); ok {
describer = d
} else {
var err error
var extraChainMethods map[string]reflect.Value
if pd, ok := l.(PartialDescriber); ok {
extraChainMethods = pd.ChainMethods()
}
describer, err = NewReflectionDescriber(l, extraChainMethods)
if err != nil {
return wrapError(p, err)
}
}
if describer.HasProperty(name) {
stck.Push(describer.Property(name))
} else {
return errorf(p, "object %T has no property %s", l, name)
}
default:
return errorf(p, "invalid right operand of type %T to '.' operator", r)
}
return nil
}
func evalFunc(f *FunctionNode, scope *Scope, stck *stack, args []interface{}) error {
rec := func(obj interface{}, errp *error) {
e := recover()
if e != nil {
*errp = fmt.Errorf("line %d char%d: error calling func %q on obj %T: %v", f.Line(), f.Char(), f.Func, obj, e)
if strings.Contains((*errp).Error(), "*tick.ReferenceNode") && strings.Contains((*errp).Error(), "type string") {
*errp = fmt.Errorf("line %d char%d: cannot assign *tick.ReferenceNode to type string, did you use double quotes instead of single quotes?", f.Line(), f.Char())
}
}
}
fnc := unboundFunc(func(obj interface{}) (_ interface{}, err error) {
//Setup recover method if there is a panic during the method call
defer rec(obj, &err)
if f.Type == globalFunc {
if obj != nil {
return nil, fmt.Errorf("line %d char%d: calling global function on object %T", f.Line(), f.Char(), obj)
}
// Object is nil, check for func in scope
fnc, _ := scope.Get(f.Func)
if fnc == nil {
return nil, fmt.Errorf("line %d char%d: no global function %q defined", f.Line(), f.Char(), f.Func)
}
method := reflect.ValueOf(fnc)
o, err := callMethodReflection(method, args)
return o, wrapError(f, err)
}
// Get SelfDescriber
name := f.Func
var describer SelfDescriber
if d, ok := obj.(SelfDescriber); ok {
describer = d
} else {
var err error
var extraChainMethods map[string]reflect.Value
if pd, ok := obj.(PartialDescriber); ok {
extraChainMethods = pd.ChainMethods()
}
describer, err = NewReflectionDescriber(obj, extraChainMethods)
if err != nil {
return nil, wrapError(f, err)
}
}
// Call correct type of function
switch f.Type {
case chainFunc:
if describer.HasChainMethod(name) {
o, err := describer.CallChainMethod(name, args...)
return o, wrapError(f, err)
}
if describer.HasProperty(name) {
return nil, errorf(f, "no chaining method %q on %T, but property does exist. Use '.' operator instead: 'node.%s(..)'.", name, obj, name)
}
if dm := scope.DynamicMethod(name); dm != nil {
return nil, errorf(f, "no chaining method %q on %T, but dynamic method does exist. Use '@' operator instead: 'node@%s(..)'.", name, obj, name)
}
case propertyFunc:
if describer.HasProperty(name) {
o, err := describer.SetProperty(name, args...)
return o, wrapError(f, err)
}
if describer.HasChainMethod(name) {
return nil, errorf(f, "no property method %q on %T, but chaining method does exist. Use '|' operator instead: 'node|%s(..)'.", name, obj, name)
}
if dm := scope.DynamicMethod(name); dm != nil {
return nil, errorf(f, "no property method %q on %T, but dynamic method does exist. Use '@' operator instead: 'node@%s(..)'.", name, obj, name)
}
case dynamicFunc:
// Check for dynamic method.
if dm := scope.DynamicMethod(name); dm != nil {
ret, err := dm(obj, args...)
if err != nil {
return nil, err
}
return ret, nil
}
if describer.HasProperty(name) {
return nil, errorf(f, "no dynamic method %q on %T, but property does exist. Use '.' operator instead: 'node.%s(..)'.", name, obj, name)
}
if describer.HasChainMethod(name) {
return nil, errorf(f, "no dynamic method %q on %T, but chaining method does exist. Use '|' operator instead: 'node|%s(..)'.", name, obj, name)
}
default:
return nil, errorf(f, "unknown function type %v on function %T.%s", f.Type, obj, name)
}
// Ran out of options...
return nil, errorf(f, "no method or property %q on %T", name, obj)
})
stck.Push(fnc)
return nil
}
// Wraps any object as a SelfDescriber using reflection.
//
// Uses tags on fields to determine if a method is really a PropertyMethod
// Can disambiguate property fields and chain methods of the same name but
// from different composed anonymous fields.
// Cannot disambiguate property methods and chain methods of the same name.
// See NewReflectionDescriber for providing explicit chain methods in this case.
//
// Example:
// type MyType struct {
// UseX `tick:"X"`
// }
// func (m *MyType) X() *MyType{
// m.UseX = true
// return m
// }
//
// UseX will be ignored as a property and the method X will become a property method.
//
//
// Expects that all callable methods are pointer receiver methods.
type ReflectionDescriber struct {
obj interface{}
// Set of chain methods
chainMethods map[string]reflect.Value
// Set of methods that modify properties
propertyMethods map[string]reflect.Value
// Set of fields on obj that can be set
properties map[string]reflect.Value
}
// Create a NewReflectionDescriber from an object.
// The object must be a pointer type.
// Use the chainMethods parameter to provide a set of explicit methods
// that should be considered chain methods even if an embedded type declares them as property methods
//
// Example:
// type MyType struct {
// UseX `tick:"X"`
// }
// func (m *MyType) X() *MyType{
// m.UseX = true
// return m
// }
//
// type AnotherType struct {
// MyType
// }
// func (a *AnotherType) X() *YetAnotherType {
// // do chain method work here...
// }
//
// // Now create NewReflectionDescriber with X as a chain method and property method
// at := new(AnotherType)
// rd := NewReflectionDescriber(at, map[string]reflect.Value{
// "X": reflect.ValueOf(at.X),
// })
// rd.HasProperty("x") // true
// rd.HasChainMethod("x") // true
//
func NewReflectionDescriber(obj interface{}, chainMethods map[string]reflect.Value) (*ReflectionDescriber, error) {
r := &ReflectionDescriber{
obj: obj,
}
rv := reflect.ValueOf(r.obj)
if !rv.IsValid() && rv.Kind() != reflect.Struct {
return nil, fmt.Errorf("object is invalid %v of type %T", obj, obj)
}
// Get all properties
var err error
r.properties, r.propertyMethods, err = getProperties(r.Desc(), rv)
if err != nil {
return nil, err
}
// Get all methods
r.chainMethods, err = getChainMethods(r.Desc(), rv, r.propertyMethods)
if err != nil {
return nil, err
}
for k, v := range chainMethods {
r.chainMethods[k] = v
}
return r, nil
}
// Get properties from a struct and populate properties and propertyMethods maps
// Recurses up anonymous fields.
func getProperties(
desc string,
rv reflect.Value,
) (
map[string]reflect.Value,
map[string]reflect.Value,
error,
) {
if rv.Kind() != reflect.Ptr {
return nil, nil, errors.New("cannot get properties of non pointer value")
}
element := rv.Elem()
if !element.IsValid() {
return nil, nil, errors.New("cannot get properties of nil pointer")
}
rStructType := element.Type()
if rStructType.Kind() != reflect.Struct {
return nil, nil, errors.New("cannot get properties of non struct")
}
properties := make(map[string]reflect.Value, rStructType.NumField())
propertyMethods := make(map[string]reflect.Value)
for i := 0; i < rStructType.NumField(); i++ {
property := rStructType.Field(i)
if property.Anonymous {
// Recursively get properties from anon fields
anonValue := reflect.Indirect(rv).Field(i)
if anonValue.Kind() != reflect.Ptr && anonValue.CanAddr() {
anonValue = anonValue.Addr()
}
if anonValue.Kind() == reflect.Ptr && anonValue.IsNil() {
// Skip nil fields
continue
}
props, propMethods, err := getProperties(fmt.Sprintf("%s.%s", desc, property.Name), anonValue)
if err != nil {
return nil, nil, err
}
// Update local maps
for k, v := range props {
if _, ok := properties[k]; !ok {
properties[k] = v
}
}
for k, v := range propMethods {
if _, ok := propertyMethods[k]; !ok {
propertyMethods[k] = v
}
}
continue
}
methodName := property.Tag.Get("tick")
if methodName != "" {
// Property is set via a property method.
method := rv.MethodByName(methodName)
if !method.IsValid() && rv.CanAddr() {
method = rv.Addr().MethodByName(methodName)
}
if method.IsValid() {
propertyMethods[methodName] = method
} else {
return nil, nil, fmt.Errorf("referenced method %s for type %s is invalid", methodName, desc)
}
} else {
// Property is set directly via reflection.
field := reflect.Indirect(rv).FieldByName(property.Name)
if field.IsValid() && field.CanSet() {
properties[property.Name] = field
}
}
}
return properties, propertyMethods, nil
}
func getChainMethods(desc string, rv reflect.Value, propertyMethods map[string]reflect.Value) (map[string]reflect.Value, error) {
if rv.Kind() != reflect.Ptr {
return nil, errors.New("cannot get chain methods of non pointer")
}
element := rv.Elem()
if !element.IsValid() {
return nil, errors.New("cannot get chain methods of nil pointer")
}
// Find all methods on value
rRecvType := rv.Type()
chainMethods := make(map[string]reflect.Value, rRecvType.NumMethod())
for i := 0; i < rRecvType.NumMethod(); i++ {
method := rRecvType.Method(i)
if !ast.IsExported(method.Name) {
continue
}
if !rv.MethodByName(method.Name).IsValid() {
return nil, fmt.Errorf("invalid method %s on type %s", method.Name, desc)
}
if _, exists := propertyMethods[method.Name]; !exists {
chainMethods[method.Name] = rv.MethodByName(method.Name)
}
}
// Find all methods from anonymous fields.
rStructType := element.Type()
for i := 0; i < rStructType.NumField(); i++ {
field := rStructType.Field(i)
if field.Anonymous {
anonValue := element.Field(i)
if anonValue.Kind() != reflect.Ptr && anonValue.CanAddr() {
anonValue = anonValue.Addr()
}
anonChainMethods, err := getChainMethods(fmt.Sprintf("%s.%s", desc, field.Name), anonValue, propertyMethods)
if err != nil {
return nil, err
}
for k, v := range anonChainMethods {
if _, exists := chainMethods[k]; !exists {
chainMethods[k] = v
}
}
}
}
return chainMethods, nil
}
func (r *ReflectionDescriber) Desc() string {
return fmt.Sprintf("%T", r.obj)
}
// Using reflection check if the object has the method or field.
// A field is a valid method because we can set it via reflection too.
func (r *ReflectionDescriber) HasChainMethod(name string) bool {
name = capilatizeFirst(name)
_, ok := r.chainMethods[name]
return ok
}
func (r *ReflectionDescriber) CallChainMethod(name string, args ...interface{}) (interface{}, error) {
// Check for a method and call it
name = capilatizeFirst(name)
if method, ok := r.chainMethods[name]; ok {
return callMethodReflection(method, args)
}
return nil, fmt.Errorf("unknown method %s on %T", name, r.obj)
}
// Using reflection check if the object has a field with the property name.
func (r *ReflectionDescriber) HasProperty(name string) bool {
name = capilatizeFirst(name)
_, ok := r.propertyMethods[name]
if ok {
return ok
}
_, ok = r.properties[name]
return ok
}
func (r *ReflectionDescriber) Property(name string) interface{} {
// Properties set by property methods cannot be read
name = capilatizeFirst(name)
property := r.properties[name]
return property.Interface()
}
func (r *ReflectionDescriber) SetProperty(name string, values ...interface{}) (interface{}, error) {
name = capilatizeFirst(name)
propertyMethod, ok := r.propertyMethods[name]
if ok {
return callMethodReflection(propertyMethod, values)
} else {
if len(values) == 1 {
property, ok := r.properties[name]
if ok {
v := reflect.ValueOf(values[0])
property.Set(v)
return r.obj, nil
}
} else {
return nil, fmt.Errorf("too many arguments to set property %s on %T", name, r.obj)
}
}
return nil, fmt.Errorf("no property %s on %T", name, r.obj)
}
func callMethodReflection(method reflect.Value, args []interface{}) (interface{}, error) {
rargs := make([]reflect.Value, len(args))
for i, arg := range args {
rargs[i] = reflect.ValueOf(arg)
}
ret := method.Call(rargs)
if l := len(ret); l == 1 {
return ret[0].Interface(), nil
} else if l == 2 {
if i := ret[1].Interface(); i != nil {
if err, ok := i.(error); !ok {
return nil, fmt.Errorf("second return value form function must be an 'error', got %T", i)
} else {
return nil, err
}
} else {
return ret[0].Interface(), nil
}
}
return nil, fmt.Errorf("function must return a single value or (interface{}, error)")
}
// Capilatizes the first rune in the string
func capilatizeFirst(s string) string {
r, n := utf8.DecodeRuneInString(s)
s = string(unicode.ToUpper(r)) + s[n:]
return s
}
// Resolve all identifiers immediately in the tree with their value from the scope.
// This operation is performed in place.
// Panics if the scope value does not exist or if the value cannot be expressed as a literal.
func resolveIdents(n Node, scope *Scope) Node {
switch node := n.(type) {
case *IdentifierNode:
v, err := scope.Get(node.Ident)
if err != nil {
panic(err)
}
return valueToLiteralNode(node.position, v)
case *UnaryNode:
node.Node = resolveIdents(node.Node, scope)
case *BinaryNode:
node.Left = resolveIdents(node.Left, scope)
node.Right = resolveIdents(node.Right, scope)
case *FunctionNode:
for i, arg := range node.Args {
node.Args[i] = resolveIdents(arg, scope)
}
case *ListNode:
for i, n := range node.Nodes {
node.Nodes[i] = resolveIdents(n, scope)
}
}
return n
}
// Convert raw value to literal node, for all supported basic types.
func valueToLiteralNode(p position, v interface{}) Node {
switch value := v.(type) {
case bool:
return &BoolNode{
position: p,
Bool: value,
}
case int64:
return &NumberNode{
position: p,
IsInt: true,
Int64: value,
}
case float64:
return &NumberNode{
position: p,
IsFloat: true,
Float64: value,
}
case time.Duration:
return &DurationNode{
position: p,
Dur: value,
}
case string:
return &StringNode{
position: p,
Literal: value,
}
case *regexp.Regexp:
return &RegexNode{
position: p,
Regex: value,
}
default:
panic(errorf(p, "unsupported literal type %T", v))
}
}