-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathconfig.go
More file actions
402 lines (341 loc) · 13.8 KB
/
config.go
File metadata and controls
402 lines (341 loc) · 13.8 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/****************************************************************************
* Copyright 2019-2025, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* https://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
// Package datafileprojectconfig //
package datafileprojectconfig
import (
"errors"
"fmt"
"github.com/optimizely/go-sdk/v2/pkg/config/datafileprojectconfig/mappers"
"github.com/optimizely/go-sdk/v2/pkg/entities"
"github.com/optimizely/go-sdk/v2/pkg/logging"
)
var datafileVersions = map[string]struct{}{
"4": {},
}
// DatafileProjectConfig is a project config backed by a datafile
type DatafileProjectConfig struct {
datafile string
hostForODP string
publicKeyForODP string
accountID string
projectID string
revision string
experimentKeyToIDMap map[string]string
audienceMap map[string]entities.Audience
attributeMap map[string]entities.Attribute
attributeKeyMap map[string]entities.Attribute
eventMap map[string]entities.Event
attributeKeyToIDMap map[string]string
attributeIDToKeyMap map[string]string
experimentMap map[string]entities.Experiment
featureMap map[string]entities.Feature
groupMap map[string]entities.Group
rollouts []entities.Rollout
integrations []entities.Integration
segments []string
rolloutMap map[string]entities.Rollout
anonymizeIP bool
botFiltering bool
sendFlagDecisions bool
sdkKey string
environmentKey string
region string
flagVariationsMap map[string][]entities.Variation
holdouts []entities.Holdout
holdoutIDMap map[string]entities.Holdout
flagHoldoutsMap map[string][]entities.Holdout
experimentHoldoutsMap map[string][]entities.Holdout
}
// GetDatafile returns a string representation of the environment's datafile
func (c DatafileProjectConfig) GetDatafile() string {
return c.datafile
}
// GetHostForODP returns hostForODP
func (c DatafileProjectConfig) GetHostForODP() string {
return c.hostForODP
}
// GetPublicKeyForODP returns publicKeyForODP
func (c DatafileProjectConfig) GetPublicKeyForODP() string {
return c.publicKeyForODP
}
// GetProjectID returns projectID
func (c DatafileProjectConfig) GetProjectID() string {
return c.projectID
}
// GetRevision returns revision
func (c DatafileProjectConfig) GetRevision() string {
return c.revision
}
// GetAccountID returns accountID
func (c DatafileProjectConfig) GetAccountID() string {
return c.accountID
}
// GetAnonymizeIP returns anonymizeIP
func (c DatafileProjectConfig) GetAnonymizeIP() bool {
return c.anonymizeIP
}
// GetAttributes returns attributes
func (c DatafileProjectConfig) GetAttributes() (attributeList []entities.Attribute) {
for _, attribute := range c.attributeMap {
attributeList = append(attributeList, attribute)
}
return attributeList
}
// GetAttributeID returns attributeID
func (c DatafileProjectConfig) GetAttributeID(key string) string {
return c.attributeKeyToIDMap[key]
}
// GetAttributeByKey returns the attribute with the given key
func (c DatafileProjectConfig) GetAttributeByKey(key string) (entities.Attribute, error) {
if attribute, ok := c.attributeKeyMap[key]; ok {
return attribute, nil
}
return entities.Attribute{}, fmt.Errorf(`attribute with key "%s" not found`, key)
}
// GetAttributeKeyByID returns the attribute key for the given ID
func (c DatafileProjectConfig) GetAttributeKeyByID(id string) (string, error) {
if key, ok := c.attributeIDToKeyMap[id]; ok {
return key, nil
}
return "", fmt.Errorf(`attribute with ID "%s" not found`, id)
}
// GetBotFiltering returns botFiltering
func (c DatafileProjectConfig) GetBotFiltering() bool {
return c.botFiltering
}
// GetSdkKey returns sdkKey for specific environment.
func (c DatafileProjectConfig) GetSdkKey() string {
return c.sdkKey
}
// GetEnvironmentKey returns current environment of the datafile.
func (c DatafileProjectConfig) GetEnvironmentKey() string {
return c.environmentKey
}
// GetEvents returns all events
func (c DatafileProjectConfig) GetEvents() (eventList []entities.Event) {
for _, event := range c.eventMap {
eventList = append(eventList, event)
}
return eventList
}
// GetEventByKey returns the event with the given key
func (c DatafileProjectConfig) GetEventByKey(eventKey string) (entities.Event, error) {
if event, ok := c.eventMap[eventKey]; ok {
return event, nil
}
return entities.Event{}, fmt.Errorf(`event with key "%s" not found`, eventKey)
}
// GetFeatureByKey returns the feature with the given key
func (c DatafileProjectConfig) GetFeatureByKey(featureKey string) (entities.Feature, error) {
if feature, ok := c.featureMap[featureKey]; ok {
return feature, nil
}
return entities.Feature{}, fmt.Errorf(`feature with key "%s" not found`, featureKey)
}
// GetVariableByKey returns the featureVariable with the given key
func (c DatafileProjectConfig) GetVariableByKey(featureKey, variableKey string) (entities.Variable, error) {
var variable entities.Variable
var err = fmt.Errorf(`variable with key "%s" not found`, featureKey)
if feature, ok := c.featureMap[featureKey]; ok {
if v, ok := feature.VariableMap[variableKey]; ok {
variable = v
err = nil
}
}
return variable, err
}
// GetFeatureList returns an array of all the features
func (c DatafileProjectConfig) GetFeatureList() (featureList []entities.Feature) {
for _, feature := range c.featureMap {
featureList = append(featureList, feature)
}
return featureList
}
// GetExperimentList returns an array of all the experiments
func (c DatafileProjectConfig) GetExperimentList() (experimentList []entities.Experiment) {
for _, experiment := range c.experimentMap {
experimentList = append(experimentList, experiment)
}
return experimentList
}
// GetIntegrationList returns an array of all the integrations
func (c DatafileProjectConfig) GetIntegrationList() (integrationList []entities.Integration) {
return c.integrations
}
// GetSegmentList returns an array of all the segments
func (c DatafileProjectConfig) GetSegmentList() (segmentList []string) {
return c.segments
}
// GetRolloutList returns an array of all the rollouts
func (c DatafileProjectConfig) GetRolloutList() (rolloutList []entities.Rollout) {
return c.rollouts
}
// GetAudienceList returns an array of all the audiences
func (c DatafileProjectConfig) GetAudienceList() (audienceList []entities.Audience) {
for _, audience := range c.audienceMap {
audienceList = append(audienceList, audience)
}
return audienceList
}
// GetAudienceByID returns the audience with the given ID
func (c DatafileProjectConfig) GetAudienceByID(audienceID string) (entities.Audience, error) {
if audience, ok := c.audienceMap[audienceID]; ok {
return audience, nil
}
return entities.Audience{}, fmt.Errorf(`audience with ID "%s" not found`, audienceID)
}
// GetAudienceMap returns the audience map
func (c DatafileProjectConfig) GetAudienceMap() map[string]entities.Audience {
return c.audienceMap
}
// GetExperimentByKey returns the experiment with the given key
func (c DatafileProjectConfig) GetExperimentByKey(experimentKey string) (entities.Experiment, error) {
if experimentID, ok := c.experimentKeyToIDMap[experimentKey]; ok {
if experiment, ok := c.experimentMap[experimentID]; ok {
return experiment, nil
}
}
return entities.Experiment{}, fmt.Errorf(`experiment with key "%s" not found`, experimentKey)
}
// GetExperimentByID returns the experiment with the given ID
func (c DatafileProjectConfig) GetExperimentByID(experimentID string) (entities.Experiment, error) {
if experiment, ok := c.experimentMap[experimentID]; ok {
return experiment, nil
}
return entities.Experiment{}, fmt.Errorf(`experiment with ID "%s" not found`, experimentID)
}
// GetGroupByID returns the group with the given ID
func (c DatafileProjectConfig) GetGroupByID(groupID string) (entities.Group, error) {
if group, ok := c.groupMap[groupID]; ok {
return group, nil
}
return entities.Group{}, fmt.Errorf(`group with ID "%s" not found`, groupID)
}
// SendFlagDecisions determines whether impressions events are sent for ALL decision types
func (c DatafileProjectConfig) SendFlagDecisions() bool {
return c.sendFlagDecisions
}
// GetFlagVariationsMap returns map containing all variations for each flag
func (c DatafileProjectConfig) GetFlagVariationsMap() map[string][]entities.Variation {
return c.flagVariationsMap
}
// GetRegion returns the region for the datafile
func (c DatafileProjectConfig) GetRegion() string {
return c.region
}
// GetHoldoutsForFlag returns all holdouts applicable to the given feature flag
func (c DatafileProjectConfig) GetHoldoutsForFlag(featureKey string) []entities.Holdout {
if holdouts, exists := c.flagHoldoutsMap[featureKey]; exists {
return holdouts
}
return []entities.Holdout{}
}
// GetHoldoutsForExperiment returns the holdouts that apply to a specific experiment
func (c DatafileProjectConfig) GetHoldoutsForExperiment(experimentID string) []entities.Holdout {
if holdouts, exists := c.experimentHoldoutsMap[experimentID]; exists {
return holdouts
}
return []entities.Holdout{}
}
// GetHoldoutList returns all holdouts
func (c DatafileProjectConfig) GetHoldoutList() []entities.Holdout {
return c.holdouts
}
// NewDatafileProjectConfig initializes a new datafile from a json byte array using the default JSON datafile parser
func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogProducer) (*DatafileProjectConfig, error) {
datafile, err := Parse(jsonDatafile)
if err != nil {
logger.Error("Error parsing datafile", err)
return nil, err
}
if _, ok := datafileVersions[datafile.Version]; !ok {
err = errors.New("unsupported datafile version")
logger.Error(fmt.Sprintf("Version %s of datafile not supported", datafile.Version), err)
return nil, err
}
var hostForODP, publicKeyForODP string
for _, integration := range datafile.Integrations {
if integration.Key == nil {
err = errors.New("unsupported key in integrations")
logger.Error("Error parsing datafile", err)
return nil, err
}
if *integration.Key == "odp" {
hostForODP = integration.Host
publicKeyForODP = integration.PublicKey
break
}
}
attributeMap, attributeKeyToIDMap := mappers.MapAttributes(datafile.Attributes)
allExperiments := mappers.MergeExperiments(datafile.Experiments, datafile.Groups)
groupMap, experimentGroupMap := mappers.MapGroups(datafile.Groups)
experimentIDMap, experimentKeyMap := mappers.MapExperiments(allExperiments, experimentGroupMap)
rollouts, rolloutMap := mappers.MapRollouts(datafile.Rollouts)
integrations := []entities.Integration{}
for _, integration := range datafile.Integrations {
integrations = append(integrations, entities.Integration{Key: *integration.Key, Host: integration.Host, PublicKey: integration.PublicKey})
}
eventMap := mappers.MapEvents(datafile.Events)
featureMap := mappers.MapFeatures(datafile.FeatureFlags, rolloutMap, experimentIDMap)
audienceMap, audienceSegmentList := mappers.MapAudiences(append(datafile.TypedAudiences, datafile.Audiences...))
flagVariationsMap := mappers.MapFlagVariations(featureMap)
holdouts, holdoutIDMap, flagHoldoutsMap, experimentHoldoutsMap := mappers.MapHoldouts(datafile.Holdouts, featureMap)
attributeKeyMap := make(map[string]entities.Attribute)
attributeIDToKeyMap := make(map[string]string)
for id, attribute := range attributeMap {
attributeIDToKeyMap[id] = attribute.Key
attributeKeyMap[attribute.Key] = attribute
}
region := datafile.Region
if region == "" {
region = "US"
}
config := &DatafileProjectConfig{
hostForODP: hostForODP,
publicKeyForODP: publicKeyForODP,
datafile: string(jsonDatafile),
accountID: datafile.AccountID,
anonymizeIP: datafile.AnonymizeIP,
attributeKeyToIDMap: attributeKeyToIDMap,
audienceMap: audienceMap,
attributeMap: attributeMap,
botFiltering: datafile.BotFiltering,
sdkKey: datafile.SDKKey,
environmentKey: datafile.EnvironmentKey,
experimentKeyToIDMap: experimentKeyMap,
experimentMap: experimentIDMap,
groupMap: groupMap,
eventMap: eventMap,
featureMap: featureMap,
projectID: datafile.ProjectID,
revision: datafile.Revision,
rollouts: rollouts,
integrations: integrations,
segments: audienceSegmentList,
rolloutMap: rolloutMap,
sendFlagDecisions: datafile.SendFlagDecisions,
flagVariationsMap: flagVariationsMap,
attributeKeyMap: attributeKeyMap,
attributeIDToKeyMap: attributeIDToKeyMap,
region: region,
holdouts: holdouts,
holdoutIDMap: holdoutIDMap,
flagHoldoutsMap: flagHoldoutsMap,
experimentHoldoutsMap: experimentHoldoutsMap,
}
logger.Info("Datafile is valid.")
return config, nil
}