Skip to content

Commit

Permalink
fix: set flag key on $flagd.flagKey (#492)
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
toddbaert authored Oct 13, 2023
1 parent d3f5a9d commit 934f934
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package dev.openfeature.contrib.providers.flagd.resolver.process.targeting;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import dev.openfeature.sdk.EvaluationContext;
import io.github.jamsesso.jsonlogic.JsonLogic;
import io.github.jamsesso.jsonlogic.JsonLogicException;
import lombok.Getter;

import java.util.Map;

/**
* Targeting operator wraps JsonLogic handlers and expose a simple API for external layers.
* This helps to isolate external dependencies to this package.
*/
public class Operator {

static final String FLAG_KEY = "$flagKey";
static final String FLAGD_PROPS_KEY = "$flagd";
static final String FLAG_KEY = "flagKey";
static final String TARGET_KEY = "targetingKey";

private final JsonLogic jsonLogicHandler;
Expand All @@ -34,8 +38,10 @@ public Operator() {
*/
public Object apply(final String flagKey, final String targetingRule, final EvaluationContext ctx)
throws TargetingRuleException {
final Map<String, String> flagdProperties = new HashMap<>();
flagdProperties.put(FLAG_KEY, flagKey);
final Map<String, Object> valueMap = ctx.asObjectMap();
valueMap.put(FLAG_KEY, flagKey);
valueMap.put(FLAGD_PROPS_KEY, flagdProperties);

try {
return jsonLogicHandler.apply(targetingRule, valueMap);
Expand All @@ -53,15 +59,18 @@ static class FlagProperties {
if (from instanceof Map) {
Map<?, ?> dataMap = (Map<?, ?>) from;

Object flagKey = dataMap.get(FLAG_KEY);
final Object flagKey = Optional.ofNullable(dataMap.get(FLAGD_PROPS_KEY))
.filter(flagdProps -> flagdProps instanceof Map)
.map(flagdProps -> ((Map<?, ?>)flagdProps).get(FLAG_KEY))
.orElse(null);

if (flagKey instanceof String) {
this.flagKey = (String) flagKey;
} else {
this.flagKey = null;
}

Object targetKey = dataMap.get(TARGET_KEY);
final Object targetKey = dataMap.get(TARGET_KEY);

if (targetKey instanceof String) {
targetingKey = (String) targetKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package dev.openfeature.contrib.providers.flagd.resolver.process.targeting;

import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluationException;
import org.junit.jupiter.api.Test;
import static dev.openfeature.contrib.providers.flagd.resolver.process.targeting.Operator.FLAGD_PROPS_KEY;
import static dev.openfeature.contrib.providers.flagd.resolver.process.targeting.Operator.FLAG_KEY;
import static dev.openfeature.contrib.providers.flagd.resolver.process.targeting.Operator.TARGET_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static dev.openfeature.contrib.providers.flagd.resolver.process.targeting.Operator.FLAG_KEY;
import static dev.openfeature.contrib.providers.flagd.resolver.process.targeting.Operator.TARGET_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;

import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluationException;

class FractionalTest {

Expand Down Expand Up @@ -48,8 +50,10 @@ void selfContainedFractionalA() throws JsonLogicEvaluationException {
rule.add(bucket1);
rule.add(bucket2);

Map<String, String> data = new HashMap<>();
data.put(FLAG_KEY, "flagA");
Map<String, String> flagdProperties = new HashMap<>();
flagdProperties.put(FLAG_KEY, "flagA");
Map<String, Object> data = new HashMap<>();
data.put(FLAGD_PROPS_KEY, flagdProperties);

// when
Object evaluate = fractional.evaluate(rule, data);
Expand Down Expand Up @@ -91,8 +95,10 @@ void selfContainedFractionalB() throws JsonLogicEvaluationException {
rule.add(bucket1);
rule.add(bucket2);

Map<String, String> data = new HashMap<>();
data.put(FLAG_KEY, "flagA");
Map<String, String> flagdProperties = new HashMap<>();
flagdProperties.put(FLAG_KEY, "flagA");
Map<String, Object> data = new HashMap<>();
data.put(FLAGD_PROPS_KEY, flagdProperties);

// when
Object evaluate = fractional.evaluate(rule, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package dev.openfeature.contrib.providers.flagd.resolver.process.targeting;

import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.Value;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.Value;

class OperatorTest {
private static Operator OPERATOR;
Expand All @@ -18,6 +19,20 @@ static void setUp() {
OPERATOR = new Operator();
}

@Test
void flagKeyPresent() throws TargetingRuleException {
// given

// rule asserting $flagd.flagKey equals the flag key
final String targetingRule = "{\"===\":[{\"var\":[\"$flagd.flagKey\"]},\"some-key\"]}";

// when
Object evalVariant = OPERATOR.apply("some-key", targetingRule, new ImmutableContext());

// then
assertEquals(true, evalVariant);
}

@Test
void fractionalTestA() throws TargetingRuleException {
// given
Expand Down

0 comments on commit 934f934

Please sign in to comment.