|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2025, 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/v2/pkg/config/datafileprojectconfig/entities" |
| 22 | + "github.com/optimizely/go-sdk/v2/pkg/entities" |
| 23 | +) |
| 24 | + |
| 25 | +// MapHoldouts maps the raw datafile holdout entities to SDK Holdout entities |
| 26 | +// and organizes them by flag relationships |
| 27 | +func MapHoldouts(holdouts []datafileEntities.Holdout, featureMap map[string]entities.Feature) ( |
| 28 | + holdoutList []entities.Holdout, |
| 29 | + holdoutIDMap map[string]entities.Holdout, |
| 30 | + flagHoldoutsMap map[string][]entities.Holdout, |
| 31 | +) { |
| 32 | + holdoutList = []entities.Holdout{} |
| 33 | + holdoutIDMap = make(map[string]entities.Holdout) |
| 34 | + flagHoldoutsMap = make(map[string][]entities.Holdout) |
| 35 | + |
| 36 | + globalHoldouts := []entities.Holdout{} |
| 37 | + includedHoldouts := make(map[string][]entities.Holdout) |
| 38 | + excludedHoldouts := make(map[string][]entities.Holdout) |
| 39 | + |
| 40 | + for _, holdout := range holdouts { |
| 41 | + // Only process running holdouts |
| 42 | + if holdout.Status != string(entities.HoldoutStatusRunning) { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + mappedHoldout := mapHoldout(holdout) |
| 47 | + holdoutList = append(holdoutList, mappedHoldout) |
| 48 | + holdoutIDMap[holdout.ID] = mappedHoldout |
| 49 | + |
| 50 | + // Classify holdout by flag relationships |
| 51 | + if len(holdout.IncludedFlags) == 0 { |
| 52 | + // Global holdout - applies to all flags except excluded |
| 53 | + globalHoldouts = append(globalHoldouts, mappedHoldout) |
| 54 | + |
| 55 | + // Track exclusions |
| 56 | + for _, flagID := range holdout.ExcludedFlags { |
| 57 | + excludedHoldouts[flagID] = append(excludedHoldouts[flagID], mappedHoldout) |
| 58 | + } |
| 59 | + } else { |
| 60 | + // Specific holdout - applies only to included flags |
| 61 | + for _, flagID := range holdout.IncludedFlags { |
| 62 | + includedHoldouts[flagID] = append(includedHoldouts[flagID], mappedHoldout) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Build flagHoldoutsMap by combining global and specific holdouts |
| 68 | + for _, feature := range featureMap { |
| 69 | + flagKey := feature.Key |
| 70 | + flagID := feature.ID |
| 71 | + applicableHoldouts := []entities.Holdout{} |
| 72 | + |
| 73 | + // Add specifically included holdouts |
| 74 | + if included, exists := includedHoldouts[flagID]; exists { |
| 75 | + applicableHoldouts = append(applicableHoldouts, included...) |
| 76 | + } |
| 77 | + |
| 78 | + // Add global holdouts (if not excluded) |
| 79 | + isExcluded := false |
| 80 | + if _, exists := excludedHoldouts[flagID]; exists { |
| 81 | + isExcluded = true |
| 82 | + } |
| 83 | + |
| 84 | + if !isExcluded { |
| 85 | + applicableHoldouts = append(applicableHoldouts, globalHoldouts...) |
| 86 | + } |
| 87 | + |
| 88 | + if len(applicableHoldouts) > 0 { |
| 89 | + flagHoldoutsMap[flagKey] = applicableHoldouts |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return holdoutList, holdoutIDMap, flagHoldoutsMap |
| 94 | +} |
| 95 | + |
| 96 | +func mapHoldout(datafileHoldout datafileEntities.Holdout) entities.Holdout { |
| 97 | + var audienceConditionTree *entities.TreeNode |
| 98 | + var err error |
| 99 | + |
| 100 | + // Build audience condition tree similar to experiments |
| 101 | + if datafileHoldout.AudienceConditions == nil && len(datafileHoldout.AudienceIds) > 0 { |
| 102 | + audienceConditionTree, err = buildAudienceConditionTree(datafileHoldout.AudienceIds) |
| 103 | + } else { |
| 104 | + switch audienceConditions := datafileHoldout.AudienceConditions.(type) { |
| 105 | + case []interface{}: |
| 106 | + if len(audienceConditions) > 0 { |
| 107 | + audienceConditionTree, err = buildAudienceConditionTree(audienceConditions) |
| 108 | + } |
| 109 | + case string: |
| 110 | + if audienceConditions != "" { |
| 111 | + audienceConditionTree, err = buildAudienceConditionTree([]string{audienceConditions}) |
| 112 | + } |
| 113 | + default: |
| 114 | + } |
| 115 | + } |
| 116 | + if err != nil { |
| 117 | + // @TODO: handle error |
| 118 | + func() {}() // cheat the linters |
| 119 | + } |
| 120 | + |
| 121 | + // Map variations |
| 122 | + variations := make(map[string]entities.Variation) |
| 123 | + for _, datafileVariation := range datafileHoldout.Variations { |
| 124 | + variation := mapVariation(datafileVariation) |
| 125 | + variations[variation.ID] = variation |
| 126 | + } |
| 127 | + |
| 128 | + // Map traffic allocations |
| 129 | + trafficAllocation := make([]entities.Range, len(datafileHoldout.TrafficAllocation)) |
| 130 | + for i, allocation := range datafileHoldout.TrafficAllocation { |
| 131 | + trafficAllocation[i] = entities.Range{ |
| 132 | + EntityID: allocation.EntityID, |
| 133 | + EndOfRange: allocation.EndOfRange, |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return entities.Holdout{ |
| 138 | + ID: datafileHoldout.ID, |
| 139 | + Key: datafileHoldout.Key, |
| 140 | + Status: entities.HoldoutStatus(datafileHoldout.Status), |
| 141 | + AudienceIds: datafileHoldout.AudienceIds, |
| 142 | + AudienceConditions: datafileHoldout.AudienceConditions, |
| 143 | + Variations: variations, |
| 144 | + TrafficAllocation: trafficAllocation, |
| 145 | + AudienceConditionTree: audienceConditionTree, |
| 146 | + } |
| 147 | +} |
0 commit comments