Skip to content

[FSSDK-11551] Adding project config support for holdouts to go-sdk #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions pkg/config/datafileprojectconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ func NewDatafileProjectConfig(jsonDatafile []byte, logger logging.OptimizelyLogP
audienceMap, audienceSegmentList := mappers.MapAudiences(append(datafile.TypedAudiences, datafile.Audiences...))
flagVariationsMap := mappers.MapFlagVariations(featureMap)

// Process holdouts and populate HoldoutIDs for features
holdoutMaps := mappers.MapHoldouts(datafile.Holdouts, audienceMap)
for featureKey, feature := range featureMap {
feature.HoldoutIDs = mappers.GetHoldoutsForFlag(feature.ID, holdoutMaps)
featureMap[featureKey] = feature
}

attributeKeyMap := make(map[string]entities.Attribute)
attributeIDToKeyMap := make(map[string]string)

Expand Down
42 changes: 36 additions & 6 deletions pkg/config/datafileprojectconfig/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ type Cmab struct {
TrafficAllocation int `json:"trafficAllocation"`
}

// Experiment represents an Experiment object from the Optimizely datafile
type Experiment struct {
// ExperimentCore contains the shared properties between Experiment and Holdout
// This represents the common bucketing and targeting logic
type ExperimentCore struct {
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"`
Cmab *Cmab `json:"cmab,omitempty"` // is optional
}

// Experiment represents an Experiment object from the Optimizely datafile
type Experiment struct {
ExperimentCore
LayerID string `json:"layerId"`
Status string `json:"status"`
ForcedVariations map[string]string `json:"forcedVariations"`
Cmab *Cmab `json:"cmab,omitempty"` // is optional
}

// Group represents an Group object from the Optimizely datafile
Expand All @@ -62,6 +68,29 @@ type Group struct {
Experiments []Experiment `json:"experiments"`
}

// HoldoutStatus represents the status of a holdout
type HoldoutStatus string

const (
// HoldoutStatusDraft - the holdout status is draft
HoldoutStatusDraft HoldoutStatus = "Draft"
// HoldoutStatusRunning - the holdout status is running
HoldoutStatusRunning HoldoutStatus = "Running"
// HoldoutStatusConcluded - the holdout status is concluded
HoldoutStatusConcluded HoldoutStatus = "Concluded"
// HoldoutStatusArchived - the holdout status is archived
HoldoutStatusArchived HoldoutStatus = "Archived"
)

// Holdout represents a Holdout object from the Optimizely datafile
// Holdouts share core properties with Experiments through ExperimentCore embedding
type Holdout struct {
ExperimentCore
Status HoldoutStatus `json:"status"`
IncludedFlags []string `json:"includedFlags"`
ExcludedFlags []string `json:"excludedFlags"`
}

// FeatureFlag represents a FeatureFlag object from the Optimizely datafile
type FeatureFlag struct {
ID string `json:"id"`
Expand Down Expand Up @@ -132,6 +161,7 @@ type Datafile struct {
Integrations []Integration `json:"integrations"`
TypedAudiences []Audience `json:"typedAudiences"`
Variables []string `json:"variables"`
Holdouts []Holdout `json:"holdouts"`
AccountID string `json:"accountId"`
ProjectID string `json:"projectId"`
Revision string `json:"revision"`
Expand Down
44 changes: 28 additions & 16 deletions pkg/config/datafileprojectconfig/mappers/experiment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ func TestMapExperiments(t *testing.T) {
func TestMapExperimentsWithStringAudienceCondition(t *testing.T) {

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

rawExperiments := []datafileEntities.Experiment{rawExperiment}
Expand Down Expand Up @@ -167,7 +169,9 @@ func TestMapExperimentsWithStringAudienceCondition(t *testing.T) {
func TestMergeExperiments(t *testing.T) {

rawExperiment := datafileEntities.Experiment{
ID: "11111",
ExperimentCore: datafileEntities.ExperimentCore{
ID: "11111",
},
}
rawGroup := datafileEntities.Group{
Policy: "random",
Expand All @@ -184,7 +188,9 @@ func TestMergeExperiments(t *testing.T) {
},
Experiments: []datafileEntities.Experiment{
{
ID: "11112",
ExperimentCore: datafileEntities.ExperimentCore{
ID: "11112",
},
},
},
}
Expand All @@ -195,10 +201,14 @@ func TestMergeExperiments(t *testing.T) {

expectedExperiments := []datafileEntities.Experiment{
{
ID: "11111",
ExperimentCore: datafileEntities.ExperimentCore{
ID: "11111",
},
},
{
ID: "11112",
ExperimentCore: datafileEntities.ExperimentCore{
ID: "11112",
},
},
}

Expand Down Expand Up @@ -302,15 +312,17 @@ func TestMapCmab(t *testing.T) {
func TestMapExperimentWithCmab(t *testing.T) {
// Create a raw experiment with CMAB configuration
rawExperiment := datafileEntities.Experiment{
ID: "exp1",
Key: "experiment_1",
LayerID: "layer1",
Variations: []datafileEntities.Variation{
{ID: "var1", Key: "variation_1"},
},
TrafficAllocation: []datafileEntities.TrafficAllocation{
{EntityID: "var1", EndOfRange: 10000},
ExperimentCore: datafileEntities.ExperimentCore{
ID: "exp1",
Key: "experiment_1",
Variations: []datafileEntities.Variation{
{ID: "var1", Key: "variation_1"},
},
TrafficAllocation: []datafileEntities.TrafficAllocation{
{EntityID: "var1", EndOfRange: 10000},
},
},
LayerID: "layer1",
Cmab: &datafileEntities.Cmab{
AttributeIds: []string{"attr1", "attr2"},
TrafficAllocation: 5000, // Changed from array to int
Expand Down
1 change: 1 addition & 0 deletions pkg/config/datafileprojectconfig/mappers/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func MapFeatures(featureFlags []datafileEntities.FeatureFlag, rolloutMap map[str
feature.ExperimentIDs = featureFlag.ExperimentIDs
feature.FeatureExperiments = featureExperiments
feature.VariableMap = variableMap
// HoldoutIDs will be populated later when holdouts are processed from the datafile
featureMap[featureFlag.Key] = feature
}
return featureMap
Expand Down
35 changes: 35 additions & 0 deletions pkg/config/datafileprojectconfig/mappers/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestMapFeatures(t *testing.T) {
Rollout: rollout,
FeatureExperiments: []entities.Experiment{experiment31111},
VariableMap: map[string]entities.Variable{variable.Key: variable},
HoldoutIDs: []string(nil),
},
}
expectedExperimentMap := map[string]entities.Experiment{
Expand All @@ -77,3 +78,37 @@ func TestMapFeatures(t *testing.T) {
assert.Equal(t, expectedFeatureMap, featureMap)
assert.Equal(t, expectedExperimentMap, experimentMap)
}

func TestMapFeaturesWithHoldoutIds(t *testing.T) {
const testFeatureFlagString = `{
"id": "22222",
"key": "test_feature_22222",
"rolloutId": "42222",
"experimentIds": ["32222"],
"variables": [{"defaultValue":"test","id":"2","key":"var","type":"string"}]
}`

var rawFeatureFlag datafileEntities.FeatureFlag
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal([]byte(testFeatureFlagString), &rawFeatureFlag)

rawFeatureFlags := []datafileEntities.FeatureFlag{rawFeatureFlag}
rollout := entities.Rollout{ID: "42222"}
rolloutMap := map[string]entities.Rollout{
"42222": rollout,
}
experiment32222 := entities.Experiment{ID: "32222"}
experimentMap := map[string]entities.Experiment{
"32222": experiment32222,
}
featureMap := MapFeatures(rawFeatureFlags, rolloutMap, experimentMap)

// Verify that the feature is created properly
// HoldoutIDs will be populated later when holdouts are processed from the datafile
feature := featureMap["test_feature_22222"]

// For now, HoldoutIDs should be nil/empty since they're not in the datafile directly
assert.Nil(t, feature.HoldoutIDs)
assert.Equal(t, "22222", feature.ID)
assert.Equal(t, "test_feature_22222", feature.Key)
}
113 changes: 113 additions & 0 deletions pkg/config/datafileprojectconfig/mappers/holdout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/****************************************************************************
* Copyright 2025, 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 ...
package mappers

import (
datafileEntities "github.com/optimizely/go-sdk/v2/pkg/config/datafileprojectconfig/entities"
"github.com/optimizely/go-sdk/v2/pkg/entities"
)

// HoldoutMaps contains the different holdout mappings for efficient lookup
type HoldoutMaps struct {
HoldoutIDMap map[string]entities.Holdout // Map holdout ID to holdout
GlobalHoldouts []entities.Holdout // Holdouts with no specific flag inclusion
IncludedHoldouts map[string][]entities.Holdout // Map flag ID to holdouts that include it
ExcludedHoldouts map[string][]entities.Holdout // Map flag ID to holdouts that exclude it
FlagHoldoutsMap map[string][]string // Cached map of flag ID to holdout IDs
}

// MapHoldouts maps the raw datafile holdout entities to SDK Holdout entities
// and creates the necessary mappings for efficient holdout lookup
func MapHoldouts(holdouts []datafileEntities.Holdout, audienceMap map[string]entities.Audience) HoldoutMaps {
holdoutMaps := HoldoutMaps{
HoldoutIDMap: make(map[string]entities.Holdout),
GlobalHoldouts: []entities.Holdout{},
IncludedHoldouts: make(map[string][]entities.Holdout),
ExcludedHoldouts: make(map[string][]entities.Holdout),
FlagHoldoutsMap: make(map[string][]string),
}

for _, datafileHoldout := range holdouts {
// Create minimal runtime holdout entity - only what's needed for flag dependency
holdout := entities.Holdout{
ID: datafileHoldout.ID,
Key: datafileHoldout.Key,
Status: entities.HoldoutStatus(datafileHoldout.Status),
IncludedFlags: datafileHoldout.IncludedFlags,
ExcludedFlags: datafileHoldout.ExcludedFlags,
}

// Add to ID map
holdoutMaps.HoldoutIDMap[holdout.ID] = holdout

// Categorize holdouts based on flag targeting
if len(datafileHoldout.IncludedFlags) == 0 {
// This is a global holdout (applies to all flags unless excluded)
holdoutMaps.GlobalHoldouts = append(holdoutMaps.GlobalHoldouts, holdout)

// Add to excluded flags map
for _, flagID := range datafileHoldout.ExcludedFlags {
holdoutMaps.ExcludedHoldouts[flagID] = append(holdoutMaps.ExcludedHoldouts[flagID], holdout)
}
} else {
// This holdout specifically includes certain flags
for _, flagID := range datafileHoldout.IncludedFlags {
holdoutMaps.IncludedHoldouts[flagID] = append(holdoutMaps.IncludedHoldouts[flagID], holdout)
}
}
}

return holdoutMaps
}

// GetHoldoutsForFlag returns the holdout IDs that apply to a specific flag
// This follows the logic from JavaScript SDK: global holdouts (minus excluded) + specifically included
func GetHoldoutsForFlag(flagID string, holdoutMaps HoldoutMaps) []string {
// Check cache first
if cachedHoldoutIDs, exists := holdoutMaps.FlagHoldoutsMap[flagID]; exists {
return cachedHoldoutIDs
}

holdoutIDs := []string{}

// Add global holdouts that don't exclude this flag
for _, holdout := range holdoutMaps.GlobalHoldouts {
isExcluded := false
for _, excludedFlagID := range holdout.ExcludedFlags {
if excludedFlagID == flagID {
isExcluded = true
break
}
}
if !isExcluded {
holdoutIDs = append(holdoutIDs, holdout.ID)
}
}

// Add holdouts that specifically include this flag
if includedHoldouts, exists := holdoutMaps.IncludedHoldouts[flagID]; exists {
for _, holdout := range includedHoldouts {
holdoutIDs = append(holdoutIDs, holdout.ID)
}
}

// Cache the result
holdoutMaps.FlagHoldoutsMap[flagID] = holdoutIDs

return holdoutIDs
}
Loading