Skip to content

Commit 5fa86c6

Browse files
leakonvalinkatoddbaertgemini-code-assist[bot]
authored
fix: allow single entry in fractional operator (#1935)
<!-- 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 --> - allows single entries in the fractional operator ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> #1924 ### 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: Lea Konvalinka <lea.konvalinka@dynatrace.com> Signed-off-by: Todd Baert <todd.baert@dynatrace.com> Co-authored-by: Todd Baert <todd.baert@dynatrace.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 8b9db9f commit 5fa86c6

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

core/pkg/evaluator/fractional.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func parseFractionalEvaluationData(values, data any, logger *logger.Logger) (str
5757
if !ok {
5858
return "", nil, errors.New("fractional evaluation data is not an array")
5959
}
60-
if len(valuesArray) < 2 {
61-
return "", nil, errors.New("fractional evaluation data has length under 2")
60+
if len(valuesArray) < 1 {
61+
return "", nil, errors.New("fractional evaluation data must contain at least one distribution")
6262
}
6363

6464
dataMap, ok := data.(map[string]any)

core/pkg/evaluator/fractional_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,67 @@ func TestFractionalEvaluation(t *testing.T) {
442442
expectedValue: blueHex,
443443
expectedReason: model.TargetingMatchReason,
444444
},
445+
"single-entry always returns the sole variant": {
446+
flags: []model.Flag{{
447+
Key: "headerColor",
448+
State: "ENABLED",
449+
DefaultVariant: redVariant,
450+
Variants: colorVariants,
451+
Targeting: []byte(`{
452+
"fractional": [
453+
["blue", 1]
454+
]
455+
}`),
456+
}},
457+
flagKey: "headerColor",
458+
context: map[string]any{
459+
"targetingKey": "any-user",
460+
},
461+
expectedVariant: blueVariant,
462+
expectedValue: blueHex,
463+
expectedReason: model.TargetingMatchReason,
464+
},
465+
"single-entry with explicit bucket-by always returns the sole variant": {
466+
flags: []model.Flag{{
467+
Key: "headerColor",
468+
State: "ENABLED",
469+
DefaultVariant: redVariant,
470+
Variants: colorVariants,
471+
Targeting: []byte(`{
472+
"fractional": [
473+
{"var": "email"},
474+
["green", 100]
475+
]
476+
}`),
477+
}},
478+
flagKey: "headerColor",
479+
context: map[string]any{
480+
"email": "any@user.com",
481+
},
482+
expectedVariant: greenVariant,
483+
expectedValue: greenHex,
484+
expectedReason: model.TargetingMatchReason,
485+
},
486+
"single-entry shorthand without weight always returns the sole variant": {
487+
flags: []model.Flag{{
488+
Key: "headerColor",
489+
State: "ENABLED",
490+
DefaultVariant: redVariant,
491+
Variants: colorVariants,
492+
Targeting: []byte(`{
493+
"fractional": [
494+
["yellow"]
495+
]
496+
}`),
497+
}},
498+
flagKey: "headerColor",
499+
context: map[string]any{
500+
"targetingKey": "any-user",
501+
},
502+
expectedVariant: yellowVariant,
503+
expectedValue: yellowHex,
504+
expectedReason: model.TargetingMatchReason,
505+
},
445506
}
446507
const reqID = "default"
447508
for name, tt := range tests {

0 commit comments

Comments
 (0)