|
| 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 | +// HoldoutMaps contains the different holdout mappings for efficient lookup |
| 26 | +type HoldoutMaps struct { |
| 27 | + HoldoutIDMap map[string]entities.Holdout // Map holdout ID to holdout |
| 28 | + GlobalHoldouts []entities.Holdout // Holdouts with no specific flag inclusion |
| 29 | + IncludedHoldouts map[string][]entities.Holdout // Map flag ID to holdouts that include it |
| 30 | + ExcludedHoldouts map[string][]entities.Holdout // Map flag ID to holdouts that exclude it |
| 31 | + FlagHoldoutsMap map[string][]string // Cached map of flag ID to holdout IDs |
| 32 | +} |
| 33 | + |
| 34 | +// MapHoldouts maps the raw datafile holdout entities to SDK Holdout entities |
| 35 | +// and creates the necessary mappings for efficient holdout lookup |
| 36 | +func MapHoldouts(holdouts []datafileEntities.Holdout, audienceMap map[string]entities.Audience) HoldoutMaps { |
| 37 | + holdoutMaps := HoldoutMaps{ |
| 38 | + HoldoutIDMap: make(map[string]entities.Holdout), |
| 39 | + GlobalHoldouts: []entities.Holdout{}, |
| 40 | + IncludedHoldouts: make(map[string][]entities.Holdout), |
| 41 | + ExcludedHoldouts: make(map[string][]entities.Holdout), |
| 42 | + FlagHoldoutsMap: make(map[string][]string), |
| 43 | + } |
| 44 | + |
| 45 | + for _, datafileHoldout := range holdouts { |
| 46 | + // Map variations similar to experiments |
| 47 | + variationMap := make(map[string]entities.Variation) |
| 48 | + variationKeyToIDMap := make(map[string]string) |
| 49 | + |
| 50 | + for _, variation := range datafileHoldout.Variations { |
| 51 | + variableMap := make(map[string]entities.VariationVariable) |
| 52 | + for _, variable := range variation.Variables { |
| 53 | + variableMap[variable.ID] = entities.VariationVariable{ |
| 54 | + ID: variable.ID, |
| 55 | + Value: variable.Value, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + v := entities.Variation{ |
| 60 | + ID: variation.ID, |
| 61 | + Key: variation.Key, |
| 62 | + FeatureEnabled: variation.FeatureEnabled, |
| 63 | + Variables: variableMap, |
| 64 | + } |
| 65 | + variationMap[variation.ID] = v |
| 66 | + variationKeyToIDMap[variation.Key] = variation.ID |
| 67 | + } |
| 68 | + |
| 69 | + // Map traffic allocation |
| 70 | + trafficAllocation := []entities.Range{} |
| 71 | + for _, allocation := range datafileHoldout.TrafficAllocation { |
| 72 | + trafficAllocation = append(trafficAllocation, entities.Range{ |
| 73 | + EntityID: allocation.EntityID, |
| 74 | + EndOfRange: allocation.EndOfRange, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + // Build audience condition tree |
| 79 | + var audienceConditionTree *entities.TreeNode |
| 80 | + if datafileHoldout.AudienceConditions != nil { |
| 81 | + audienceConditionTree, _, _ = buildConditionTree(datafileHoldout.AudienceConditions) |
| 82 | + } else if len(datafileHoldout.AudienceIds) > 0 { |
| 83 | + // Build from audience IDs similar to experiments |
| 84 | + audienceConditionTree, _ = buildAudienceConditionTree(datafileHoldout.AudienceIds) |
| 85 | + } |
| 86 | + |
| 87 | + // Convert status string to HoldoutStatus type |
| 88 | + status := entities.HoldoutStatus(datafileHoldout.Status) |
| 89 | + |
| 90 | + // Create the runtime holdout entity |
| 91 | + holdout := entities.Holdout{ |
| 92 | + ID: datafileHoldout.ID, |
| 93 | + Key: datafileHoldout.Key, |
| 94 | + Status: status, |
| 95 | + Variations: variationMap, |
| 96 | + VariationKeyToIDMap: variationKeyToIDMap, |
| 97 | + TrafficAllocation: trafficAllocation, |
| 98 | + AudienceIds: datafileHoldout.AudienceIds, |
| 99 | + AudienceConditions: datafileHoldout.AudienceConditions, |
| 100 | + AudienceConditionTree: audienceConditionTree, |
| 101 | + IncludedFlags: datafileHoldout.IncludedFlags, |
| 102 | + ExcludedFlags: datafileHoldout.ExcludedFlags, |
| 103 | + } |
| 104 | + |
| 105 | + // Add to ID map |
| 106 | + holdoutMaps.HoldoutIDMap[holdout.ID] = holdout |
| 107 | + |
| 108 | + // Categorize holdouts based on flag targeting |
| 109 | + if len(datafileHoldout.IncludedFlags) == 0 { |
| 110 | + // This is a global holdout (applies to all flags unless excluded) |
| 111 | + holdoutMaps.GlobalHoldouts = append(holdoutMaps.GlobalHoldouts, holdout) |
| 112 | + |
| 113 | + // Add to excluded flags map |
| 114 | + for _, flagID := range datafileHoldout.ExcludedFlags { |
| 115 | + holdoutMaps.ExcludedHoldouts[flagID] = append(holdoutMaps.ExcludedHoldouts[flagID], holdout) |
| 116 | + } |
| 117 | + } else { |
| 118 | + // This holdout specifically includes certain flags |
| 119 | + for _, flagID := range datafileHoldout.IncludedFlags { |
| 120 | + holdoutMaps.IncludedHoldouts[flagID] = append(holdoutMaps.IncludedHoldouts[flagID], holdout) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return holdoutMaps |
| 126 | +} |
| 127 | + |
| 128 | +// GetHoldoutsForFlag returns the holdout IDs that apply to a specific flag |
| 129 | +// This follows the logic from JavaScript SDK: global holdouts (minus excluded) + specifically included |
| 130 | +func GetHoldoutsForFlag(flagID string, holdoutMaps HoldoutMaps) []string { |
| 131 | + // Check cache first |
| 132 | + if cachedHoldoutIDs, exists := holdoutMaps.FlagHoldoutsMap[flagID]; exists { |
| 133 | + return cachedHoldoutIDs |
| 134 | + } |
| 135 | + |
| 136 | + holdoutIDs := []string{} |
| 137 | + |
| 138 | + // Add global holdouts that don't exclude this flag |
| 139 | + for _, holdout := range holdoutMaps.GlobalHoldouts { |
| 140 | + isExcluded := false |
| 141 | + for _, excludedFlagID := range holdout.ExcludedFlags { |
| 142 | + if excludedFlagID == flagID { |
| 143 | + isExcluded = true |
| 144 | + break |
| 145 | + } |
| 146 | + } |
| 147 | + if !isExcluded { |
| 148 | + holdoutIDs = append(holdoutIDs, holdout.ID) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Add holdouts that specifically include this flag |
| 153 | + if includedHoldouts, exists := holdoutMaps.IncludedHoldouts[flagID]; exists { |
| 154 | + for _, holdout := range includedHoldouts { |
| 155 | + holdoutIDs = append(holdoutIDs, holdout.ID) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Cache the result |
| 160 | + holdoutMaps.FlagHoldoutsMap[flagID] = holdoutIDs |
| 161 | + |
| 162 | + return holdoutIDs |
| 163 | +} |
0 commit comments