forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_lambda_node.go
122 lines (102 loc) · 3.17 KB
/
eval_lambda_node.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
package stateful
import (
"fmt"
"regexp"
"time"
"github.com/influxdata/kapacitor/tick/ast"
)
type EvalLambdaNode struct {
nodeEvaluator NodeEvaluator
constReturnType ast.ValueType
state ExecutionState
}
func NewEvalLambdaNode(lambda *ast.LambdaNode) (*EvalLambdaNode, error) {
nodeEvaluator, err := createNodeEvaluator(lambda.Expression)
if err != nil {
return nil, fmt.Errorf("Failed to handle node: %v", err)
}
return &EvalLambdaNode{
nodeEvaluator: nodeEvaluator,
constReturnType: getConstantNodeType(lambda.Expression),
// Create an independent state for this expression
state: CreateExecutionState(),
}, nil
}
func (n *EvalLambdaNode) Type(scope ReadOnlyScope, _ ExecutionState) (ast.ValueType, error) {
if n.constReturnType == ast.InvalidType {
// We are dynamic and we need to figure out our type
// Do NOT cache this result in n.returnType since it can change.
return n.nodeEvaluator.Type(scope, n.state)
}
return n.constReturnType, nil
}
func (n *EvalLambdaNode) IsDynamic() bool {
return n.nodeEvaluator.IsDynamic()
}
func (n *EvalLambdaNode) EvalRegex(scope *Scope, _ ExecutionState) (*regexp.Regexp, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return nil, err
}
if typ == ast.TRegex {
return n.nodeEvaluator.EvalRegex(scope, n.state)
}
return nil, ErrTypeGuardFailed{RequestedType: ast.TRegex, ActualType: typ}
}
func (n *EvalLambdaNode) EvalTime(scope *Scope, _ ExecutionState) (time.Time, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return time.Time{}, err
}
return time.Time{}, ErrTypeGuardFailed{RequestedType: ast.TTime, ActualType: typ}
}
func (n *EvalLambdaNode) EvalDuration(scope *Scope, _ ExecutionState) (time.Duration, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return 0, err
}
if typ == ast.TDuration {
return n.nodeEvaluator.EvalDuration(scope, n.state)
}
return 0, ErrTypeGuardFailed{RequestedType: ast.TDuration, ActualType: typ}
}
func (n *EvalLambdaNode) EvalString(scope *Scope, _ ExecutionState) (string, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return "", err
}
if typ == ast.TString {
return n.nodeEvaluator.EvalString(scope, n.state)
}
return "", ErrTypeGuardFailed{RequestedType: ast.TString, ActualType: typ}
}
func (n *EvalLambdaNode) EvalFloat(scope *Scope, _ ExecutionState) (float64, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return 0, err
}
if typ == ast.TFloat {
return n.nodeEvaluator.EvalFloat(scope, n.state)
}
return 0, ErrTypeGuardFailed{RequestedType: ast.TFloat, ActualType: typ}
}
func (n *EvalLambdaNode) EvalInt(scope *Scope, _ ExecutionState) (int64, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return 0, err
}
if typ == ast.TInt {
return n.nodeEvaluator.EvalInt(scope, n.state)
}
return 0, ErrTypeGuardFailed{RequestedType: ast.TInt, ActualType: typ}
}
func (n *EvalLambdaNode) EvalBool(scope *Scope, _ ExecutionState) (bool, error) {
typ, err := n.Type(scope, n.state)
if err != nil {
return false, err
}
if typ == ast.TBool {
return n.nodeEvaluator.EvalBool(scope, n.state)
}
return false, ErrTypeGuardFailed{RequestedType: ast.TBool, ActualType: typ}
}