Skip to content

Commit

Permalink
resolving logic cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan committed Sep 1, 2023
1 parent 014bcc1 commit ee9d3d5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion providers/flagd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>dev.openfeature.contrib.providers</groupId>
<artifactId>flagd</artifactId>
<version>0.6.1</version> <!--x-release-please-version -->
<version>999-snapshot</version> <!--x-release-please-version -->

<properties>
<!-- exclusion expression for e2e tests -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public GrpcConnector(final FlagdOptions options, final Cache cache, Consumer<Pro
*/
public void initialize() throws Exception {
eventObserverThread = new Thread(this::observeEventStream);
eventObserverThread.setDaemon(true);
eventObserverThread.start();

// block till ready
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private <T> ProviderEvaluation<T> resolveGeneric(Class<T> type, String key, T de
EvaluationContext ctx) {
final FeatureFlag flag = flagStore.getFLag(key);

// missing flag
if (flag == null) {
return ProviderEvaluation.<T>builder()
.value(defaultValue)
Expand All @@ -84,7 +85,7 @@ private <T> ProviderEvaluation<T> resolveGeneric(Class<T> type, String key, T de
if ("DISABLED".equals(flag.getState())) {
return ProviderEvaluation.<T>builder()
.value(defaultValue)
.reason(Reason.ERROR.toString())
.reason(Reason.DISABLED.toString())
.errorCode(ErrorCode.GENERAL)
.errorMessage(String.format("requested flag is disabled: %s", key))
.build();
Expand All @@ -94,25 +95,45 @@ private <T> ProviderEvaluation<T> resolveGeneric(Class<T> type, String key, T de
final Object resolvedVariant;
final String reason;

if (flag.getTargeting() != "{}") {
if ("{}".equals(flag.getTargeting())) {
resolvedVariant = flag.getDefaultVariant();
reason = Reason.STATIC.toString();
} else {
try {
resolvedVariant = jsonLogicHandler.apply(flag.getTargeting(), ctx.asObjectMap());
reason = Reason.TARGETING_MATCH.toString();
final Object jsonResolved = jsonLogicHandler.apply(flag.getTargeting(), ctx.asObjectMap());
if (jsonResolved == null) {
resolvedVariant = flag.getDefaultVariant();
reason = Reason.DEFAULT.toString();
} else {
resolvedVariant = jsonResolved;
reason = Reason.TARGETING_MATCH.toString();
}
} catch (JsonLogicException e) {
log.log(Level.INFO, "Error evaluating targeting rule", e);
log.log(Level.FINE, "Error evaluating targeting rule", e);
return ProviderEvaluation.<T>builder()
.value(defaultValue)
.reason(Reason.ERROR.toString())
.errorCode(ErrorCode.PARSE_ERROR)
.errorMessage(String.format("error parsing targeting rule: %s", key))
.build();
}
} else {
resolvedVariant = flag.getVariants().get(flag.getDefaultVariant());
reason = Reason.STATIC.toString();
}

if (!resolvedVariant.getClass().isAssignableFrom(type)) {
// check variant existence
Object value = flag.getVariants().get(resolvedVariant);
if (value == null){
log.log(Level.FINE, String.format("variant %s not found in flag with key %s", resolvedVariant, key));
return ProviderEvaluation.<T>builder()
.value(defaultValue)
.reason(Reason.ERROR.toString())
.errorCode(ErrorCode.TYPE_MISMATCH)
.errorMessage(String.format("requested flag is not of the evaluation type: %s", type.getName()))
.build();
}

if (!value.getClass().isAssignableFrom(type) || !(resolvedVariant instanceof String)) {
log.log(Level.FINE, String.format("returning default variant for flagKey: %s, type not valid", key));

return ProviderEvaluation.<T>builder()
.value(defaultValue)
.reason(Reason.ERROR.toString())
Expand All @@ -122,7 +143,8 @@ private <T> ProviderEvaluation<T> resolveGeneric(Class<T> type, String key, T de
}

return ProviderEvaluation.<T>builder()
.value((T) resolvedVariant)
.value((T) value)
.variant((String) resolvedVariant)
.reason(reason)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ public GrpcStreamConnector(final FlagdOptions options) {
}

public void init(Consumer<String> callback) {
new Thread(() -> {
Thread listener = new Thread(() -> {
try {
observeEventStream(callback, shutdown, serviceStub);
} catch (InterruptedException e) {
log.log(Level.WARNING, "Event stream interrupted, flag configurations are stale", e);
}
}).start();
});

listener.setDaemon(true);
listener.start();
}

public void shutdown() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package dev.openfeature.contrib.providers.flagd.resolver.process;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import io.github.jamsesso.jsonlogic.JsonLogic;
import io.github.jamsesso.jsonlogic.JsonLogicException;
import org.junit.jupiter.api.Test;

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

class InProcessResolverTest {

@Test
Expand All @@ -23,32 +16,11 @@ public void testInitializer() throws Exception {

resolver.init();

// delay for init
Thread.sleep(2000);

resolver.booleanEvaluation("booleanFlag", false, null);
resolver.integerEvaluation("booleanFlag", 1, null);

Thread.sleep(100_000);
}

@Test
public void jsonEvaluator() throws JsonProcessingException {
String logic = "{\"if\":[{\"in\":[\"@faas.com\",{\"var\":[\"email\"]}]},\"binet\",null]}";

ObjectMapper mapper = new ObjectMapper();

Map<String, Object> ctx = new HashMap<>();
ctx.put("email", "abc@faas.com");

JsonLogic jsonLogic = new JsonLogic();

try {
Object result = jsonLogic.apply(logic, ctx);

System.out.println(result);
} catch (JsonLogicException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit ee9d3d5

Please sign in to comment.