Skip to content

Commit

Permalink
fix(audience-conditions): fixed parsing for audience conditions. (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirfolio3 authored and Michael Ng committed Nov 5, 2019
1 parent 168b57c commit 9c082a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/config/datafileprojectconfig/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Experiment struct {
TrafficAllocation []TrafficAllocation `json:"trafficAllocation"`
AudienceIds []string `json:"audienceIds"`
ForcedVariations map[string]string `json:"forcedVariations"`
AudienceConditions []interface{} `json:"audienceConditions"`
AudienceConditions interface{} `json:"audienceConditions"`
}

// Group represents an Group object from the Optimizely datafile
Expand Down
14 changes: 12 additions & 2 deletions pkg/config/datafileprojectconfig/mappers/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,18 @@ func mapExperiment(rawExperiment datafileEntities.Experiment) entities.Experimen
var err error
if rawExperiment.AudienceConditions == nil && len(rawExperiment.AudienceIds) > 0 {
audienceConditionTree, err = buildAudienceConditionTree(rawExperiment.AudienceIds)
} else if len(rawExperiment.AudienceConditions) > 0 {
audienceConditionTree, err = buildAudienceConditionTree(rawExperiment.AudienceConditions)
} else {
switch audienceConditions := rawExperiment.AudienceConditions.(type) {
case []interface{}:
if len(audienceConditions) > 0 {
audienceConditionTree, err = buildAudienceConditionTree(audienceConditions)
}
case string:
if audienceConditions != "" {
audienceConditionTree, err = buildAudienceConditionTree([]string{audienceConditions})
}
default:
}
}
if err != nil {
// @TODO: handle error
Expand Down
40 changes: 40 additions & 0 deletions pkg/config/datafileprojectconfig/mappers/experiment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,46 @@ func TestMapExperiments(t *testing.T) {
assert.Equal(t, expectedExperimentKeyMap, experimentKeyMap)
}

func TestMapExperimentsWithStringAudienceCondition(t *testing.T) {

rawExperiment := datafileEntities.Experiment{
ID: "11111",
AudienceIds: []string{"31111"},
Key: "test_experiment_11111",
AudienceConditions: "31111",
}

rawExperiments := []datafileEntities.Experiment{rawExperiment}
experimentGroupMap := map[string]string{"11111": "15"}

experiments, experimentKeyMap := MapExperiments(rawExperiments, experimentGroupMap)
expectedExperiments := map[string]entities.Experiment{
"11111": {
AudienceIds: []string{"31111"},
ID: "11111",
GroupID: "15",
Key: "test_experiment_11111",
Variations: map[string]entities.Variation{},
TrafficAllocation: []entities.Range{},
AudienceConditionTree: &entities.TreeNode{
Operator: "or",
Nodes: []*entities.TreeNode{
{
Operator: "",
Item: "31111",
},
},
},
},
}
expectedExperimentKeyMap := map[string]string{
"test_experiment_11111": "11111",
}

assert.Equal(t, expectedExperiments, experiments)
assert.Equal(t, expectedExperimentKeyMap, experimentKeyMap)
}

func TestMergeExperiments(t *testing.T) {

rawExperiment := datafileEntities.Experiment{
Expand Down

0 comments on commit 9c082a9

Please sign in to comment.