Skip to content

Commit 2386d16

Browse files
committed
fix fractional hash calculation
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent d19bbae commit 2386d16

File tree

2 files changed

+46
-7
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

+46
-7
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
7777
private static String distributeValue(final String hashKey, final List<FractionProperty> propertyList)
7878
throws JsonLogicEvaluationException {
7979
byte[] bytes = hashKey.getBytes(StandardCharsets.UTF_8);
80-
double bucket =
81-
(Math.abs(Murmur3.hash64(bytes, 0, bytes.length, 0)) * 1.0d / Long.MAX_VALUE) * 100.0d;
80+
int mmrHash = Murmur3.hash32(bytes, 0, bytes.length, 0);
81+
int bucket = (int) ((Math.abs(mmrHash) * 1.0f / Integer.MAX_VALUE) * 100);
8282

83-
double bucketSum = 0;
83+
int bucketSum = 0;
8484
for (FractionProperty p : propertyList) {
8585
bucketSum += p.getPercentage();
8686

@@ -96,7 +96,7 @@ private static String distributeValue(final String hashKey, final List<FractionP
9696
@Getter
9797
private static class FractionProperty {
9898
private final String variant;
99-
private final double percentage;
99+
private final int percentage;
100100

101101
FractionProperty(final Object from) throws JsonLogicException {
102102
if (!(from instanceof List<?>)) {
@@ -120,7 +120,7 @@ private static class FractionProperty {
120120
}
121121

122122
variant = (String) array.get(0);
123-
percentage = ((Number) array.get(1)).doubleValue();
123+
percentage = ((Number) array.get(1)).intValue();
124124
}
125125

126126
}

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

+41-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void fractionalTestA() throws TargetingRuleException {
5454
Object evalVariant = OPERATOR.apply("headerColor", targetingRule, new ImmutableContext(ctxData));
5555

5656
// then
57-
assertEquals("red", evalVariant);
57+
assertEquals("yellow", evalVariant);
5858
}
5959

6060
@Test
@@ -93,7 +93,46 @@ void fractionalTestB() throws TargetingRuleException {
9393
Object evalVariant = OPERATOR.apply("headerColor", targetingRule, new ImmutableContext(ctxData));
9494

9595
// then
96-
assertEquals("yellow", evalVariant);
96+
assertEquals("blue", evalVariant);
97+
}
98+
99+
@Test
100+
void fractionalTestC() throws TargetingRuleException {
101+
// given
102+
103+
// fractional rule with email as expression key
104+
final String targetingRule = "" +
105+
"{\n" +
106+
" \"fractional\": [\n" +
107+
" {\"var\": \"email\"},\n" +
108+
" [\n" +
109+
" \"red\",\n" +
110+
" 25\n" +
111+
" ],\n" +
112+
" [\n" +
113+
" \"blue\",\n" +
114+
" 25\n" +
115+
" ],\n" +
116+
" [\n" +
117+
" \"green\",\n" +
118+
" 25\n" +
119+
" ],\n" +
120+
" [\n" +
121+
" \"yellow\",\n" +
122+
" 25\n" +
123+
" ]\n" +
124+
" ]\n" +
125+
"}";
126+
127+
Map<String, Value> ctxData = new HashMap<>();
128+
ctxData.put("email", new Value("[email protected]"));
129+
130+
131+
// when
132+
Object evalVariant = OPERATOR.apply("headerColor", targetingRule, new ImmutableContext(ctxData));
133+
134+
// then
135+
assertEquals("red", evalVariant);
97136
}
98137

99138
@Test

0 commit comments

Comments
 (0)