forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
81 lines (76 loc) · 1.72 KB
/
filter.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
package growthbook
// Filter represents a filter condition for experiment mutual
// exclusion.
type Filter struct {
Attribute string
Seed string
HashVersion int
Ranges []Range
}
func jsonFilter(v interface{}, typeName string, fieldName string) *Filter {
obj, ok := v.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
attribute := ""
seed := ""
hashVersion := 0
var ranges []Range
vAttribute, atOk := obj["attribute"]
if atOk {
tmp, ok := vAttribute.(string)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
attribute = tmp
}
vSeed, seedOk := obj["seed"]
if seedOk {
tmp, ok := vSeed.(string)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
seed = tmp
}
vHashVersion, hvOk := obj["hashVersion"]
if hvOk {
tmp, ok := vHashVersion.(float64)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
vHashVersion = int(tmp)
}
vRanges, rngOk := obj["ranges"]
if rngOk {
tmp, ok := vRanges.([]interface{})
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
ranges, ok = jsonRangeArray(tmp, typeName, fieldName)
if !ok {
return nil
}
}
return &Filter{attribute, seed, hashVersion, ranges}
}
func jsonFilterArray(v interface{}, typeName string, fieldName string) ([]Filter, bool) {
vals, ok := v.([]interface{})
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil, false
}
filters := make([]Filter, len(vals))
for i := range vals {
tmp := jsonFilter(vals[i], typeName, fieldName)
if tmp == nil {
return nil, false
}
filters[i] = *tmp
}
return filters, true
}