/
util.go
211 lines (193 loc) · 4.32 KB
/
util.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package eval
import (
"reflect"
"regexp"
"github.com/alecthomas/participle/lexer"
)
func inArray(value interface{}, array []interface{}) bool {
return arrayOp(value, array, true)
}
func notInArray(value interface{}, array []interface{}) bool {
return arrayOp(value, array, false)
}
func arrayOp(value interface{}, array []interface{}, in bool) bool {
for _, rhs := range array {
rhs = coerceIntegers(rhs)
if reflect.DeepEqual(value, rhs) {
return in
}
}
return !in
}
func stringCompare(op string, lhs, rhs string, pos lexer.Position) (bool, error) {
switch op {
case "==":
return lhs == rhs, nil
case "!=":
return lhs != rhs, nil
case "<":
return lhs < rhs, nil
case ">":
return lhs > rhs, nil
case "<=":
return lhs <= rhs, nil
case ">=":
return lhs >= rhs, nil
case "=~", "!~":
re, err := regexp.Compile(rhs)
if err != nil {
return false, lexer.Errorf(pos, `failed to parse regexp "%s" for string match using %s`, rhs, op)
}
match := re.MatchString(lhs)
if op == "=~" {
return match, nil
}
return !match, nil
default:
return false, lexer.Errorf(pos, "unsupported operator %s for string comparison", op)
}
}
func uintCompare(op string, lhs, rhs uint64, pos lexer.Position) (bool, error) {
switch op {
case "==":
return lhs == rhs, nil
case "!=":
return lhs != rhs, nil
case "<":
return lhs < rhs, nil
case ">":
return lhs > rhs, nil
case "<=":
return lhs <= rhs, nil
case ">=":
return lhs >= rhs, nil
default:
return false, lexer.Errorf(pos, "unsupported operator %s for integer comparison", op)
}
}
func intCompare(op string, lhs, rhs int64, pos lexer.Position) (bool, error) {
switch op {
case "==":
return lhs == rhs, nil
case "!=":
return lhs != rhs, nil
case "<":
return lhs < rhs, nil
case ">":
return lhs > rhs, nil
case "<=":
return lhs <= rhs, nil
case ">=":
return lhs >= rhs, nil
default:
return false, lexer.Errorf(pos, "unsupported operator %s for integer comparison", op)
}
}
func uintBinaryOp(op string, lhs, rhs uint64, pos lexer.Position) (uint64, error) {
switch op {
case "&":
return lhs & rhs, nil
case "|":
return lhs | rhs, nil
case "^":
return lhs ^ rhs, nil
default:
return 0, lexer.Errorf(pos, "unsupported integer binary operator %s", op)
}
}
func intBinaryOp(op string, lhs, rhs int64, pos lexer.Position) (int64, error) {
switch op {
case "&":
return lhs & rhs, nil
case "|":
return lhs | rhs, nil
case "^":
return lhs ^ rhs, nil
default:
return 0, lexer.Errorf(pos, "unsupported integer binary operator %s", op)
}
}
func stringBinaryOp(op string, lhs, rhs string, pos lexer.Position) (string, error) {
switch op {
case "+":
return lhs + rhs, nil
default:
return "", lexer.Errorf(pos, "unsupported string binary operator %s", op)
}
}
type coerceFunc func(value interface{}) interface{}
func coerceIntegers(value interface{}) interface{} {
switch value := value.(type) {
case int:
return int64(value)
case int16:
return int64(value)
case int32:
return int64(value)
case uint:
return uint64(value)
case uint16:
return uint64(value)
case uint32:
return uint64(value)
}
return value
}
func coerceArrays(value interface{}) interface{} {
var to []interface{}
switch from := value.(type) {
case []int:
for _, v := range from {
to = append(to, int64(v))
}
case []int16:
for _, v := range from {
to = append(to, int64(v))
}
case []int32:
for _, v := range from {
to = append(to, int64(v))
}
case []int64:
for _, v := range from {
to = append(to, v)
}
case []uint:
for _, v := range from {
to = append(to, uint64(v))
}
case []uint16:
for _, v := range from {
to = append(to, uint64(v))
}
case []uint32:
for _, v := range from {
to = append(to, uint64(v))
}
case []uint64:
for _, v := range from {
to = append(to, v)
}
case []string:
for _, v := range from {
to = append(to, v)
}
default:
return value
}
return to
}
func coerceValues(value interface{}) interface{} {
coerceFns := []coerceFunc{
coerceIntegers,
coerceArrays,
}
for _, fn := range coerceFns {
value = fn(value)
}
return value
}