Skip to content

Commit

Permalink
add optional custom seed override for fractional op
Browse files Browse the repository at this point in the history
Signed-off-by: Cole Bailey <[email protected]>
  • Loading branch information
colebaileygit committed Apr 3, 2024
1 parent 57ef518 commit b97d0f8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
9 changes: 8 additions & 1 deletion core/pkg/evaluator/fractional.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ func parseFractionalEvaluationData(values, data any) (string, []fractionalEvalua
bucketBy = fmt.Sprintf("%s%s", properties.FlagKey, targetingKey)
}

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 bucketBy, feDistributions, nil
return fmt.Sprintf("%s%s", seed, bucketBy), feDistributions, nil
}

func parseFractionalEvaluationDistributions(values []any) ([]fractionalEvaluationDistribution, error) {
Expand Down
59 changes: 57 additions & 2 deletions core/pkg/evaluator/fractional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestFractionalEvaluation(t *testing.T) {
},
{
"fractional": [
{"cat": [{"var": "$flagd.flagKey"}, {"var": "email"}]},
{"var": "email"},
{"var": "$flagd.flagKey"},
[
"red",
25
Expand Down Expand Up @@ -69,7 +70,8 @@ func TestFractionalEvaluation(t *testing.T) {
},
{
"fractional": [
{"cat": ["my-seed", {"var": "email"}]},
{"var": "email"},
"my-seed",
["red",25],
["blue",25],
["green",25],
Expand Down Expand Up @@ -224,6 +226,59 @@ func TestFractionalEvaluation(t *testing.T) {
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected] 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": "[email protected]",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"non even split": {
flags: Flags{
Flags: map[string]model.Flag{
Expand Down

0 comments on commit b97d0f8

Please sign in to comment.