Skip to content

Commit 43dd94a

Browse files
Feat: added SDKkey and environmentKey in datafile (#312)
* Added support for sdkKey and Environment to config. * updating `environment` to `environmentKey` as per fsc changes. * suggested changes made. Co-authored-by: msohailhussain <[email protected]>
1 parent 873c9fd commit 43dd94a

File tree

8 files changed

+138
-11
lines changed

8 files changed

+138
-11
lines changed

pkg/config/datafileprojectconfig/config.go

Lines changed: 16 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-2021, 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. *
@@ -48,6 +48,8 @@ type DatafileProjectConfig struct {
4848
anonymizeIP bool
4949
botFiltering bool
5050
sendFlagDecisions bool
51+
sdkKey string
52+
environmentKey string
5153
}
5254

5355
// GetDatafile returns a string representation of the environment's datafile
@@ -80,11 +82,21 @@ func (c DatafileProjectConfig) GetAttributeID(key string) string {
8082
return c.attributeKeyToIDMap[key]
8183
}
8284

83-
// GetBotFiltering returns GetBotFiltering
85+
// GetBotFiltering returns botFiltering
8486
func (c DatafileProjectConfig) GetBotFiltering() bool {
8587
return c.botFiltering
8688
}
8789

90+
// GetSdkKey returns sdkKey for specific environment.
91+
func (c DatafileProjectConfig) GetSdkKey() string {
92+
return c.sdkKey
93+
}
94+
95+
// GetEnvironmentKey returns current environment of the datafile.
96+
func (c DatafileProjectConfig) GetEnvironmentKey() string {
97+
return c.environmentKey
98+
}
99+
88100
// GetEventByKey returns the event with the given key
89101
func (c DatafileProjectConfig) GetEventByKey(eventKey string) (entities.Event, error) {
90102
if event, ok := c.eventMap[eventKey]; ok {
@@ -215,6 +227,8 @@ func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogP
215227
audienceMap: mappers.MapAudiences(mergedAudiences),
216228
attributeMap: attributeMap,
217229
botFiltering: datafile.BotFiltering,
230+
sdkKey: datafile.SDKKey,
231+
environmentKey: datafile.EnvironmentKey,
218232
experimentKeyToIDMap: experimentKeyMap,
219233
experimentMap: experimentMap,
220234
groupMap: groupMap,

pkg/config/datafileprojectconfig/config_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019,2021, 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. *
@@ -39,15 +39,17 @@ func TestNewDatafileProjectConfigNil(t *testing.T) {
3939
}
4040

4141
func TestNewDatafileProjectConfigNotNil(t *testing.T) {
42-
dpc := DatafileProjectConfig{accountID: "123", revision: "1", projectID: "12345"}
43-
jsonDatafileStr := `{"accountID": "123", "revision": "1", "projectId": "12345", "version": "4"}`
42+
dpc := DatafileProjectConfig{accountID: "123", revision: "1", projectID: "12345", sdkKey: "a", environmentKey: "production"}
43+
jsonDatafileStr := `{"accountID": "123", "revision": "1", "projectId": "12345", "version": "4", "sdkKey": "a", "environmentKey": "production"}`
4444
jsonDatafile := []byte(jsonDatafileStr)
4545
projectConfig, err := NewDatafileProjectConfig(jsonDatafile, logging.GetLogger("", "DatafileProjectConfig"))
4646
assert.Nil(t, err)
4747
assert.NotNil(t, projectConfig)
4848
assert.Equal(t, dpc.accountID, projectConfig.accountID)
4949
assert.Equal(t, dpc.revision, projectConfig.revision)
5050
assert.Equal(t, dpc.projectID, projectConfig.projectID)
51+
assert.Equal(t, dpc.environmentKey, projectConfig.environmentKey)
52+
assert.Equal(t, dpc.sdkKey, projectConfig.sdkKey)
5153
}
5254

5355
func TestGetProjectID(t *testing.T) {
@@ -98,6 +100,21 @@ func TestGetAttributeID(t *testing.T) {
98100
assert.Equal(t, id, config.GetAttributeID(key))
99101
}
100102

103+
func TestGetSdkKey(t *testing.T) {
104+
config := &DatafileProjectConfig{
105+
sdkKey: "1",
106+
}
107+
108+
assert.Equal(t, "1", config.GetSdkKey())
109+
}
110+
111+
func TestGetEnvironmentKey(t *testing.T) {
112+
config := &DatafileProjectConfig{
113+
environmentKey: "production",
114+
}
115+
assert.Equal(t, "production", config.GetEnvironmentKey())
116+
}
117+
101118
func TestGetBotFiltering(t *testing.T) {
102119
botFiltering := true
103120
config := &DatafileProjectConfig{

pkg/config/datafileprojectconfig/entities/entities.go

Lines changed: 3 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,2021, 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. *
@@ -122,4 +122,6 @@ type Datafile struct {
122122
AnonymizeIP bool `json:"anonymizeIP"`
123123
BotFiltering bool `json:"botFiltering"`
124124
SendFlagDecisions bool `json:"sendFlagDecisions"`
125+
SDKKey string `json:"sdkKey,omitempty"`
126+
EnvironmentKey string `json:"environmentKey,omitempty"`
125127
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/****************************************************************************
2+
* Copyright 2021, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package entities
18+
19+
import (
20+
"encoding/json"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestUnMarshalSdkKeyAndEnvironmentKeyFromDatafile(t *testing.T) {
27+
datafile := Datafile{}
28+
var jsonMap map[string]interface{}
29+
bytesData, _ := json.Marshal(datafile)
30+
json.Unmarshal(bytesData, &jsonMap)
31+
32+
_, keyExists := jsonMap["sdkKey"]
33+
assert.False(t, keyExists)
34+
35+
_, keyExists = jsonMap["environmentKey"]
36+
assert.False(t, keyExists)
37+
38+
datafile.SDKKey = "a"
39+
datafile.EnvironmentKey = "production"
40+
bytesData, _ = json.Marshal(datafile)
41+
json.Unmarshal(bytesData, &jsonMap)
42+
43+
_, keyExists = jsonMap["sdkKey"]
44+
assert.True(t, keyExists)
45+
46+
_, keyExists = jsonMap["environmentKey"]
47+
assert.True(t, keyExists)
48+
}

pkg/config/interface.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019-2020, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2021, 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. *
@@ -42,6 +42,8 @@ type ProjectConfig interface {
4242
GetProjectID() string
4343
GetRevision() string
4444
SendFlagDecisions() bool
45+
GetSdkKey() string
46+
GetEnvironmentKey() string
4547
}
4648

4749
// ProjectConfigManager maintains an instance of the ProjectConfig

pkg/config/optimizely_config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019-2020, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2021, 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. *
@@ -26,6 +26,8 @@ type OptimizelyConfig struct {
2626
Revision string `json:"revision"`
2727
ExperimentsMap map[string]OptimizelyExperiment `json:"experimentsMap"`
2828
FeaturesMap map[string]OptimizelyFeature `json:"featuresMap"`
29+
SdkKey string `json:"sdkKey,omitempty"`
30+
EnvironmentKey string `json:"environmentKey,omitempty"`
2931
datafile string
3032
}
3133

@@ -167,6 +169,8 @@ func NewOptimizelyConfig(projConfig ProjectConfig) *OptimizelyConfig {
167169
optimizelyConfig.ExperimentsMap = getExperimentMap(featuresList, experimentsList, variableByIDMap)
168170
optimizelyConfig.FeaturesMap = getFeatureMap(featuresList, optimizelyConfig.ExperimentsMap)
169171
optimizelyConfig.Revision = revision
172+
optimizelyConfig.SdkKey = projConfig.GetSdkKey()
173+
optimizelyConfig.EnvironmentKey = projConfig.GetEnvironmentKey()
170174
optimizelyConfig.datafile = projConfig.GetDatafile()
171175

172176
return optimizelyConfig

pkg/config/optimizely_config_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019-2020, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2021, 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. *
@@ -64,10 +64,48 @@ func (s *OptimizelyConfigTestSuite) TestOptlyConfig() {
6464
s.Equal(s.expectedOptimizelyConfig.ExperimentsMap, optimizelyConfig.ExperimentsMap)
6565
s.Equal(s.expectedOptimizelyConfig.Revision, optimizelyConfig.Revision)
6666
s.Equal(s.expectedOptimizelyConfig.datafile, optimizelyConfig.datafile)
67+
s.Equal(s.expectedOptimizelyConfig.SdkKey, optimizelyConfig.SdkKey)
68+
s.Equal(s.expectedOptimizelyConfig.EnvironmentKey, optimizelyConfig.EnvironmentKey)
6769

6870
s.Equal(s.expectedOptimizelyConfig, *optimizelyConfig)
6971
}
7072

73+
func (s *OptimizelyConfigTestSuite) TestOptlyConfigUnMarshalEmptySDKKeyAndEnvironmentKey() {
74+
datafile := []byte(`{"version":"4"}`)
75+
projectMgr := NewStaticProjectConfigManagerWithOptions("", WithInitialDatafile(datafile))
76+
optimizelyConfig := NewOptimizelyConfig(projectMgr.projectConfig)
77+
s.Equal("", optimizelyConfig.SdkKey)
78+
s.Equal("", optimizelyConfig.EnvironmentKey)
79+
80+
var jsonMap map[string]interface{}
81+
bytesData, _ := json.Marshal(optimizelyConfig)
82+
json.Unmarshal(bytesData, &jsonMap)
83+
84+
_, keyExists := jsonMap["sdkKey"]
85+
s.False(keyExists)
86+
87+
_, keyExists = jsonMap["environmentKey"]
88+
s.False(keyExists)
89+
}
90+
91+
func (s *OptimizelyConfigTestSuite) TestOptlyConfigUnMarshalNonEmptySDKKeyAndEnvironmentKey() {
92+
datafile := []byte(`{"version":"4", "sdkKey":"a", "environmentKey": "production"}`)
93+
projectMgr := NewStaticProjectConfigManagerWithOptions("", WithInitialDatafile(datafile))
94+
optimizelyConfig := NewOptimizelyConfig(projectMgr.projectConfig)
95+
s.Equal("a", optimizelyConfig.SdkKey)
96+
s.Equal("production", optimizelyConfig.EnvironmentKey)
97+
98+
var jsonMap map[string]interface{}
99+
bytesData, _ := json.Marshal(optimizelyConfig)
100+
json.Unmarshal(bytesData, &jsonMap)
101+
102+
_, keyExists := jsonMap["sdkKey"]
103+
s.True(keyExists)
104+
105+
_, keyExists = jsonMap["environmentKey"]
106+
s.True(keyExists)
107+
}
108+
71109
func (s *OptimizelyConfigTestSuite) TestOptlyConfigNullProjectConfig() {
72110
optimizelyConfig := NewOptimizelyConfig(nil)
73111

test-data/decide-test-datafile.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,7 @@
342342
"key": "event_multiple_running_exp_attached"
343343
}
344344
],
345-
"revision": "241"
346-
}
345+
"revision": "241",
346+
"sdkKey": "ValidProjectConfigV4",
347+
"environmentKey": "production"
348+
}

0 commit comments

Comments
 (0)