forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
67 lines (62 loc) · 1.46 KB
/
meta.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
// VariationMeta represents meta-information that can be passed
// through to tracking callbacks.
type VariationMeta struct {
Passthrough bool
Key string
Name string
}
func jsonVariationMeta(v interface{}, typeName string, fieldName string) *VariationMeta {
obj, ok := v.(map[string]interface{})
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
passthrough := false
key := ""
name := ""
vPassthrough, ptOk := obj["passthrough"]
if ptOk {
tmp, ok := vPassthrough.(bool)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
passthrough = tmp
}
vKey, keyOk := obj["key"]
if keyOk {
tmp, ok := vKey.(string)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
key = tmp
}
vName, nameOk := obj["name"]
if nameOk {
tmp, ok := vName.(string)
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil
}
name = tmp
}
return &VariationMeta{passthrough, key, name}
}
func jsonVariationMetaArray(v interface{}, typeName string, fieldName string) ([]VariationMeta, bool) {
vals, ok := v.([]interface{})
if !ok {
logError("Invalid JSON data type", typeName, fieldName)
return nil, false
}
metas := make([]VariationMeta, len(vals))
for i := range vals {
tmp := jsonVariationMeta(vals[i], typeName, fieldName)
if tmp == nil {
return nil, false
}
metas[i] = *tmp
}
return metas, true
}