Skip to content

Commit 68f77f8

Browse files
committed
feat: Change fractional custom op from percentage-based to relative weighting. #828
Signed-off-by: Simon Schrottner <[email protected]>
1 parent cf77d56 commit 68f77f8

File tree

2 files changed

+45
-10
lines changed
  • providers/flagd/src
    • main/java/dev/openfeature/contrib/providers/flagd/resolver/process/targeting
    • test/java/dev/openfeature/contrib/providers/flagd/resolver/process/targeting

2 files changed

+45
-10
lines changed

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/process/targeting/Fractional.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,16 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
5151

5252
final List<FractionProperty> propertyList = new ArrayList<>();
5353

54-
double distribution = 0;
5554
try {
5655
for (Object dist : distibutions) {
5756
FractionProperty fractionProperty = new FractionProperty(dist);
5857
propertyList.add(fractionProperty);
59-
distribution += fractionProperty.getPercentage();
6058
}
6159
} catch (JsonLogicException e) {
6260
log.debug("Error parsing fractional targeting rule", e);
6361
return null;
6462
}
6563

66-
if (distribution != 100) {
67-
log.debug("Fractional properties do not sum to 100");
68-
return null;
69-
}
70-
7164
// find distribution
7265
return distributeValue(bucketBy, propertyList);
7366
}

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/resolver/process/targeting/FractionalTest.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void targetingBackedFractional() throws JsonLogicEvaluationException {
151151

152152

153153
@Test
154-
void invalidRuleSumNot100() throws JsonLogicEvaluationException {
154+
void invalidRuleSumGreater100() throws JsonLogicEvaluationException {
155155
// given
156156
Fractional fractional = new Fractional();
157157

@@ -189,7 +189,49 @@ void invalidRuleSumNot100() throws JsonLogicEvaluationException {
189189
Object evaluate = fractional.evaluate(rule, data);
190190

191191
// then
192-
assertNull(evaluate);
192+
assertEquals("blue", evaluate);
193+
}
194+
195+
@Test
196+
void invalidRuleSumlower100() throws JsonLogicEvaluationException {
197+
// given
198+
Fractional fractional = new Fractional();
199+
200+
/* Rule
201+
* [
202+
* [
203+
* "blue",
204+
* 50
205+
* ],
206+
* [
207+
* "green",
208+
* 30
209+
* ]
210+
* ]
211+
* */
212+
213+
final List<Object> rule = new ArrayList<>();
214+
215+
final List<Object> bucket1 = new ArrayList<>();
216+
bucket1.add("blue");
217+
bucket1.add(50);
218+
219+
final List<Object> bucket2 = new ArrayList<>();
220+
bucket2.add("green");
221+
bucket2.add(70);
222+
223+
rule.add(bucket1);
224+
rule.add(bucket2);
225+
226+
Map<String, String> data = new HashMap<>();
227+
data.put(FLAG_KEY, "headerColor");
228+
data.put(TARGET_KEY, "[email protected]");
229+
230+
// when
231+
Object evaluate = fractional.evaluate(rule, data);
232+
233+
// then
234+
assertEquals("blue", evaluate);
193235
}
194236

195237
@Test
@@ -266,4 +308,4 @@ void invalidRule() throws JsonLogicEvaluationException {
266308
assertNull(evaluate);
267309
}
268310

269-
}
311+
}

0 commit comments

Comments
 (0)