Skip to content

Commit 9c75529

Browse files
yasirfolio3Michael Ngmsohailhussain
authored
feat: add datafile accessor (#290)
* Initial commit. * fixes. Co-authored-by: Michael Ng <[email protected]> Co-authored-by: msohailhussain <[email protected]>
1 parent d8a3c2f commit 9c75529

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

pkg/config/datafileprojectconfig/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -32,6 +32,7 @@ var datafileVersions = map[string]struct{}{
3232

3333
// DatafileProjectConfig is a project config backed by a datafile
3434
type DatafileProjectConfig struct {
35+
datafile string
3536
accountID string
3637
projectID string
3738
revision string
@@ -48,6 +49,11 @@ type DatafileProjectConfig struct {
4849
botFiltering bool
4950
}
5051

52+
// GetDatafile returns a string representation of the environment's datafile
53+
func (c DatafileProjectConfig) GetDatafile() string {
54+
return c.datafile
55+
}
56+
5157
// GetProjectID returns projectID
5258
func (c DatafileProjectConfig) GetProjectID() string {
5359
return c.projectID
@@ -196,6 +202,7 @@ func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogP
196202
mergedAudiences := append(datafile.TypedAudiences, datafile.Audiences...)
197203
featureMap := mappers.MapFeatures(datafile.FeatureFlags, rolloutMap, experimentMap)
198204
config := &DatafileProjectConfig{
205+
datafile: string(jsonDatafile),
199206
accountID: datafile.AccountID,
200207
anonymizeIP: datafile.AnonymizeIP,
201208
attributeKeyToIDMap: attributeKeyToIDMap,

pkg/config/interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -24,6 +24,7 @@ import (
2424

2525
// ProjectConfig represents the project's experiments and feature flags and contains methods for accessing the them.
2626
type ProjectConfig interface {
27+
GetDatafile() string
2728
GetAccountID() string
2829
GetAnonymizeIP() bool
2930
GetAttributeID(id string) string // returns "" if there is no id

pkg/config/optimizely_config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -17,13 +17,21 @@
1717
// Package config //
1818
package config
1919

20-
import "github.com/optimizely/go-sdk/pkg/entities"
20+
import (
21+
"github.com/optimizely/go-sdk/pkg/entities"
22+
)
2123

2224
// OptimizelyConfig is a snapshot of the experiments and features in the project config
2325
type OptimizelyConfig struct {
2426
Revision string `json:"revision"`
2527
ExperimentsMap map[string]OptimizelyExperiment `json:"experimentsMap"`
2628
FeaturesMap map[string]OptimizelyFeature `json:"featuresMap"`
29+
datafile string
30+
}
31+
32+
// GetDatafile returns a string representation of the environment's datafile
33+
func (c OptimizelyConfig) GetDatafile() string {
34+
return c.datafile
2735
}
2836

2937
// OptimizelyExperiment has experiment info
@@ -159,6 +167,7 @@ func NewOptimizelyConfig(projConfig ProjectConfig) *OptimizelyConfig {
159167
optimizelyConfig.ExperimentsMap = getExperimentMap(featuresList, experimentsList, variableByIDMap)
160168
optimizelyConfig.FeaturesMap = getFeatureMap(featuresList, optimizelyConfig.ExperimentsMap)
161169
optimizelyConfig.Revision = revision
170+
optimizelyConfig.datafile = projConfig.GetDatafile()
162171

163172
return optimizelyConfig
164173
}

pkg/config/optimizely_config_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -49,6 +49,7 @@ func (s *OptimizelyConfigTestSuite) SetupTest() {
4949
if err != nil {
5050
s.Fail("error opening file " + dataFileName)
5151
}
52+
s.expectedOptimizelyConfig.datafile = string(dataFile)
5253

5354
projectMgr := NewStaticProjectConfigManagerWithOptions("", WithInitialDatafile(dataFile))
5455

@@ -62,17 +63,25 @@ func (s *OptimizelyConfigTestSuite) TestOptlyConfig() {
6263
s.Equal(s.expectedOptimizelyConfig.FeaturesMap, optimizelyConfig.FeaturesMap)
6364
s.Equal(s.expectedOptimizelyConfig.ExperimentsMap, optimizelyConfig.ExperimentsMap)
6465
s.Equal(s.expectedOptimizelyConfig.Revision, optimizelyConfig.Revision)
66+
s.Equal(s.expectedOptimizelyConfig.datafile, optimizelyConfig.datafile)
6567

6668
s.Equal(s.expectedOptimizelyConfig, *optimizelyConfig)
67-
6869
}
6970

7071
func (s *OptimizelyConfigTestSuite) TestOptlyConfigNullProjectConfig() {
7172
optimizelyConfig := NewOptimizelyConfig(nil)
7273

7374
s.Nil(optimizelyConfig)
75+
}
7476

77+
func (s *OptimizelyConfigTestSuite) TestOptlyConfigGetDatafile() {
78+
datafile := []byte(`{"version":"4"}`)
79+
projectMgr := NewStaticProjectConfigManagerWithOptions("", WithInitialDatafile(datafile))
80+
optimizelyConfig := NewOptimizelyConfig(projectMgr.projectConfig)
81+
s.NotNil(optimizelyConfig.datafile)
82+
s.Equal(string(datafile), optimizelyConfig.GetDatafile())
7583
}
84+
7685
func TestOptimizelyConfigTestSuite(t *testing.T) {
7786
suite.Run(t, new(OptimizelyConfigTestSuite))
7887
}

pkg/config/static_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019-2020, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -58,7 +58,7 @@ func TestStaticGetOptimizelyConfig(t *testing.T) {
5858
optimizelyConfig := configManager.GetOptimizelyConfig()
5959
assert.NotNil(t, configManager.optimizelyConfig)
6060
assert.Equal(t, &OptimizelyConfig{ExperimentsMap: map[string]OptimizelyExperiment{},
61-
FeaturesMap: map[string]OptimizelyFeature{}}, optimizelyConfig)
61+
FeaturesMap: map[string]OptimizelyFeature{}, datafile: "{\"accountId\":\"42\",\"projectId\":\"123\",\"version\":\"4\"}"}, optimizelyConfig)
6262
}
6363
func TestNewStaticProjectConfigManagerFromURL(t *testing.T) {
6464

0 commit comments

Comments
 (0)