Skip to content

Commit da5e8a4

Browse files
leoromanovskytypotter
authored andcommitted
fix(03-01): compute correct reason in DDEvaluator instead of hardcoding TARGETING_MATCH
- Add Split parameter to resolveVariant for three-way reason computation - Return STATIC for flags with no rules and no shards (simple flags) - Return SPLIT for flags with no rules but with shards (shard-based flags) - Return TARGETING_MATCH for flags with rules (rule-based flags) - Update 9 test assertions: 7 to STATIC, 2 to SPLIT
1 parent f064e18 commit da5e8a4

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

products/feature-flagging/feature-flagging-api/src/main/java/datadog/trace/api/openfeature/DDEvaluator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public <T> ProviderEvaluation<T> evaluate(
122122
for (final Split split : allocation.splits) {
123123
if (isEmpty(split.shards)) {
124124
return resolveVariant(
125-
target, key, defaultValue, flag, split.variationKey, allocation, context);
125+
target, key, defaultValue, flag, split.variationKey, allocation, split, context);
126126
} else {
127127
if (targetingKey == null) {
128128
return error(defaultValue, ErrorCode.TARGETING_KEY_MISSING);
@@ -137,7 +137,7 @@ public <T> ProviderEvaluation<T> evaluate(
137137
}
138138
if (allShardsMatch) {
139139
return resolveVariant(
140-
target, key, defaultValue, flag, split.variationKey, allocation, context);
140+
target, key, defaultValue, flag, split.variationKey, allocation, split, context);
141141
}
142142
}
143143
}
@@ -333,6 +333,7 @@ private static <T> ProviderEvaluation<T> resolveVariant(
333333
final Flag flag,
334334
final String variationKey,
335335
final Allocation allocation,
336+
final Split split,
336337
final EvaluationContext context) {
337338
final Variant variant = flag.variations.get(variationKey);
338339
if (variant == null) {
@@ -377,7 +378,12 @@ private static <T> ProviderEvaluation<T> resolveVariant(
377378
final ProviderEvaluation<T> result =
378379
ProviderEvaluation.<T>builder()
379380
.value(mappedValue)
380-
.reason(Reason.TARGETING_MATCH.name())
381+
.reason(
382+
!isEmpty(allocation.rules)
383+
? Reason.TARGETING_MATCH.name()
384+
: !isEmpty(split.shards)
385+
? Reason.SPLIT.name()
386+
: Reason.STATIC.name())
381387
.variant(variant.key)
382388
.flagMetadata(metadataBuilder.build())
383389
.build();

products/feature-flagging/feature-flagging-api/src/test/java/datadog/trace/api/openfeature/DDEvaluatorTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import static dev.openfeature.sdk.Reason.DEFAULT;
66
import static dev.openfeature.sdk.Reason.DISABLED;
77
import static dev.openfeature.sdk.Reason.ERROR;
8+
import static dev.openfeature.sdk.Reason.SPLIT;
9+
import static dev.openfeature.sdk.Reason.STATIC;
810
import static dev.openfeature.sdk.Reason.TARGETING_MATCH;
911
import static java.util.Arrays.asList;
1012
import static java.util.Collections.emptyList;
@@ -231,7 +233,7 @@ private static List<TestCase<?>> evaluateTestCases() {
231233
new TestCase<>("default")
232234
.flag("simple-string")
233235
.targetingKey("")
234-
.result(new Result<>("test-value").reason(TARGETING_MATCH.name()).variant("on")),
236+
.result(new Result<>("test-value").reason(STATIC.name()).variant("on")),
235237
new TestCase<>("default")
236238
.flag("non-existent-flag")
237239
.targetingKey("user-123")
@@ -243,15 +245,15 @@ private static List<TestCase<?>> evaluateTestCases() {
243245
new TestCase<>("default")
244246
.flag("simple-string")
245247
.targetingKey("user-123")
246-
.result(new Result<>("test-value").reason(TARGETING_MATCH.name()).variant("on")),
248+
.result(new Result<>("test-value").reason(STATIC.name()).variant("on")),
247249
new TestCase<>(false)
248250
.flag("boolean-flag")
249251
.targetingKey("user-123")
250-
.result(new Result<>(true).reason(TARGETING_MATCH.name()).variant("enabled")),
252+
.result(new Result<>(true).reason(STATIC.name()).variant("enabled")),
251253
new TestCase<>(0)
252254
.flag("integer-flag")
253255
.targetingKey("user-123")
254-
.result(new Result<>(42).reason(TARGETING_MATCH.name()).variant("forty-two")),
256+
.result(new Result<>(42).reason(STATIC.name()).variant("forty-two")),
255257
new TestCase<>("default")
256258
.flag("rule-based-flag")
257259
.targetingKey("user-premium")
@@ -261,7 +263,7 @@ private static List<TestCase<?>> evaluateTestCases() {
261263
.flag("rule-based-flag")
262264
.targetingKey("user-basic")
263265
.context("email", "john@gmail.com")
264-
.result(new Result<>("basic").reason(TARGETING_MATCH.name()).variant("basic")),
266+
.result(new Result<>("basic").reason(STATIC.name()).variant("basic")),
265267
new TestCase<>("default")
266268
.flag("numeric-rule-flag")
267269
.targetingKey("user-vip")
@@ -287,7 +289,7 @@ private static List<TestCase<?>> evaluateTestCases() {
287289
.result(
288290
new Result<>("default")
289291
// Result depends on shard calculation - either match or default
290-
.reason(TARGETING_MATCH.name(), DEFAULT.name())),
292+
.reason(SPLIT.name(), DEFAULT.name())),
291293
// Type mismatch: STRING flag evaluated as Integer
292294
new TestCase<>(0)
293295
.flag("string-number-flag")
@@ -369,7 +371,7 @@ private static List<TestCase<?>> evaluateTestCases() {
369371
.targetingKey("user-123")
370372
.result(
371373
new Result<>("tracked-value")
372-
.reason(TARGETING_MATCH.name())
374+
.reason(STATIC.name())
373375
.variant("tracked")
374376
.flagMetadata("allocationKey", "exposure-alloc")
375377
.flagMetadata("doLog", true)),
@@ -443,7 +445,7 @@ private static List<TestCase<?>> evaluateTestCases() {
443445
.flag("shard-matching-flag")
444446
.targetingKey("specific-key-that-matches-shard")
445447
.result(
446-
new Result<>("shard-matched").reason(TARGETING_MATCH.name()).variant("matched")),
448+
new Result<>("shard-matched").reason(SPLIT.name()).variant("matched")),
447449
new TestCase<>("default")
448450
.flag("future-allocation-flag")
449451
.targetingKey("user-123")

0 commit comments

Comments
 (0)