This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
legacy_feature.go
205 lines (176 loc) · 4.52 KB
/
legacy_feature.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
package ldclient
import (
"crypto/sha1" // nolint:gas // just used for insecure hashing
"encoding/hex"
"io"
"reflect"
"strconv"
"time"
)
// Feature describes a Legacy (v1) feature
type Feature struct {
Name *string `json:"name"`
Key *string `json:"key"`
Kind *string `json:"kind"`
Salt *string `json:"salt"`
On *bool `json:"on"`
Variations *[]Variation `json:"variations"`
CommitDate *time.Time `json:"commitDate"`
CreationDate *time.Time `json:"creationDate"`
Version int `json:"version,omitempty"`
Deleted bool `json:"deleted,omitempty"`
}
// TargetRule describes an individual targeting rule
type TargetRule struct {
Attribute string `json:"attribute"`
Op Operator `json:"op"`
Values []interface{} `json:"values"`
}
// Variation describes what value to return for a user
type Variation struct {
Value interface{} `json:"value"`
Weight int `json:"weight"`
Targets []TargetRule `json:"targets"`
UserTarget *TargetRule `json:"userTarget,omitempty"`
}
// Evaluate returns the value of a feature for a specified user
func (f Feature) Evaluate(user User) (value interface{}, rulesPassed bool) {
value, _, rulesPassed = f.EvaluateExplain(user)
return
}
// EvaluateExplain returns the value of a feature for a specified user with an explanation of which rule was applied
func (f Feature) EvaluateExplain(user User) (value interface{}, targetMatch *TargetRule, rulesPassed bool) {
if !*f.On {
return nil, nil, true
}
param, passErr := f.paramForId(user)
if passErr {
return nil, nil, true
}
for _, variation := range *f.Variations {
target := variation.matchUser(user)
if target != nil {
return variation.Value, target, false
}
}
for _, variation := range *f.Variations {
target := variation.matchTarget(user)
if target != nil {
return variation.Value, target, false
}
}
var sum float32
for _, variation := range *f.Variations {
sum += float32(variation.Weight) / 100.0
if param < sum {
return variation.Value, nil, false
}
}
return nil, nil, true
}
func (f Feature) paramForId(user User) (float32, bool) {
if user.Key == nil {
return 0, true // without a key, this rule should pass
}
idHash := *user.Key
if user.Secondary != nil {
idHash = idHash + "." + *user.Secondary
}
h := sha1.New() // nolint:gas // just used for insecure hashing
_, _ = io.WriteString(h, *f.Key+"."+*f.Salt+"."+idHash)
hash := hex.EncodeToString(h.Sum(nil))[:15]
intVal, _ := strconv.ParseInt(hash, 16, 64)
param := float32(intVal) / longScale
return param, false
}
func (target TargetRule) matchCustom(user User) bool {
if user.Custom == nil {
return false
}
var v interface{} = (*user.Custom)[target.Attribute]
if v == nil {
return false
}
val := reflect.ValueOf(v)
if val.Kind() == reflect.Array || val.Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
if compareValues(val.Index(i).Interface(), target.Values) {
return true
}
}
return false
}
return compareValues(v, target.Values)
}
func compareValues(value interface{}, values []interface{}) bool {
if value == "" {
return false
}
for _, v := range values {
if value == v {
return true
}
}
return false
}
func (target TargetRule) matchTarget(user User) bool {
var uValue interface{}
switch target.Attribute {
case userKey:
if user.Key != nil {
uValue = *user.Key
}
case "ip":
if user.Ip != nil {
uValue = *user.Ip
}
case "country":
if user.Country != nil {
uValue = *user.Country
}
case "email":
if user.Email != nil {
uValue = *user.Email
}
case "firstName":
if user.FirstName != nil {
uValue = *user.FirstName
}
case "lastName":
if user.LastName != nil {
uValue = *user.LastName
}
case "avatar":
if user.Avatar != nil {
uValue = *user.Avatar
}
case "name":
if user.Name != nil {
uValue = *user.Name
}
case "anonymous":
if user.Anonymous != nil {
uValue = *user.Anonymous
}
default:
return target.matchCustom(user)
}
return compareValues(uValue, target.Values)
}
func (variation Variation) matchTarget(user User) *TargetRule {
for _, target := range variation.Targets {
if variation.UserTarget != nil && target.Attribute == userKey {
continue
}
if target.matchTarget(user) {
return &target
}
}
return nil
}
func (variation Variation) matchUser(user User) *TargetRule {
if variation.UserTarget != nil && variation.UserTarget.matchTarget(user) {
return variation.UserTarget
}
return nil
}