Skip to content

Commit

Permalink
Merge pull request #94 from optimizely/pawel/track_event_bug
Browse files Browse the repository at this point in the history
added event mapper (needed for track)
  • Loading branch information
pawels-optimizely committed Sep 5, 2019
2 parents ac4dbd6 + 3b48883 commit 52f864a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions optimizely/config/datafileprojectconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func NewDatafileProjectConfig(jsonDatafile []byte) (*DatafileProjectConfig, erro
attributeMap, attributeKeyToIDMap := mappers.MapAttributes(datafile.Attributes)
experimentMap, experimentKeyMap := mappers.MapExperiments(datafile.Experiments)
rolloutMap := mappers.MapRollouts(datafile.Rollouts)
eventMap := mappers.MapEvents(datafile.Events)
mergedAudiences := append(datafile.TypedAudiences, datafile.Audiences...)
config := &DatafileProjectConfig{
accountID: datafile.AccountID,
Expand All @@ -97,6 +98,7 @@ func NewDatafileProjectConfig(jsonDatafile []byte) (*DatafileProjectConfig, erro
botFiltering: datafile.BotFiltering,
experimentKeyToIDMap: experimentKeyMap,
experimentMap: experimentMap,
eventMap: eventMap,
featureMap: mappers.MapFeatures(datafile.FeatureFlags, rolloutMap, experimentMap),
projectID: datafile.ProjectID,
revision: datafile.Revision,
Expand Down
34 changes: 34 additions & 0 deletions optimizely/config/datafileprojectconfig/mappers/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/****************************************************************************
* Copyright 2019, 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 *
* *
* http://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 mappers ...
package mappers

import (
datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
"github.com/optimizely/go-sdk/optimizely/entities"
)

// MapEvents maps the raw datafile event entities to SDK Event entities
func MapEvents(events []datafileEntities.Event) map[string]entities.Event {
eventMap := make(map[string]entities.Event)
for _, event := range events {
entityEvent := entities.Event{ID: event.ID, Key: event.Key, ExperimentIds: event.ExperimentIds}
eventMap[entityEvent.Key] = entityEvent
}

return eventMap
}
48 changes: 48 additions & 0 deletions optimizely/config/datafileprojectconfig/mappers/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/****************************************************************************
* Copyright 2019, 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 *
* *
* http://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 mappers

import (
"testing"

datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
"github.com/optimizely/go-sdk/optimizely/entities"
"github.com/stretchr/testify/assert"
)

func TestMapEvents(t *testing.T) {
const testEventString = `{
"id": "some_id",
"key": "event1",
"experimentIds": ["11111", "11112"]
}`

var rawEvent datafileEntities.Event
json.Unmarshal([]byte(testEventString), &rawEvent)

rawEvents := []datafileEntities.Event{rawEvent}
eventsMap := MapEvents(rawEvents)
expectedEventMap := map[string]entities.Event{
"event1": entities.Event{
ID: "some_id",
Key: "event1",
ExperimentIds: []string{"11111", "11112"},
},
}

assert.Equal(t, expectedEventMap, eventsMap)
}

0 comments on commit 52f864a

Please sign in to comment.