diff --git a/core/pkg/evaluator/fractional.go b/core/pkg/evaluator/fractional.go index 90d8ac0ca..f97e3e3bc 100644 --- a/core/pkg/evaluator/fractional.go +++ b/core/pkg/evaluator/fractional.go @@ -62,12 +62,19 @@ func parseFractionalEvaluationData(values, data any) (string, []fractionalEvalua } } + seed, ok := valuesArray[0].(string) + if ok { + valuesArray = valuesArray[1:] + } else { + seed = properties.FlagKey + } + feDistributions, err := parseFractionalEvaluationDistributions(valuesArray) if err != nil { return "", nil, err } - return fmt.Sprintf("%s%s", properties.FlagKey, bucketBy), feDistributions, nil + return fmt.Sprintf("%s%s", seed, bucketBy), feDistributions, nil } func parseFractionalEvaluationDistributions(values []any) ([]fractionalEvaluationDistribution, error) { diff --git a/core/pkg/evaluator/fractional_test.go b/core/pkg/evaluator/fractional_test.go index c7153baea..a3aacceb5 100644 --- a/core/pkg/evaluator/fractional_test.go +++ b/core/pkg/evaluator/fractional_test.go @@ -30,6 +30,7 @@ func TestFractionalEvaluation(t *testing.T) { { "fractional": [ {"var": "email"}, + {"var": "$flagd.flagKey"}, [ "red", 25 @@ -51,6 +52,35 @@ func TestFractionalEvaluation(t *testing.T) { ] }`), }, + "customSeededHeaderColor": { + State: "ENABLED", + DefaultVariant: "red", + Variants: map[string]any{ + "red": "#FF0000", + "blue": "#0000FF", + "green": "#00FF00", + "yellow": "#FFFF00", + }, + Targeting: []byte(`{ + "if": [ + { + "in": ["@faas.com", { + "var": ["email"] + }] + }, + { + "fractional": [ + {"var": "email"}, + "my-seed", + ["red",25], + ["blue",25], + ["green",25], + ["yellow",25] + ] + }, null + ] + }`), + }, }, } @@ -103,6 +133,46 @@ func TestFractionalEvaluation(t *testing.T) { expectedValue: "#00FF00", expectedReason: model.TargetingMatchReason, }, + "rachel@faas.com with custom seed": { + flags: flags, + flagKey: "customSeededHeaderColor", + context: map[string]any{ + "email": "rachel@faas.com", + }, + expectedVariant: "green", + expectedValue: "#00FF00", + expectedReason: model.TargetingMatchReason, + }, + "monica@faas.com with custom seed": { + flags: flags, + flagKey: "customSeededHeaderColor", + context: map[string]any{ + "email": "monica@faas.com", + }, + expectedVariant: "red", + expectedValue: "#FF0000", + expectedReason: model.TargetingMatchReason, + }, + "joey@faas.com with custom seed": { + flags: flags, + flagKey: "customSeededHeaderColor", + context: map[string]any{ + "email": "joey@faas.com", + }, + expectedVariant: "green", + expectedValue: "#00FF00", + expectedReason: model.TargetingMatchReason, + }, + "ross@faas.com with custom seed": { + flags: flags, + flagKey: "customSeededHeaderColor", + context: map[string]any{ + "email": "ross@faas.com", + }, + expectedVariant: "green", + expectedValue: "#00FF00", + expectedReason: model.TargetingMatchReason, + }, "ross@faas.com with different flag key": { flags: Flags{ Flags: map[string]model.Flag{ @@ -156,6 +226,59 @@ func TestFractionalEvaluation(t *testing.T) { expectedValue: "#FF0000", expectedReason: model.TargetingMatchReason, }, + "ross@faas.com with default seed": { + flags: Flags{ + Flags: map[string]model.Flag{ + "headerColor": { + State: "ENABLED", + DefaultVariant: "red", + Variants: map[string]any{ + "red": "#FF0000", + "blue": "#0000FF", + "green": "#00FF00", + "yellow": "#FFFF00", + }, + Targeting: []byte(`{ + "if": [ + { + "in": ["@faas.com", { + "var": ["email"] + }] + }, + { + "fractional": [ + {"var": "email"}, + [ + "red", + 25 + ], + [ + "blue", + 25 + ], + [ + "green", + 25 + ], + [ + "yellow", + 25 + ] + ] + }, null + ] + }`), + }, + }, + }, + flagKey: "headerColor", + context: map[string]any{ + "email": "ross@faas.com", + }, + expectedVariant: "green", + expectedValue: "#00FF00", + expectedReason: model.TargetingMatchReason, + }, "non even split": { flags: Flags{ Flags: map[string]model.Flag{ diff --git a/schemas b/schemas index b471b4089..efefbb441 160000 --- a/schemas +++ b/schemas @@ -1 +1 @@ -Subproject commit b471b4089f4c82ab57df358d9166921464e8138e +Subproject commit efefbb4415078a4e59a2afa8b22cd76aa3f64a50 diff --git a/test-harness b/test-harness index 7ab147dbf..3427ec0df 160000 --- a/test-harness +++ b/test-harness @@ -1 +1 @@ -Subproject commit 7ab147dbf80d84ed55b3bd85316cede1035335d3 +Subproject commit 3427ec0df1432a97ffd04f896800519bc6c45e46