Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set flag key on $flagd.flagKey #492

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Kavindu-Dodan marked this conversation as resolved.
Show resolved Hide resolved
.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 {
Kavindu-Dodan marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading