forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_function_node.go
246 lines (199 loc) · 6.86 KB
/
eval_function_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
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
package stateful
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/influxdata/kapacitor/tick/ast"
)
type EvalFunctionNode struct {
funcName string
argsEvaluators []NodeEvaluator
}
func NewEvalFunctionNode(funcNode *ast.FunctionNode) (*EvalFunctionNode, error) {
evalFuncNode := &EvalFunctionNode{
funcName: funcNode.Func,
}
evalFuncNode.argsEvaluators = make([]NodeEvaluator, 0, len(funcNode.Args))
for i, argNode := range funcNode.Args {
argEvaluator, err := createNodeEvaluator(argNode)
if err != nil {
return nil, fmt.Errorf("Failed to handle %v argument: %v", i+1, err)
}
evalFuncNode.argsEvaluators = append(evalFuncNode.argsEvaluators, argEvaluator)
}
return evalFuncNode, nil
}
func (n *EvalFunctionNode) Type(scope ReadOnlyScope) (ast.ValueType, error) {
f := lookupFunc(n.funcName, builtinFuncs, scope)
if f == nil {
return ast.InvalidType, fmt.Errorf("undefined function: %q", n.funcName)
}
signature := f.Signature()
domain := Domain{}
for i, argEvaluator := range n.argsEvaluators {
t, err := argEvaluator.Type(scope)
if err != nil {
return ast.InvalidType, fmt.Errorf("Failed to handle %v argument: %v", i+1, err)
}
domain[i] = t
}
if gotLen, expLen := len(n.argsEvaluators), len(domain); gotLen > expLen {
return ast.InvalidType, ErrWrongFuncSignature{Name: n.funcName, DomainProvided: domain, Func: f}
}
retType, ok := signature[domain]
if !ok {
return ast.InvalidType, ErrWrongFuncSignature{Name: n.funcName, DomainProvided: domain, Func: f}
}
return retType, nil
}
func (n *EvalFunctionNode) IsDynamic() bool {
return true
}
// callFunction - core method for evaluating function where all NodeEvaluator methods should use
func (n *EvalFunctionNode) callFunction(scope *Scope, executionState ExecutionState) (interface{}, error) {
args := make([]interface{}, 0, len(n.argsEvaluators))
for i, argEvaluator := range n.argsEvaluators {
value, err := eval(argEvaluator, scope, executionState)
if err != nil {
return nil, fmt.Errorf("Failed to handle %v argument: %v", i+1, err)
}
args = append(args, value)
}
f := lookupFunc(n.funcName, executionState.Funcs, scope)
if f == nil {
return ast.InvalidType, fmt.Errorf("undefined function: %q", n.funcName)
}
ret, err := f.Call(args...)
if err != nil {
return nil, fmt.Errorf("error calling %q: %s", n.funcName, err)
}
return ret, nil
}
func (n *EvalFunctionNode) EvalRegex(scope *Scope, executionState ExecutionState) (*regexp.Regexp, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return nil, err
}
if regexValue, isRegex := refValue.(*regexp.Regexp); isRegex {
return regexValue, nil
}
return nil, ErrTypeGuardFailed{RequestedType: ast.TRegex, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalTime(scope *Scope, executionState ExecutionState) (time.Time, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return time.Time{}, err
}
if timeValue, isTime := refValue.(time.Time); isTime {
return timeValue, nil
}
return time.Time{}, ErrTypeGuardFailed{RequestedType: ast.TTime, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalDuration(scope *Scope, executionState ExecutionState) (time.Duration, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return 0, err
}
if durValue, isDuration := refValue.(time.Duration); isDuration {
return durValue, nil
}
return 0, ErrTypeGuardFailed{RequestedType: ast.TDuration, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalString(scope *Scope, executionState ExecutionState) (string, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return "", err
}
if stringValue, isString := refValue.(string); isString {
return stringValue, nil
}
return "", ErrTypeGuardFailed{RequestedType: ast.TString, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalFloat(scope *Scope, executionState ExecutionState) (float64, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return float64(0), err
}
if float64Value, isFloat64 := refValue.(float64); isFloat64 {
return float64Value, nil
}
return float64(0), ErrTypeGuardFailed{RequestedType: ast.TFloat, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalInt(scope *Scope, executionState ExecutionState) (int64, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return int64(0), err
}
if int64Value, isInt64 := refValue.(int64); isInt64 {
return int64Value, nil
}
return int64(0), ErrTypeGuardFailed{RequestedType: ast.TInt, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalBool(scope *Scope, executionState ExecutionState) (bool, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return false, err
}
if boolValue, isBool := refValue.(bool); isBool {
return boolValue, nil
}
return false, ErrTypeGuardFailed{RequestedType: ast.TBool, ActualType: ast.TypeOf(refValue)}
}
func (n *EvalFunctionNode) EvalMissing(scope *Scope, executionState ExecutionState) (*ast.Missing, error) {
refValue, err := n.callFunction(scope, executionState)
if err != nil {
return nil, err
}
if missingValue, isMissing := refValue.(*ast.Missing); isMissing {
return missingValue, nil
}
return nil, ErrTypeGuardFailed{RequestedType: ast.TMissing, ActualType: ast.TypeOf(refValue)}
}
// eval - generic evaluation until we have reflection/introspection capabillities so we can know the type of args
// and return type, we can remove this entirely
func eval(n NodeEvaluator, scope *Scope, executionState ExecutionState) (interface{}, error) {
retType, err := n.Type(scope)
if err != nil {
return nil, err
}
switch retType {
case ast.TBool:
return n.EvalBool(scope, executionState)
case ast.TInt:
return n.EvalInt(scope, executionState)
case ast.TFloat:
return n.EvalFloat(scope, executionState)
case ast.TString:
return n.EvalString(scope, executionState)
case ast.TRegex:
return n.EvalRegex(scope, executionState)
case ast.TTime:
return n.EvalTime(scope, executionState)
case ast.TDuration:
return n.EvalDuration(scope, executionState)
case ast.TMissing:
v, err := n.EvalMissing(scope, executionState)
if err != nil && !strings.Contains(err.Error(), "missing value") {
return v, err
}
return v, nil
default:
return nil, fmt.Errorf("function arg expression returned unexpected type %s", retType)
}
}
func lookupFunc(name string, funcs Funcs, scope ReadOnlyScope) Func {
f := funcs[name]
if f != nil {
return f
}
df := scope.DynamicFunc(name)
if df != nil {
return df
}
// Return nil here explicitly so its a nil Func not nil *DynamicFunc
// returning scope.DynamicFunc(name) caused nil check in callFunction
// f == nil to evaluate to false even though f was nil. This was weird
// enough that I felt it warranted a comment.
return nil
}