23
23
LocalEvaluationDeploymentKey = "server-jAqqJaX3l8PgNiJpcv9j20ywPzANQQFh"
24
24
)
25
25
26
- type variant struct {
27
- Value string `json:"value,omitempty"`
28
- Payload interface {} `json:"payload,omitempty"`
29
- }
30
-
31
26
type UserProperties struct {
32
27
OrgId string `json:"org_id,omitempty"`
28
+ UserId string `json:"user_id,omitempty"`
33
29
OrgName string `json:"org_name,omitempty"`
34
30
Username string `json:"username,omitempty"`
35
31
UserStatus string `json:"user_status,omitempty"`
@@ -41,8 +37,19 @@ type UserProperties struct {
41
37
TemplateId string `json:"template_id,omitempty"`
42
38
}
43
39
44
- func init () {
40
+ type AmplitudeConfig struct {
41
+ Debug bool
42
+ ServerUrl string
43
+ FlagConfigPollerInterval time.Duration
44
+ FlagConfigPollerRequestTimeout time.Duration
45
+ }
45
46
47
+ type AmplitudeVariant struct {
48
+ Value string `json:"value,omitempty"`
49
+ Payload interface {} `json:"payload,omitempty"`
50
+ }
51
+
52
+ func init () {
46
53
err := godotenv .Load ()
47
54
if err != nil {
48
55
fmt .Printf ("No .env file found" )
@@ -77,13 +84,31 @@ func Initialize() {
77
84
client = local .Initialize (LocalEvaluationDeploymentKey , & config )
78
85
err := client .Start ()
79
86
if err != nil {
80
- err = fmt .Errorf ("unable to create local evaluation client with given config %v with error %s" , config , err .Error ())
87
+ err = fmt .Errorf ("unable to create local evaluation client with given config %+ v with error %s" , config , err .Error ())
81
88
panic (err )
82
89
}
83
90
}
84
91
85
- func fetch (flagName string , user UserProperties ) variant {
86
- flagKeys := []string {flagName }
92
+ func InitializeWithConfig (conf AmplitudeConfig ) {
93
+ client = local .Initialize (LocalEvaluationDeploymentKey , (* local .Config )(& conf ))
94
+ err := client .Start ()
95
+ if err != nil {
96
+ err = fmt .Errorf ("unable to create local evaluation client with given config %+v with error %s" , conf , err .Error ())
97
+ panic (err )
98
+ }
99
+ }
100
+
101
+ func contains (s []string , e string ) bool {
102
+ for _ , a := range s {
103
+ if a == e {
104
+ return true
105
+ }
106
+ }
107
+ return false
108
+ }
109
+
110
+ func fetch (flagKeys []string , user UserProperties ) map [string ]AmplitudeVariant {
111
+ variants := make (map [string ]AmplitudeVariant )
87
112
userProp := map [string ]interface {}{
88
113
"org_id" : user .OrgId ,
89
114
"org_name" : user .OrgName ,
@@ -95,36 +120,64 @@ func fetch(flagName string, user UserProperties) variant {
95
120
"infra_provider" : user .InfraProvider ,
96
121
"template_id" : user .TemplateId ,
97
122
}
98
-
99
123
expUser := experiment.User {
124
+ UserId : user .UserId ,
100
125
UserProperties : userProp ,
101
126
}
102
127
103
- variants , err := client .Evaluate (& expUser , flagKeys )
128
+ result , err := client .EvaluateByOrg (& expUser )
104
129
if err != nil {
105
- return variant {}
130
+ return variants
131
+ }
132
+ filter := len (flagKeys ) != 0
133
+ for k , v := range * result {
134
+ if v .IsDefaultVariant {
135
+ continue
136
+ }
137
+ if ! filter {
138
+ variants [k ] = AmplitudeVariant {
139
+ Value : v .Variant .Key ,
140
+ Payload : v .Variant .Payload ,
141
+ }
142
+ continue
143
+ }
144
+ if contains (flagKeys , k ) {
145
+ variants [k ] = AmplitudeVariant {
146
+ Value : v .Variant .Key ,
147
+ Payload : v .Variant .Payload ,
148
+ }
149
+ }
106
150
}
107
151
108
- return variant ( variants [ flagName ])
152
+ return variants
109
153
}
110
154
111
155
func GetFeatureFlagString (flagName string , user UserProperties ) string {
112
- data := fetch (flagName , user )
113
- return data .Value
156
+ flagKeys := []string {flagName }
157
+ data := fetch (flagKeys , user )
158
+ return data [flagName ].Value
114
159
}
115
160
116
161
func GetFeatureFlagBool (flagName string , user UserProperties ) bool {
117
- data := fetch (flagName , user )
118
- if val , err := strconv .ParseBool (data .Value ); err == nil {
162
+ flagKeys := []string {flagName }
163
+ data := fetch (flagKeys , user )
164
+ if val , err := strconv .ParseBool (data [flagName ].Value ); err == nil {
119
165
return val
120
166
}
121
167
return false
122
168
}
123
169
124
170
func GetFeatureFlagPayload (flagName string , user UserProperties ) map [string ]interface {} {
125
- data := fetch (flagName , user )
171
+ flagKeys := []string {flagName }
172
+ data := fetch (flagKeys , user )
126
173
mapData := make (map [string ]interface {})
127
- mapData ["value" ] = data .Value
128
- mapData ["payload" ] = data .Payload
174
+ mapData ["value" ] = data [ flagName ] .Value
175
+ mapData ["payload" ] = data [ flagName ] .Payload
129
176
return mapData
130
177
}
178
+
179
+ func GetFeatureFlagByOrg (user UserProperties ) map [string ]AmplitudeVariant {
180
+ flagKeys := []string {}
181
+ data := fetch (flagKeys , user )
182
+ return data
183
+ }
0 commit comments