|
| 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 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "github.com/optimizely/go-sdk/optimizely/entities" |
| 23 | + "reflect" |
| 24 | +) |
| 25 | + |
| 26 | +var ErrEmptyTree = errors.New("Empty Tree") |
| 27 | + |
| 28 | +// Takes the conditions array from the audience in the datafile and turns it into a condition tree |
| 29 | +func buildConditionTree(conditions interface{}) (conditionTree *entities.ConditionTreeNode, retErr error) { |
| 30 | + |
| 31 | + value := reflect.ValueOf(conditions) |
| 32 | + visited := make(map[interface{}]bool) |
| 33 | + |
| 34 | + conditionTree = &entities.ConditionTreeNode{} |
| 35 | + var populateConditions func(v reflect.Value, root *entities.ConditionTreeNode) |
| 36 | + populateConditions = func(v reflect.Value, root *entities.ConditionTreeNode) { |
| 37 | + |
| 38 | + for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { |
| 39 | + if v.Kind() == reflect.Ptr { |
| 40 | + // Check for recursive data |
| 41 | + if visited[v.Interface()] { |
| 42 | + return |
| 43 | + } |
| 44 | + visited[v.Interface()] = true |
| 45 | + } |
| 46 | + v = v.Elem() |
| 47 | + } |
| 48 | + |
| 49 | + switch v.Kind() { |
| 50 | + |
| 51 | + case reflect.Slice, reflect.Array: |
| 52 | + for i := 0; i < v.Len(); i++ { |
| 53 | + n := &entities.ConditionTreeNode{} |
| 54 | + typedV := v.Index(i).Interface() |
| 55 | + switch typedV.(type) { |
| 56 | + case string: |
| 57 | + n.Operator = typedV.(string) |
| 58 | + root.Operator = n.Operator |
| 59 | + continue |
| 60 | + |
| 61 | + case map[string]interface{}: |
| 62 | + jsonBody, err := json.Marshal(typedV) |
| 63 | + if err != nil { |
| 64 | + retErr = err |
| 65 | + return |
| 66 | + } |
| 67 | + condition := entities.Condition{} |
| 68 | + if err := json.Unmarshal(jsonBody, &condition); err != nil { |
| 69 | + retErr = err |
| 70 | + return |
| 71 | + } |
| 72 | + n.Condition = condition |
| 73 | + } |
| 74 | + |
| 75 | + root.Nodes = append(root.Nodes, n) |
| 76 | + |
| 77 | + populateConditions(v.Index(i), n) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + populateConditions(value, conditionTree) |
| 83 | + if conditionTree.Nodes == nil && conditionTree.Operator == "" { |
| 84 | + retErr = ErrEmptyTree |
| 85 | + conditionTree = nil |
| 86 | + } |
| 87 | + return conditionTree, retErr |
| 88 | +} |
| 89 | + |
| 90 | +// Takes the conditions array from the audience in the datafile and turns it into a condition tree |
| 91 | +func buildAudienceConditionTree(conditions interface{}) (conditionTree *entities.ConditionTreeNode, err error) { |
| 92 | + |
| 93 | + var operators = []string{"or", "and", "not"} // any other operators? |
| 94 | + value := reflect.ValueOf(conditions) |
| 95 | + visited := make(map[interface{}]bool) |
| 96 | + |
| 97 | + conditionTree = &entities.ConditionTreeNode{} |
| 98 | + var populateConditions func(v reflect.Value, root *entities.ConditionTreeNode) |
| 99 | + populateConditions = func(v reflect.Value, root *entities.ConditionTreeNode) { |
| 100 | + |
| 101 | + for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { |
| 102 | + if v.Kind() == reflect.Ptr { |
| 103 | + // Check for recursive data |
| 104 | + if visited[v.Interface()] { |
| 105 | + return |
| 106 | + } |
| 107 | + visited[v.Interface()] = true |
| 108 | + } |
| 109 | + v = v.Elem() |
| 110 | + } |
| 111 | + |
| 112 | + switch v.Kind() { |
| 113 | + |
| 114 | + case reflect.Slice, reflect.Array: |
| 115 | + for i := 0; i < v.Len(); i++ { |
| 116 | + n := &entities.ConditionTreeNode{} |
| 117 | + typedV := v.Index(i).Interface() |
| 118 | + switch typedV.(type) { |
| 119 | + case string: |
| 120 | + value := typedV.(string) |
| 121 | + if stringInSlice(value, operators) { |
| 122 | + n.Operator = typedV.(string) |
| 123 | + root.Operator = n.Operator |
| 124 | + continue |
| 125 | + } else { |
| 126 | + n.Condition.Value = value |
| 127 | + n.Condition.Type = "audience_condition" |
| 128 | + n.Condition.Name = "optimizely_populated" |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + root.Nodes = append(root.Nodes, n) |
| 133 | + |
| 134 | + populateConditions(v.Index(i), n) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + populateConditions(value, conditionTree) |
| 140 | + |
| 141 | + if conditionTree.Nodes == nil && conditionTree.Operator == "" { |
| 142 | + err = ErrEmptyTree |
| 143 | + conditionTree = nil |
| 144 | + } |
| 145 | + |
| 146 | + return conditionTree, err |
| 147 | +} |
| 148 | + |
| 149 | +func stringInSlice(str string, list []string) bool { |
| 150 | + for _, v := range list { |
| 151 | + if v == str { |
| 152 | + return true |
| 153 | + } |
| 154 | + } |
| 155 | + return false |
| 156 | +} |
0 commit comments