Skip to content

Commit 425c3e2

Browse files
added event mapper (needed for track)
1 parent 23f6de1 commit 425c3e2

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

optimizely/config/datafileprojectconfig/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func NewDatafileProjectConfig(jsonDatafile []byte) (*DatafileProjectConfig, erro
8787
attributeMap, attributeKeyToIDMap := mappers.MapAttributes(datafile.Attributes)
8888
experimentMap, experimentKeyMap := mappers.MapExperiments(datafile.Experiments)
8989
rolloutMap := mappers.MapRollouts(datafile.Rollouts)
90+
eventMap := mappers.MapEvents(datafile.Events)
9091
mergedAudiences := append(datafile.TypedAudiences, datafile.Audiences...)
9192
config := &DatafileProjectConfig{
9293
accountID: datafile.AccountID,
@@ -97,6 +98,7 @@ func NewDatafileProjectConfig(jsonDatafile []byte) (*DatafileProjectConfig, erro
9798
botFiltering: datafile.BotFiltering,
9899
experimentKeyToIDMap: experimentKeyMap,
99100
experimentMap: experimentMap,
101+
eventMap: eventMap,
100102
featureMap: mappers.MapFeatures(datafile.FeatureFlags, rolloutMap, experimentMap),
101103
projectID: datafile.ProjectID,
102104
revision: datafile.Revision,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/****************************************************************************
2+
* Copyright 2019, 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 mappers ...
18+
package mappers
19+
20+
import (
21+
datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
22+
"github.com/optimizely/go-sdk/optimizely/entities"
23+
)
24+
25+
// MapEvents maps the raw datafile event entities to SDK Event entities
26+
func MapEvents(events []datafileEntities.Event) map[string]entities.Event {
27+
eventMap := make(map[string]entities.Event)
28+
for _, event := range events {
29+
entityEvent := entities.Event{ID: event.ID, Key: event.Key, ExperimentIds: event.ExperimentIds}
30+
eventMap[entityEvent.Key] = entityEvent
31+
}
32+
33+
return eventMap
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/****************************************************************************
2+
* Copyright 2019, 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 mappers
18+
19+
import (
20+
"testing"
21+
22+
datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
23+
"github.com/optimizely/go-sdk/optimizely/entities"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestMapEvents(t *testing.T) {
28+
const testEventString = `{
29+
"id": "some_id",
30+
"key": "event1",
31+
"experimentIds": ["11111", "11112"]
32+
}`
33+
34+
var rawEvent datafileEntities.Event
35+
json.Unmarshal([]byte(testEventString), &rawEvent)
36+
37+
rawEvents := []datafileEntities.Event{rawEvent}
38+
eventsMap := MapEvents(rawEvents)
39+
expectedEventMap := map[string]entities.Event{
40+
"event1": entities.Event{
41+
ID: "some_id",
42+
Key: "event1",
43+
ExperimentIds: []string{"11111", "11112"},
44+
},
45+
}
46+
47+
assert.Equal(t, expectedEventMap, eventsMap)
48+
}

0 commit comments

Comments
 (0)