forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_rule.go
143 lines (141 loc) · 3.13 KB
/
feature_rule.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
package growthbook
// FeatureRule overrides the default value of a Feature.
type FeatureRule struct {
ID string
Condition Condition
Force FeatureValue
Variations []FeatureValue
Weights []float64
Key string
HashAttribute string
HashVersion int
Range *Range
Coverage *float64
Namespace *Namespace
Ranges []Range
Meta []VariationMeta
Filters []Filter
Seed string
Name string
Phase string
}
// BuildFeatureRule creates an FeatureRule value from a generic JSON
// value.
func BuildFeatureRule(val interface{}) *FeatureRule {
rule := FeatureRule{}
dict, ok := val.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", "FeatureRule")
return nil
}
for k, v := range dict {
switch k {
case "id":
id, ok := jsonString(v, "FeatureRule", "id")
if !ok {
return nil
}
rule.ID = id
case "condition":
condmap, ok := v.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", "FeatureRule", "condition")
return nil
}
condition := BuildCondition(condmap)
if condition == nil {
return nil
}
rule.Condition = condition
case "force":
rule.Force = v
case "variations":
variations := BuildFeatureValues(v)
if variations == nil {
return nil
}
rule.Variations = variations
case "weights":
weights, ok := jsonFloatArray(v, "FeatureRule", "weights")
if !ok {
return nil
}
rule.Weights = weights
case "key":
key, ok := jsonString(v, "FeatureRule", "key")
if !ok {
return nil
}
rule.Key = key
case "hashAttribute":
hashAttribute, ok := jsonString(v, "FeatureRule", "hashAttribute")
if !ok {
return nil
}
rule.HashAttribute = hashAttribute
case "hashVersion":
hashVersion, ok := jsonInt(v, "FeatureRule", "hashVersion")
if !ok {
return nil
}
rule.HashVersion = hashVersion
case "range":
rng, ok := jsonRange(v, "FeatureRule", "range")
if !ok {
return nil
}
rule.Range = rng
case "coverage":
coverage, ok := jsonMaybeFloat(v, "FeatureRule", "coverage")
if !ok {
return nil
}
rule.Coverage = coverage
case "namespace":
namespace := BuildNamespace(v)
if namespace == nil {
return nil
}
rule.Namespace = namespace
case "ranges":
ranges, ok := jsonRangeArray(v, "FeatureRule", "ranges")
if !ok {
return nil
}
rule.Ranges = ranges
case "meta":
meta, ok := jsonVariationMetaArray(v, "Experiment", "meta")
if !ok {
return nil
}
rule.Meta = meta
case "filters":
filters, ok := jsonFilterArray(v, "Experiment", "filters")
if !ok {
return nil
}
rule.Filters = filters
case "seed":
seed, ok := jsonString(v, "FeatureRule", "seed")
if !ok {
return nil
}
rule.Seed = seed
case "name":
name, ok := jsonString(v, "FeatureRule", "name")
if !ok {
return nil
}
rule.Name = name
case "phase":
phase, ok := jsonString(v, "FeatureRule", "phase")
if !ok {
return nil
}
rule.Phase = phase
default:
logWarn("Unknown key in JSON data", "FeatureRule", k)
}
}
return &rule
}