-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.go
138 lines (129 loc) · 4.31 KB
/
evaluation.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
package featbit
import (
. "github.com/featbit/featbit-go-sdk/interfaces"
"github.com/featbit/featbit-go-sdk/internal/types/data"
"github.com/featbit/featbit-go-sdk/internal/util/log"
)
const (
ExptKeyPrefix = "expt"
ReasonFlagOff = "flag off"
ReasonTargetMatch = "target match"
ReasonRuleMatch = "rule match"
ReasonFallthrough = "fall through all rules"
ReasonClientNotReady = "client not ready"
ReasonFlagNotFound = "flag not found"
ReasonWrongType = "wrong type"
ReasonUserNotSpecified = "user not specified"
ReasonError = "error in evaluation"
FlagNameUnknown = "flag Name unknown"
ThanClause = "Than"
GeClause = "BiggerEqualThan"
GtClause = "BiggerThan"
LeClause = "LessEqualThan"
LtClause = "LessThan"
EqClause = "Equal"
NeqClause = "NotEqual"
ContainsClause = "Contains"
NotContainClause = "NotContain"
IsOneOfClause = "IsOneOf"
NotOneOfClause = "NotOneOf"
StartsWithClause = "StartsWith"
EndsWithClause = "EndsWith"
IsTrueClause = "IsTrue"
IsFalseClause = "IsFalse"
MatchRegexClause = "MatchRegex"
NotMatchRegexClause = "NotMatchRegex"
IsInSegmentClause = "User is in segment"
NotInSegmentClause = "User is not in segment"
FlagJsonType = "json"
FlagBoolType = "boolean"
FlagNumericType = "number"
FlagStringType = "string"
)
type evaluator struct {
getFlag func(key string) *data.FeatureFlag
getSegment func(key string) *data.Segment
funcSlice []func(*data.FeatureFlag, *FBUser) (*evalResult, bool)
}
func newEvaluator(getFlag func(key string) *data.FeatureFlag,
getSegment func(key string) *data.Segment) *evaluator {
e := &evaluator{getFlag: getFlag, getSegment: getSegment}
fs := []func(*data.FeatureFlag, *FBUser) (*evalResult, bool){
e.matchFeatureFlagDisabledUserVariation,
e.matchTargetedUserVariation,
e.matchConditionedUserVariation,
e.matchFallThroughUserVariation,
}
e.funcSlice = fs
return e
}
func (e *evaluator) evaluate(flag *data.FeatureFlag, user *FBUser, event Event) (er *evalResult) {
defer func() {
if er.success {
log.LogInfo("FB Go SDK: User %v, Feature Flag %v, Flag Value %v", user.GetKey(), flag.Key, er.fv)
if event != nil {
eventFlag := er.toEventFlag()
event.Add(eventFlag)
}
}
}()
var ok bool
for _, f := range e.funcSlice {
er, ok = f(flag, user)
if ok {
return
}
}
return
}
func (e *evaluator) matchFeatureFlagDisabledUserVariation(flag *data.FeatureFlag, _ *FBUser) (*evalResult, bool) {
if !flag.Enabled {
return &evalResult{
id: flag.DisabledVariationId,
reason: ReasonFlagOff,
keyName: flag.Key,
name: flag.Name,
sendToExperiment: false,
fv: flag.GetFlagValue(flag.DisabledVariationId),
success: true,
flagType: flag.VariationType,
}, true
}
return nil, false
}
func (e *evaluator) matchTargetedUserVariation(flag *data.FeatureFlag, user *FBUser) (*evalResult, bool) {
for _, targetUser := range flag.TargetUsers {
for _, keyId := range targetUser.KeyIds {
if keyId == user.GetKey() {
return &evalResult{
id: targetUser.VariationId,
reason: ReasonTargetMatch,
keyName: flag.Key,
name: flag.Name,
sendToExperiment: flag.ExptIncludeAllTargets,
fv: flag.GetFlagValue(targetUser.VariationId),
success: true,
flagType: flag.VariationType,
}, true
}
}
}
return nil, false
}
func (e *evaluator) matchConditionedUserVariation(flag *data.FeatureFlag, user *FBUser) (*evalResult, bool) {
var rule *data.TargetRule
for _, targetRule := range flag.Rules {
if e.ifUserMatchRule(user, targetRule.Conditions) {
rule = &targetRule
break
}
}
if rule != nil {
return getRolloutVariationValue(flag, rule.Variations, user, ReasonRuleMatch, rule.IncludedInExpt, rule.DispatchKey)
}
return nil, false
}
func (e *evaluator) matchFallThroughUserVariation(flag *data.FeatureFlag, user *FBUser) (*evalResult, bool) {
ft := flag.Fallthrough
return getRolloutVariationValue(flag, ft.Variations, user, ReasonFallthrough, ft.IncludedInExpt, ft.DispatchKey)
}