-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support nested props in fractional evaluator (#869)
<!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> This PR adds the ability to use nested properties for the bucket key in fractional evaluation. It is a breaking change from the current behaviour. If the bucket key is not provided it will default to `{"cat": [{"var":"$flagd.flagKey"}, {"var":"targetingKey"}]}`. @toddbaert I am not sure that my approach to the missing bucket key is the best. I would have preferred to add `{"cat": [{"var":"$flagd.flagKey"}, {"var":"targetingKey"}]}` to the targeting object and let JsonLogic handle it, but it would be a bit clumsy and involve string manipulation. I don't think there is a nice way to do it, but I'll play around a bit more. ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> See #848. Closes #843. ### Notes <!-- any additional notes for this PR --> ### Follow-up Tasks <!-- anything that is related to this PR but not done here should be noted under this section --> <!-- if there is a need for a new issue, please link it here --> ### How to test <!-- if applicable, add testing instructions under this section --> --------- Signed-off-by: Craig Pastro <[email protected]> Co-authored-by: Todd Baert <[email protected]>
- Loading branch information
Showing
4 changed files
with
106 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,22 +29,22 @@ func TestFractionalEvaluation(t *testing.T) { | |
}, | ||
{ | ||
"fractionalEvaluation": [ | ||
"email", | ||
{"var": "email"}, | ||
[ | ||
"red", | ||
25 | ||
"red", | ||
25 | ||
], | ||
[ | ||
"blue", | ||
25 | ||
"blue", | ||
25 | ||
], | ||
[ | ||
"green", | ||
25 | ||
"green", | ||
25 | ||
], | ||
[ | ||
"yellow", | ||
25 | ||
"yellow", | ||
25 | ||
] | ||
] | ||
}, null | ||
|
@@ -69,41 +69,41 @@ func TestFractionalEvaluation(t *testing.T) { | |
context: map[string]any{ | ||
"email": "[email protected]", | ||
}, | ||
expectedVariant: "blue", | ||
expectedValue: "#0000FF", | ||
expectedVariant: "yellow", | ||
expectedValue: "#FFFF00", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
"phoebe@faas.com": { | ||
"monica@faas.com": { | ||
flags: flags, | ||
flagKey: "headerColor", | ||
context: map[string]any{ | ||
"email": "phoebe@faas.com", | ||
"email": "monica@faas.com", | ||
}, | ||
expectedVariant: "yellow", | ||
expectedValue: "#FFFF00", | ||
expectedVariant: "green", | ||
expectedValue: "#00FF00", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
"monica@faas.com": { | ||
"joey@faas.com": { | ||
flags: flags, | ||
flagKey: "headerColor", | ||
context: map[string]any{ | ||
"email": "monica@faas.com", | ||
"email": "joey@faas.com", | ||
}, | ||
expectedVariant: "red", | ||
expectedValue: "#FF0000", | ||
expectedVariant: "blue", | ||
expectedValue: "#0000FF", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
"rossg@faas.com": { | ||
"ross@faas.com": { | ||
flags: flags, | ||
flagKey: "headerColor", | ||
context: map[string]any{ | ||
"email": "rossg@faas.com", | ||
"email": "ross@faas.com", | ||
}, | ||
expectedVariant: "green", | ||
expectedValue: "#00FF00", | ||
expectedVariant: "red", | ||
expectedValue: "#FF0000", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
"rossg@faas.com with different flag key": { | ||
"ross@faas.com with different flag key": { | ||
flags: Flags{ | ||
Flags: map[string]model.Flag{ | ||
"footerColor": { | ||
|
@@ -124,7 +124,7 @@ func TestFractionalEvaluation(t *testing.T) { | |
}, | ||
{ | ||
"fractionalEvaluation": [ | ||
"email", | ||
{"var": "email"}, | ||
[ | ||
"red", | ||
25 | ||
|
@@ -150,10 +150,10 @@ func TestFractionalEvaluation(t *testing.T) { | |
}, | ||
flagKey: "footerColor", | ||
context: map[string]any{ | ||
"email": "rossg@faas.com", | ||
"email": "ross@faas.com", | ||
}, | ||
expectedVariant: "red", | ||
expectedValue: "#FF0000", | ||
expectedVariant: "blue", | ||
expectedValue: "#0000FF", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
"non even split": { | ||
|
@@ -201,8 +201,8 @@ func TestFractionalEvaluation(t *testing.T) { | |
context: map[string]any{ | ||
"email": "[email protected]", | ||
}, | ||
expectedVariant: "red", | ||
expectedValue: "#FF0000", | ||
expectedVariant: "green", | ||
expectedValue: "#00FF00", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
"fallback to default variant if no email provided": { | ||
|
@@ -219,7 +219,7 @@ func TestFractionalEvaluation(t *testing.T) { | |
}, | ||
Targeting: []byte(`{ | ||
"fractionalEvaluation": [ | ||
"email", | ||
{"var": "email"}, | ||
[ | ||
"red", | ||
25 | ||
|
@@ -261,7 +261,7 @@ func TestFractionalEvaluation(t *testing.T) { | |
}, | ||
Targeting: []byte(`{ | ||
"fractionalEvaluation": [ | ||
"email", | ||
{"var": "email"}, | ||
[ | ||
"black", | ||
100 | ||
|
@@ -293,7 +293,7 @@ func TestFractionalEvaluation(t *testing.T) { | |
}, | ||
Targeting: []byte(`{ | ||
"fractionalEvaluation": [ | ||
"email", | ||
{"var": "email"}, | ||
[ | ||
"red", | ||
25 | ||
|
@@ -315,6 +315,41 @@ func TestFractionalEvaluation(t *testing.T) { | |
expectedValue: "#FF0000", | ||
expectedReason: model.DefaultReason, | ||
}, | ||
"default to targetingKey if no bucket key provided": { | ||
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(`{ | ||
"fractionalEvaluation": [ | ||
[ | ||
"blue", | ||
50 | ||
], | ||
[ | ||
"green", | ||
50 | ||
] | ||
] | ||
}`), | ||
}, | ||
}, | ||
}, | ||
flagKey: "headerColor", | ||
context: map[string]any{ | ||
"targetingKey": "[email protected]", | ||
}, | ||
expectedVariant: "green", | ||
expectedValue: "#00FF00", | ||
expectedReason: model.TargetingMatchReason, | ||
}, | ||
} | ||
const reqID = "default" | ||
for name, tt := range tests { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters