...
1 package growthbook
2
3
4
5 type Filter struct {
6 Attribute string
7 Seed string
8 HashVersion int
9 Ranges []Range
10 }
11
12 func jsonFilter(v interface{}, typeName string, fieldName string) *Filter {
13 obj, ok := v.(map[string]interface{})
14 if !ok {
15 logError("Invalid JSON data type", typeName, fieldName)
16 return nil
17 }
18
19 attribute := ""
20 seed := ""
21 hashVersion := 0
22 var ranges []Range
23 vAttribute, atOk := obj["attribute"]
24 if atOk {
25 tmp, ok := vAttribute.(string)
26 if !ok {
27 logError("Invalid JSON data type", typeName, fieldName)
28 return nil
29 }
30 attribute = tmp
31 }
32 vSeed, seedOk := obj["seed"]
33 if seedOk {
34 tmp, ok := vSeed.(string)
35 if !ok {
36 logError("Invalid JSON data type", typeName, fieldName)
37 return nil
38 }
39 seed = tmp
40 }
41 vHashVersion, hvOk := obj["hashVersion"]
42 if hvOk {
43 tmp, ok := vHashVersion.(float64)
44 if !ok {
45 logError("Invalid JSON data type", typeName, fieldName)
46 return nil
47 }
48 vHashVersion = int(tmp)
49 }
50 vRanges, rngOk := obj["ranges"]
51 if rngOk {
52 tmp, ok := vRanges.([]interface{})
53 if !ok {
54 logError("Invalid JSON data type", typeName, fieldName)
55 return nil
56 }
57 ranges, ok = jsonRangeArray(tmp, typeName, fieldName)
58 if !ok {
59 return nil
60 }
61 }
62
63 return &Filter{attribute, seed, hashVersion, ranges}
64 }
65
66 func jsonFilterArray(v interface{}, typeName string, fieldName string) ([]Filter, bool) {
67 vals, ok := v.([]interface{})
68 if !ok {
69 logError("Invalid JSON data type", typeName, fieldName)
70 return nil, false
71 }
72 filters := make([]Filter, len(vals))
73 for i := range vals {
74 tmp := jsonFilter(vals[i], typeName, fieldName)
75 if tmp == nil {
76 return nil, false
77 }
78 filters[i] = *tmp
79 }
80 return filters, true
81 }
82
View as plain text