forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
277 lines (265 loc) · 6.8 KB
/
json.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
package growthbook
import "encoding/json"
// All of these functions build values of particular types from
// representations as JSON objects. These functions are useful both
// for testing and for user creation of GrowthBook objects from JSON
// configuration data shared with GrowthBook SDK implementations in
// other languages, all of which use JSON as a common configuration
// format.
// BuildExperimentResult creates an ExperimentResult value from a JSON
// object represented as a Go map.
func BuildExperimentResult(dict map[string]interface{}) *ExperimentResult {
res := ExperimentResult{}
for k, v := range dict {
switch k {
case "inExperiment":
tmp, ok := v.(bool)
if !ok {
logError(ErrJSONInvalidType, "ExperimentResult", "inExperiment")
continue
}
res.InExperiment = tmp
case "variationId":
tmp, ok := v.(float64)
if !ok {
logError(ErrJSONInvalidType, "ExperimentResult", "variationId")
continue
}
res.VariationID = int(tmp)
case "value":
res.Value = v
case "hashAttribute":
tmp, ok := v.(string)
if !ok {
logError(ErrJSONInvalidType, "ExperimentResult", "hashAttribute")
continue
}
res.HashAttribute = tmp
case "hashValue":
tmp, ok := v.(string)
if !ok {
logError(ErrJSONInvalidType, "ExperimentResult", "hashValue")
continue
}
res.HashValue = tmp
default:
logWarn(WarnJSONUnknownKey, "ExperimentResult", k)
}
}
return &res
}
// BuildFeatureValues creates a FeatureValue array from a generic JSON
// value.
func BuildFeatureValues(val interface{}) []FeatureValue {
vals, ok := val.([]interface{})
if !ok {
logError(ErrJSONInvalidType, "FeatureValue")
return nil
}
result := make([]FeatureValue, len(vals))
for i, v := range vals {
tmp, ok := v.(FeatureValue)
if !ok {
logError(ErrJSONInvalidType, "FeatureValue")
return nil
}
result[i] = tmp
}
return result
}
// ParseFeatureMap creates a FeatureMap value from raw JSON input.
func ParseFeatureMap(data []byte) FeatureMap {
dict := map[string]interface{}{}
err := json.Unmarshal(data, &dict)
if err != nil {
logError(ErrJSONFailedToParse, "FeatureMap")
return nil
}
return BuildFeatureMap(dict)
}
// BuildFeatureMap creates a FeatureMap value from a JSON object
// represented as a Go map.
func BuildFeatureMap(dict map[string]interface{}) FeatureMap {
fmap := FeatureMap{}
for k, v := range dict {
fmap[k] = BuildFeature(v)
}
return fmap
}
// ParseFeature creates a single Feature value from raw JSON input.
func ParseFeature(data []byte) *Feature {
dict := map[string]interface{}{}
err := json.Unmarshal(data, &dict)
if err != nil {
logError(ErrJSONFailedToParse, "Feature")
return nil
}
return BuildFeature(dict)
}
// BuildFeature creates a Feature value from a generic JSON value.
func BuildFeature(val interface{}) *Feature {
feature := Feature{}
dict, ok := val.(map[string]interface{})
if !ok {
logError(ErrJSONInvalidType, "Feature")
return &feature
}
defaultValue, ok := dict["defaultValue"]
if ok {
feature.DefaultValue = defaultValue
}
rules, ok := dict["rules"]
if ok {
rulesArray, ok := rules.([]interface{})
if !ok {
logError(ErrJSONInvalidType, "Feature")
return &feature
}
feature.Rules = make([]*FeatureRule, len(rulesArray))
for i := range rulesArray {
feature.Rules[i] = BuildFeatureRule(rulesArray[i])
}
}
return &feature
}
// 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(ErrJSONInvalidType, "FeatureRule")
return &rule
}
KeyLoop:
for k, v := range dict {
switch k {
case "condition":
condmap, ok := v.(map[string]interface{})
if !ok {
logError(ErrJSONInvalidType, "FeatureRule", "condition")
continue
}
rule.Condition = BuildCondition(condmap)
case "coverage":
tmp, ok := v.(float64)
if !ok {
logError(ErrJSONInvalidType, "FeatureRule", "coverage")
continue
}
rule.Coverage = &tmp
case "force":
rule.Force = v
case "variations":
rule.Variations = BuildFeatureValues(v)
case "key":
tmp, ok := v.(string)
if !ok {
logError(ErrJSONInvalidType, "FeatureRule", "key")
continue
}
rule.TrackingKey = &tmp
case "weights":
vals, ok := v.([]interface{})
if !ok {
logError(ErrJSONInvalidType, "FeatureRule", "weights")
continue
}
weights := make([]float64, len(vals))
for i := range vals {
tmp, ok := vals[i].(float64)
if !ok {
logError(ErrJSONInvalidType, "FeatureRule", "weights")
continue KeyLoop
}
weights[i] = tmp
}
rule.Weights = weights
case "namespace":
rule.Namespace = BuildNamespace(v)
case "hashAttribute":
tmp, ok := v.(string)
if !ok {
logError(ErrJSONInvalidType, "FeatureRule", "hashAttribute")
continue
}
rule.HashAttribute = &tmp
default:
logWarn(WarnJSONUnknownKey, "FeatureRule", k)
}
}
return &rule
}
// ParseNamespace creates a Namespace value from raw JSON input.
func ParseNamespace(data []byte) *Namespace {
array := []interface{}{}
err := json.Unmarshal(data, &array)
if err != nil {
logError(ErrJSONFailedToParse, "Namespace")
return nil
}
return BuildNamespace(array)
}
// BuildNamespace creates a Namespace value from a generic JSON value.
func BuildNamespace(val interface{}) *Namespace {
array, ok := val.([]interface{})
if !ok || len(array) != 3 {
return nil
}
id, ok1 := array[0].(string)
start, ok2 := array[1].(float64)
end, ok3 := array[2].(float64)
if !ok1 || !ok2 || !ok3 {
return nil
}
return &Namespace{id, start, end}
}
// BuildFeatureResult creates an FeatureResult value from a JSON
// object represented as a Go map.
func BuildFeatureResult(dict map[string]interface{}) *FeatureResult {
result := FeatureResult{}
for k, v := range dict {
switch k {
case "value":
result.Value = v
case "on":
tmp, ok := v.(bool)
if !ok {
logError(ErrJSONInvalidType, "FeatureResult", "on")
continue
}
result.On = tmp
case "off":
tmp, ok := v.(bool)
if !ok {
logError(ErrJSONInvalidType, "FeatureResult", "off")
continue
}
result.Off = tmp
case "source":
tmp, ok := v.(string)
if !ok {
logError(ErrJSONInvalidType, "FeatureResult", "source")
continue
}
result.Source = ParseFeatureResultSource(tmp)
case "experiment":
tmp, ok := v.(map[string]interface{})
if !ok {
logError(ErrJSONInvalidType, "FeatureResult", "experiment")
continue
}
result.Experiment = BuildExperiment(tmp)
case "experimentResult":
tmp, ok := v.(map[string]interface{})
if !ok {
logError(ErrJSONInvalidType, "FeatureResult", "experimentResult")
continue
}
result.ExperimentResult = BuildExperimentResult(tmp)
default:
logWarn(WarnJSONUnknownKey, "FeatureResult", k)
}
}
return &result
}