From 9f687dc82989686926c80ff252e2303ff905fb98 Mon Sep 17 00:00:00 2001 From: Cole Bailey Date: Mon, 1 Apr 2024 22:40:50 +0200 Subject: [PATCH] update to provide custom seed and target key in single argument Signed-off-by: Cole Bailey --- core/pkg/evaluator/fractional.go | 13 +++--- core/pkg/evaluator/fractional_test.go | 59 +-------------------------- schemas | 2 +- test-harness | 2 +- 4 files changed, 9 insertions(+), 67 deletions(-) diff --git a/core/pkg/evaluator/fractional.go b/core/pkg/evaluator/fractional.go index f97e3e3bc..ecac855e4 100644 --- a/core/pkg/evaluator/fractional.go +++ b/core/pkg/evaluator/fractional.go @@ -56,17 +56,12 @@ func parseFractionalEvaluationData(values, data any) (string, []fractionalEvalua if ok { valuesArray = valuesArray[1:] } else { - bucketBy, ok = dataMap[targetingKeyKey].(string) + targetingKey, ok := dataMap[targetingKeyKey].(string) if !ok { return "", nil, errors.New("bucketing value not supplied and no targetingKey in context") } - } - seed, ok := valuesArray[0].(string) - if ok { - valuesArray = valuesArray[1:] - } else { - seed = properties.FlagKey + bucketBy = fmt.Sprintf("%s%s", properties.FlagKey, targetingKey) } feDistributions, err := parseFractionalEvaluationDistributions(valuesArray) @@ -74,7 +69,7 @@ func parseFractionalEvaluationData(values, data any) (string, []fractionalEvalua return "", nil, err } - return fmt.Sprintf("%s%s", seed, bucketBy), feDistributions, nil + return bucketBy, feDistributions, nil } func parseFractionalEvaluationDistributions(values []any) ([]fractionalEvaluationDistribution, error) { @@ -121,6 +116,8 @@ func distributeValue(value string, feDistribution []fractionalEvaluationDistribu hashRatio := math.Abs(float64(hashValue)) / math.MaxInt32 bucket := int(hashRatio * 100) // in range [0, 100] + fmt.Println(value, feDistribution) + rangeEnd := 0 for _, dist := range feDistribution { rangeEnd += dist.percentage diff --git a/core/pkg/evaluator/fractional_test.go b/core/pkg/evaluator/fractional_test.go index a3aacceb5..516362708 100644 --- a/core/pkg/evaluator/fractional_test.go +++ b/core/pkg/evaluator/fractional_test.go @@ -29,8 +29,7 @@ func TestFractionalEvaluation(t *testing.T) { }, { "fractional": [ - {"var": "email"}, - {"var": "$flagd.flagKey"}, + {"cat": [{"var": "$flagd.flagKey"}, {"var": "email"}]}, [ "red", 25 @@ -70,8 +69,7 @@ func TestFractionalEvaluation(t *testing.T) { }, { "fractional": [ - {"var": "email"}, - "my-seed", + {"cat": ["my-seed", {"var": "email"}]}, ["red",25], ["blue",25], ["green",25], @@ -226,59 +224,6 @@ 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 efefbb441..7e0749365 160000 --- a/schemas +++ b/schemas @@ -1 +1 @@ -Subproject commit efefbb4415078a4e59a2afa8b22cd76aa3f64a50 +Subproject commit 7e074936585057e0960b2e5146fbf94103238fb6 diff --git a/test-harness b/test-harness index 3427ec0df..2c792b813 160000 --- a/test-harness +++ b/test-harness @@ -1 +1 @@ -Subproject commit 3427ec0df1432a97ffd04f896800519bc6c45e46 +Subproject commit 2c792b8137bede503c8fa4026afd0167a18e392f