/
comparator.go
481 lines (419 loc) · 9.75 KB
/
comparator.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
package dtree
import (
"errors"
"hash/crc32"
"math/rand"
"regexp"
"strings"
"time"
)
func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}
// FallbackType the fallback value (by default = "fallback"), can be overrided
var FallbackType = "fallback"
// ErrOperator : unknow operator
var ErrOperator = errors.New("unknow operator")
// ErrBadType : Types between request and Tree are different, so we are unable to compare them
var ErrBadType = errors.New("types are different")
// ErrNotSupportedType : Type in request are not supported
var ErrNotSupportedType = errors.New("type not supported")
// ErrNoNode : No Node was sent
var ErrNoNode = errors.New("Node is nil")
// ErrNoParentNode : Node has no parent
var ErrNoParentNode = errors.New("Node has no parent")
func compare(requests map[string]interface{}, jsonValue interface{}, node *Tree, config *TreeOptions) (*Tree, error) {
if node == nil {
return nil, ErrNoNode
}
// Check if it is a fallback value
if v, ok := node.Value.(string); (ok && v == FallbackType) || len(node.Operator) == 0 {
return node, nil
}
// if the operators override an existing one defined on isExistingOperator, we check first the opeators else we do it in the default
if config != nil && config.OverrideExistingOperator {
if r, err := runOperators(requests, node, config); err != ErrOperator {
return r, err
}
}
switch node.Operator {
case "eq", "==":
return eq(jsonValue, node)
case "ne", "!=":
b, err := eq(jsonValue, node)
if b == nil {
return node, err
}
return nil, err
case "gt", ">":
return gt(jsonValue, node)
case "lt", "<":
return lt(jsonValue, node)
case "gte", ">=":
return gte(jsonValue, node)
case "lte", "<=":
return lte(jsonValue, node)
case "contains":
return contains(jsonValue, node)
case "count":
return count(jsonValue, node)
case "regexp":
return regex(jsonValue, node)
case "percent", "%":
return percentage(jsonValue, node)
case "ab":
return abTest(jsonValue, node)
default:
if config != nil && config.OverrideExistingOperator == false {
if r, err := runOperators(requests, node, config); err != ErrOperator {
return r, err
}
}
return nil, ErrOperator
}
}
func runOperators(requests map[string]interface{}, node *Tree, config *TreeOptions) (*Tree, error) {
if config != nil && config.Operators != nil {
for k, f := range config.Operators {
if k == node.Operator {
return f(requests, node)
}
}
}
return nil, ErrOperator
}
func isExistingOperator(name string) bool {
switch name {
case "eq", "==", "ne", "!=", "gt", ">", "lt", "<", "gte", ">=", "lte", "<=", "contains", "count", "regexp", "percent", "%":
return true
default:
return false
}
}
// eq check if v1 == v2 (only for string, float64, bool, []interface{} (interface{} being a string or float64))
func eq(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case float64:
switch t2 := v2.Value.(type) {
case float64:
if t1 == t2 {
return v2, nil
}
return nil, nil
case []interface{}:
for _, v := range t2 {
if t2, ok := v.(float64); ok {
if t1 == t2 {
return v2, nil
}
}
}
return nil, nil
default:
return nil, ErrBadType
}
case string:
switch t2 := v2.Value.(type) {
case string:
if t1 == t2 {
return v2, nil
}
return nil, nil
case []interface{}:
for _, v := range t2 {
if t2, ok := v.(string); ok {
if t1 == t2 {
return v2, nil
}
}
}
return nil, nil
default:
return nil, ErrBadType
}
case bool:
if t2, ok := v2.Value.(bool); ok {
if t1 == t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
case []interface{}:
for _, v := range v1.([]interface{}) {
switch tv := v.(type) {
case float64:
if t2, ok := v2.Value.(float64); ok {
if tv == t2 {
return v2, nil
}
}
if t2, ok := v2.Value.([]interface{}); ok {
for _, vs := range t2 {
if t2, ok := vs.(float64); ok {
if tv == t2 {
return v2, nil
}
}
}
}
case string:
if t2, ok := v2.Value.(string); ok {
if tv == t2 {
return v2, nil
}
}
if t2, ok := v2.Value.([]interface{}); ok {
for _, vs := range t2 {
if t2, ok := vs.(string); ok {
if tv == t2 {
return v2, nil
}
}
}
}
}
}
return nil, nil
case []string:
for _, v := range v1.([]interface{}) {
switch tv := v.(type) {
case string:
if t2, ok := v2.Value.(string); ok {
if tv == t2 {
return v2, nil
}
}
}
}
return nil, nil
case []float64:
for _, v := range v1.([]interface{}) {
switch tv := v.(type) {
case float64:
if t2, ok := v2.Value.(float64); ok {
if tv == t2 {
return v2, nil
}
}
}
}
return nil, nil
default:
return nil, ErrNotSupportedType
}
}
// gt check if v1 > v2 (only for float64 and string)
func gt(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case float64:
if t2, ok := v2.Value.(float64); ok {
if t1 > t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
case string:
if t2, ok := v2.Value.(string); ok {
if t1 > t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// lt check if v1 < v2 (only for float64 and string)
func lt(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case float64:
if t2, ok := v2.Value.(float64); ok {
if t1 < t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
case string:
if t2, ok := v2.Value.(string); ok {
if t1 < t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// gte check if v1 >= v2 (only for float64 and string)
func gte(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case float64:
if t2, ok := v2.Value.(float64); ok {
if t1 >= t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
case string:
if t2, ok := v2.Value.(string); ok {
if t1 >= t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// lte check if v1 <= v2 (only for float64 and string)
func lte(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case float64:
if t2, ok := v2.Value.(float64); ok {
if t1 <= t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
case string:
if t2, ok := v2.Value.(string); ok {
if t1 <= t2 {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// contains check if a string v1 contains a string v2
func contains(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case string:
if t2, ok := v2.Value.(string); ok {
if strings.Contains(t1, t2) {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// count check if the length of a slice v1 == (int)v2
func count(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case []interface{}:
if t2, ok := v2.Value.(float64); ok {
if len(t1) == int(t2) {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// v check if the regexp pattern provided by v2 match v1 string
func regex(v1 interface{}, v2 *Tree) (*Tree, error) {
switch t1 := v1.(type) {
case string:
if t2, ok := v2.Value.(string); ok {
matched, _ := regexp.MatchString(t2, t1)
if matched {
return v2, nil
}
return nil, nil
}
return nil, ErrBadType
default:
return nil, ErrNotSupportedType
}
}
// percentage rolls the dice, to know if it falls on one of the bucket of the percents node.
func percentage(v1 interface{}, v2 *Tree) (*Tree, error) {
if v2.GetParent() == nil {
return nil, ErrNoParentNode
}
brothersNode := v2.GetParent().GetChild()
if brothersNode == nil || len(brothersNode) == 1 {
return v2, nil
}
var fallbackNode *Tree
var percent = rand.Float64() * 100.0
var total float64
for _, node := range brothersNode {
if node.Operator == "%" || node.Operator == "percent" {
if tn, ok := node.Value.(float64); ok {
max := total + tn
if percent <= max {
return node, nil
}
total = total + tn
}
}
// search if it exist a fallback node
if tn, ok := node.Value.(string); ok && tn == FallbackType {
fallbackNode = node
}
}
// check for fallback
if fallbackNode != nil {
return fallbackNode, nil
}
return nil, nil
}
// abTest hash the value, to know if it falls on one of the bucket of the percents node.
func abTest(v1 interface{}, v2 *Tree) (*Tree, error) {
if v2.GetParent() == nil {
return nil, ErrNoParentNode
}
brothersNode := v2.GetParent().GetChild()
if brothersNode == nil || len(brothersNode) == 1 {
return v2, nil
}
var percent float64
if t1, ok := v1.(string); ok {
percent = float64(crc32Num(t1, "salt", 1000)) / 10
} else {
percent = rand.Float64() * 100.0
}
var fallbackNode *Tree
var total float64
for _, node := range brothersNode {
if node.Operator == "ab" {
if tn, ok := node.Value.(float64); ok {
max := total + tn
if percent <= max {
return node, nil
}
total = total + tn
}
}
// search if it exist a fallback node
if tn, ok := node.Value.(string); ok && tn == FallbackType {
fallbackNode = node
}
}
// check for fallback
if fallbackNode != nil {
return fallbackNode, nil
}
return nil, nil
}
func crc32Num(entityID string, salt string, totalBucketNum uint) uint {
// crc32 is good in terms of uniform distribution
// http://michiel.buddingh.eu/distribution-of-hash-values
return uint(crc32.ChecksumIEEE([]byte(salt+entityID))) % totalBucketNum
}