forked from launchdarkly/go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
212 lines (175 loc) · 5.41 KB
/
flag.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
package ldclient
import (
"crypto/sha1"
"encoding/hex"
"io"
"reflect"
"strconv"
)
type FeatureFlag struct {
Key string `json:"key" bson:"key"`
Version int `json:"version" bson:"version"`
On bool `json:"on" bson:"on"`
Salt string `json:"salt" bson:"salt"`
Sel string `json:"sel" bson:"sel"`
Targets []Target `json:"targets" bson:"targets"`
Rules []Rule `json:"rules" bson:"rules"`
Fallthrough Rule `json:"fallthrough" bson:"fallthrough"`
OffVariation *int `json:"offVariation" bson:"offVariation"`
Variations []interface{} `json:"variations" bson:"variations"`
}
// Expresses a set of AND-ed matching conditions for a user, along with
// either the fixed variation or percent rollout to serve if the conditions
// match.
// Invariant: one of the variation or rollout must be non-nil.
type Rule struct {
Clauses []Clause `json:"clauses,omitempty" bson:"clauses,omitempty"`
Variation *int `json:"variation,omitempty" bson:"variation,omitempty"`
Rollout *Rollout `json:"rollout,omitempty" bson:"rollout,omitempty"`
}
type Rollout struct {
Variations []WeightedVariation `json:"variations,omitempty" bson:"variations"`
BucketBy *string `json:"bucketBy,omitempty" bson:"bucketBy,omitempty"`
}
type Clause struct {
Attribute string `json:"attribute" bson:"attribute"`
Op Operator `json:"op" bson:"op"`
Values []interface{} `json:"values" bson:"values"` // An array, interpreted as an OR of values
Negate bool `json:"negate" bson:"negate"`
}
type WeightedVariation struct {
Variation int `json:"variation" bson:"variation"`
Weight int `json:"weight" bson:"weight"` // Ranges from 0 to 100000
}
type Target struct {
Values []string `json:"values" bson:"values"`
Variation int `json:"variation" bson:"variation"`
}
// An explanation is either a target or a rule
type Explanation struct {
Kind string `json:"kind" bson:"kind"`
*Target
*Rule
}
func bucketUser(user User, key, attr, salt string) float32 {
uValue, pass := user.valueOf(attr)
if idHash, ok := uValue.(string); pass || !ok {
return 0
} else {
if user.Secondary != nil {
idHash = idHash + "." + *user.Secondary
}
h := sha1.New()
io.WriteString(h, key+"."+salt+"."+idHash)
hash := hex.EncodeToString(h.Sum(nil))[:15]
intVal, _ := strconv.ParseInt(hash, 16, 64)
bucket := float32(intVal) / long_scale
return bucket
}
}
func (f FeatureFlag) EvaluateExplain(user User) (interface{}, *Explanation) {
index, explanation := f.evaluateExplainIndex(user)
if index == nil || *index >= len(f.Variations) {
return nil, explanation
} else {
return f.Variations[*index], explanation
}
}
func (f FeatureFlag) evaluateExplainIndex(user User) (*int, *Explanation) {
// TODO: The toggle algorithm should check the kill switch. We won't check it here
// so we can potentially compute an explanation even if the kill switch is hit
if user.Key == nil {
return f.OffVariation, nil
}
// Check to see if targets match
for _, target := range f.Targets {
for _, value := range target.Values {
if value == *user.Key {
explanation := Explanation{Kind: "target", Target: &target}
return &target.Variation, &explanation
}
}
}
// Now walk through the rules and see if any match
for _, rule := range f.Rules {
if rule.matchesUser(user) {
variation := rule.variationIndexForUser(user, f.Key, f.Salt)
if variation == nil {
return f.OffVariation, nil // TODO should this continue, or return the off variation?
} else {
explanation := Explanation{Kind: "rule", Rule: &rule}
return variation, &explanation
}
}
}
// Walk through the fallthrough and see if it matches
variation := f.Fallthrough.variationIndexForUser(user, f.Key, f.Salt)
if variation == nil {
return f.OffVariation, nil
} else {
explanation := Explanation{Kind: "fallthrough", Rule: &f.Fallthrough}
return variation, &explanation
}
}
func (r Rule) matchesUser(user User) bool {
for _, clause := range r.Clauses {
if !clause.matchesUser(user) {
return false
}
}
return true
}
func (c Clause) matchesUser(user User) bool {
uValue, pass := user.valueOf(c.Attribute)
if pass {
return false
}
matchFn := operatorFn(c.Op)
val := reflect.ValueOf(uValue)
// If the user value is an array or slice,
// see if the intersection is non-empty. If so,
// this clause matches
if val.Kind() == reflect.Array || val.Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
if matchAny(matchFn, val.Index(i).Interface(), c.Values) {
return c.maybeNegate(true)
}
}
return c.maybeNegate(false)
}
return c.maybeNegate(matchAny(matchFn, uValue, c.Values))
}
func (c Clause) maybeNegate(b bool) bool {
if c.Negate {
return !b
} else {
return b
}
}
func matchAny(fn opFn, value interface{}, values []interface{}) bool {
for _, v := range values {
if fn(value, v) {
return true
}
}
return false
}
func (r Rule) variationIndexForUser(user User, key, salt string) *int {
if r.Variation != nil {
return r.Variation
} else if r.Rollout != nil {
bucketBy := "key"
if r.Rollout.BucketBy != nil {
bucketBy = *r.Rollout.BucketBy
}
var bucket = bucketUser(user, key, bucketBy, salt)
var sum float32 = 0.0
for _, wv := range r.Rollout.Variations {
sum += float32(wv.Weight) / 100000.0
if bucket < sum {
return &wv.Variation
}
}
}
return nil
}