Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions optimizely/config/datafileProjectConfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (c DatafileProjectConfig) GetAudienceByID(audienceID string) (entities.Audi
return entities.Audience{}, errors.New(errMessage)
}

// GetAudienceMap returns the audience map
func (c DatafileProjectConfig) GetAudienceMap() map[string]entities.Audience {
return c.audienceMap
}

// GetExperimentByKey returns the experiment with the given key
func (c DatafileProjectConfig) GetExperimentByKey(experimentKey string) (entities.Experiment, error) {
if experimentID, ok := c.experimentKeyToIDMap[experimentKey]; ok {
Expand Down
22 changes: 11 additions & 11 deletions optimizely/config/datafileProjectConfig/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ type Audience struct {

// Experiment represents an Experiment object from the Optimizely datafile
type Experiment struct {
// @TODO(mng): include audienceConditions
ID string `json:"id"`
Key string `json:"key"`
LayerID string `json:"layerId"`
Status string `json:"status"`
Variations []Variation `json:"variations"`
TrafficAllocation []trafficAllocation `json:"trafficAllocation"`
AudienceIds []string `json:"audienceIds"`
ForcedVariations map[string]string `json:"forcedVariations"`
ID string `json:"id"`
Key string `json:"key"`
LayerID string `json:"layerId"`
Status string `json:"status"`
Variations []Variation `json:"variations"`
TrafficAllocation []trafficAllocation `json:"trafficAllocation"`
AudienceIds []string `json:"audienceIds"`
ForcedVariations map[string]string `json:"forcedVariations"`
AudienceConditions interface{} `json:"audienceConditions"`
}

// FeatureFlag represents a FeatureFlag object from the Optimizely datafile
Expand All @@ -60,8 +60,8 @@ type Variation struct {
}

type Event struct {
ID string `json:"id"`
Key string `json:"key"`
ID string `json:"id"`
Key string `json:"key"`
ExperimentIds []string `json:"experimentIds"`
}

Expand Down
62 changes: 0 additions & 62 deletions optimizely/config/datafileProjectConfig/mappers/audience.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package mappers

import (
"encoding/json"
"reflect"

datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileProjectConfig/entities"
"github.com/optimizely/go-sdk/optimizely/entities"
)
Expand All @@ -41,62 +38,3 @@ func MapAudiences(audiences []datafileEntities.Audience) map[string]entities.Aud
}
return audienceMap
}

// Takes the conditions array from the audience in the datafile and turns it into a condition tree
func buildConditionTree(conditions interface{}) (*entities.ConditionTreeNode, error) {

value := reflect.ValueOf(conditions)
visited := make(map[interface{}]bool)
var retErr error

conditionTree := &entities.ConditionTreeNode{}
var populateConditions func(v reflect.Value, root *entities.ConditionTreeNode)
populateConditions = func(v reflect.Value, root *entities.ConditionTreeNode) {

for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
if v.Kind() == reflect.Ptr {
// Check for recursive data
if visited[v.Interface()] {
return
}
visited[v.Interface()] = true
}
v = v.Elem()
}

switch v.Kind() {

case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
n := &entities.ConditionTreeNode{}
typedV := v.Index(i).Interface()
switch typedV.(type) {
case string:
n.Operator = typedV.(string)
root.Operator = n.Operator
continue

case map[string]interface{}:
jsonBody, err := json.Marshal(typedV)
if err != nil {
retErr = err
return
}
condition := entities.Condition{}
if err := json.Unmarshal(jsonBody, &condition); err != nil {
retErr = err
return
}
n.Condition = condition
}

root.Nodes = append(root.Nodes, n)

populateConditions(v.Index(i), n)
}
}
}

populateConditions(value, conditionTree)
return conditionTree, retErr
}
43 changes: 0 additions & 43 deletions optimizely/config/datafileProjectConfig/mappers/audience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,3 @@
***************************************************************************/

package mappers

import (
"encoding/json"
"testing"

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

func TestBuildConditionTreeSimpleAudienceCondition(t *testing.T) {
conditionString := "[ \"and\", [ \"or\", [ \"or\", { \"type\": \"custom_attribute\", \"name\": \"s_foo\", \"match\": \"exact\", \"value\": \"foo\" } ] ] ]"
var conditions interface{}
json.Unmarshal([]byte(conditionString), &conditions)
conditionTree, err := buildConditionTree(conditions)
if err != nil {
assert.Fail(t, err.Error())
}

expectedConditionTree := &entities.ConditionTreeNode{
Operator: "and",
Nodes: []*entities.ConditionTreeNode{
&entities.ConditionTreeNode{
Operator: "or",
Nodes: []*entities.ConditionTreeNode{
&entities.ConditionTreeNode{
Operator: "or",
Nodes: []*entities.ConditionTreeNode{
&entities.ConditionTreeNode{
Condition: entities.Condition{
Name: "s_foo",
Match: "exact",
Type: "custom_attribute",
Value: "foo",
},
},
},
},
},
},
},
}
assert.Equal(t, expectedConditionTree, conditionTree)
}
155 changes: 155 additions & 0 deletions optimizely/config/datafileProjectConfig/mappers/condition_trees.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/****************************************************************************
* 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 (
"encoding/json"
"errors"
"github.com/optimizely/go-sdk/optimizely/entities"
"reflect"
)

var ErrEmptyTree = errors.New("Empty Tree")

// Takes the conditions array from the audience in the datafile and turns it into a condition tree
func buildConditionTree(conditions interface{}) (conditionTree *entities.TreeNode, retErr error) {

value := reflect.ValueOf(conditions)
visited := make(map[interface{}]bool)

conditionTree = &entities.TreeNode{}
var populateConditions func(v reflect.Value, root *entities.TreeNode)
populateConditions = func(v reflect.Value, root *entities.TreeNode) {

for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
if v.Kind() == reflect.Ptr {
// Check for recursive data
if visited[v.Interface()] {
return
}
visited[v.Interface()] = true
}
v = v.Elem()
}

switch v.Kind() {

case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
n := &entities.TreeNode{}
typedV := v.Index(i).Interface()
switch typedV.(type) {
case string:
n.Operator = typedV.(string)
root.Operator = n.Operator
continue

case map[string]interface{}:
jsonBody, err := json.Marshal(typedV)
if err != nil {
retErr = err
return
}
condition := entities.Condition{}
if err := json.Unmarshal(jsonBody, &condition); err != nil {
retErr = err
return
}
n.Item = condition
}

root.Nodes = append(root.Nodes, n)

populateConditions(v.Index(i), n)
}
}
}

populateConditions(value, conditionTree)
if conditionTree.Nodes == nil && conditionTree.Operator == "" {
retErr = ErrEmptyTree
conditionTree = nil
}
return conditionTree, retErr
}

// Takes the conditions array from the audience in the datafile and turns it into a condition tree
func buildAudienceConditionTree(conditions interface{}) (conditionTree *entities.TreeNode, err error) {

var operators = []string{"or", "and", "not"} // any other operators?
value := reflect.ValueOf(conditions)
visited := make(map[interface{}]bool)

conditionTree = &entities.TreeNode{}
var populateConditions func(v reflect.Value, root *entities.TreeNode)
populateConditions = func(v reflect.Value, root *entities.TreeNode) {

for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
if v.Kind() == reflect.Ptr {
// Check for recursive data
if visited[v.Interface()] {
return
}
visited[v.Interface()] = true
}
v = v.Elem()
}

switch v.Kind() {

case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
n := &entities.TreeNode{}
typedV := v.Index(i).Interface()
switch typedV.(type) {
case string:
value := typedV.(string)
if stringInSlice(value, operators) {
n.Operator = typedV.(string)
root.Operator = n.Operator
continue
} else {
n.Item = value

}
}

root.Nodes = append(root.Nodes, n)

populateConditions(v.Index(i), n)
}
}
}

populateConditions(value, conditionTree)

if conditionTree.Nodes == nil && conditionTree.Operator == "" {
err = ErrEmptyTree
conditionTree = nil
}

return conditionTree, err
}

func stringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
Loading