-
Notifications
You must be signed in to change notification settings - Fork 112
fix: handle missing/null targeting keys in fractional evaluator #1949
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,10 @@ func (fe *Fractional) Evaluate(values, data any) any { | |
| return nil | ||
| } | ||
|
|
||
| if feDistributions == nil { | ||
| return nil | ||
| } | ||
|
|
||
| hashValue := uint32(murmur3.StringSum32(valueToDistribute)) | ||
| return distributeValue(hashValue, feDistributions) | ||
| } | ||
|
|
@@ -78,11 +82,18 @@ if len(valuesArray) < 1 { | |
| valuesArray = valuesArray[1:] | ||
| } | ||
|
|
||
| if dataMap[targetingKeyKey] == nil { | ||
| return "", nil, nil | ||
| } | ||
| targetingKey, ok := dataMap[targetingKeyKey].(string) | ||
| if !ok { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a suggestion: You could also simply add a check here for an empty string. |
||
| return "", nil, fmt.Errorf("flag %q: bucketing value not supplied and no targetingKey in context", flagKey) | ||
| } | ||
|
|
||
| if targetingKey == "" { | ||
| return "", nil, nil | ||
| } | ||
|
|
||
| bucketBy = fmt.Sprintf("%s%s", properties.FlagKey, targetingKey) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -442,6 +442,67 @@ func TestFractionalEvaluation(t *testing.T) { | |
| expectedValue: blueHex, | ||
| expectedReason: model.TargetingMatchReason, | ||
| }, | ||
| "null targetingKey returns default variant": { | ||
| flags: []model.Flag{{ | ||
| Key: "headerColor", | ||
| State: "ENABLED", | ||
| DefaultVariant: redVariant, | ||
| Variants: colorVariants, | ||
| Targeting: []byte(`{ | ||
| "fractional": [ | ||
| ["blue", 50], | ||
| ["green", 50] | ||
| ] | ||
| }`), | ||
| }}, | ||
| flagKey: "headerColor", | ||
| context: map[string]any{ | ||
| "targetingKey": nil, | ||
| }, | ||
| expectedVariant: redVariant, | ||
| expectedValue: redHex, | ||
| expectedReason: model.DefaultReason, | ||
| }, | ||
| "missing targetingKey returns default variant": { | ||
| flags: []model.Flag{{ | ||
| Key: "headerColor", | ||
| State: "ENABLED", | ||
| DefaultVariant: redVariant, | ||
| Variants: colorVariants, | ||
| Targeting: []byte(`{ | ||
| "fractional": [ | ||
| ["blue", 50], | ||
| ["green", 50] | ||
| ] | ||
| }`), | ||
| }}, | ||
| flagKey: "headerColor", | ||
| context: map[string]any{}, | ||
| expectedVariant: redVariant, | ||
| expectedValue: redHex, | ||
| expectedReason: model.DefaultReason, | ||
| }, | ||
| "empty targetingKey returns default variant": { | ||
| flags: []model.Flag{{ | ||
| Key: "headerColor", | ||
| State: "ENABLED", | ||
| DefaultVariant: redVariant, | ||
| Variants: colorVariants, | ||
| Targeting: []byte(`{ | ||
| "fractional": [ | ||
| ["blue", 50], | ||
| ["green", 50] | ||
| ] | ||
| }`), | ||
| }}, | ||
| flagKey: "headerColor", | ||
| context: map[string]any{ | ||
| "targetingKey": "", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, this wasn't defined in the issue yet when you opened your PR, but the suggested fix is that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, see the testbed test for this: |
||
| }, | ||
| expectedVariant: redVariant, | ||
| expectedValue: redHex, | ||
| expectedReason: model.DefaultReason, | ||
| }, | ||
| "single-entry always returns the sole variant": { | ||
| flags: []model.Flag{{ | ||
| Key: "headerColor", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this ever be the case?
Edit: Oh yes, I see, with your changes below.