forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
87 lines (75 loc) · 2.09 KB
/
types.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
package growthbook
import "encoding/json"
// Attributes is an arbitrary JSON object containing user and request
// attributes.
type Attributes map[string]interface{}
// FeatureMap is a map of feature objects, keyed by string feature
// IDs.
type FeatureMap map[string]*Feature
// 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("Failed parsing JSON input", "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 {
feature := BuildFeature(v)
if feature != nil {
fmap[k] = feature
}
}
return fmap
}
// ForcedVariationsMap is a map that forces an Experiment to always
// assign a specific variation. Useful for QA.
//
// Keys are the experiment key, values are the array index of the
// variation.
type ForcedVariationsMap map[string]int
// URL matching supports regular expressions or simple string matches.
type URLTargetType uint
const (
RegexURLTarget URLTargetType = iota
SimpleURLTarget = iota
)
// URL match target.
type URLTarget struct {
Type URLTargetType
Include bool
Pattern string
}
// FeatureResultSource is an enumerated type representing the source
// of a FeatureResult.
type FeatureResultSource uint
// FeatureResultSource values.
const (
UnknownResultSource FeatureResultSource = iota + 1
DefaultValueResultSource
ForceResultSource
ExperimentResultSource
OverrideResultSource
)
// ParseFeatureResultSource creates a FeatureResultSource value from
// its string representation.
func ParseFeatureResultSource(source string) FeatureResultSource {
switch source {
case "", "defaultValue":
return DefaultValueResultSource
case "force":
return ForceResultSource
case "experiment":
return ExperimentResultSource
case "override":
return OverrideResultSource
default:
return UnknownResultSource
}
}