forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_result.go
67 lines (65 loc) · 1.61 KB
/
feature_result.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
package growthbook
// FeatureResult is the result of evaluating a feature.
type FeatureResult struct {
Value FeatureValue
Source FeatureResultSource
On bool
Off bool
RuleID string
Experiment *Experiment
ExperimentResult *Result
}
// 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":
on, ok := jsonBool(v, "FeatureResult", "on")
if !ok {
return nil
}
result.On = on
case "off":
off, ok := jsonBool(v, "FeatureResult", "off")
if !ok {
return nil
}
result.Off = off
case "source":
source, ok := jsonString(v, "FeatureResult", "source")
if !ok {
return nil
}
result.Source = ParseFeatureResultSource(source)
case "experiment":
tmp, ok := v.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", "FeatureResult", "experiment")
continue
}
experiment := BuildExperiment(tmp)
if experiment == nil {
return nil
}
result.Experiment = experiment
case "experimentResult":
tmp, ok := v.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", "FeatureResult", "experimentResult")
return nil
}
experimentResult := BuildResult(tmp)
if experimentResult == nil {
return nil
}
result.ExperimentResult = experimentResult
default:
logWarn("Unknown key in JSON data", "FeatureResult", k)
}
}
return &result
}