From 8beb28d865b2a1f0b54525daa74c9405acf5a513 Mon Sep 17 00:00:00 2001 From: chris-hoefgen Date: Mon, 30 Oct 2023 08:13:06 -0700 Subject: [PATCH] Applying some OpenFeature feedback --- OpenFeature.md | 22 +++++++------- .../devcycle/examples/OpenFeatureExample.java | 29 ++++++++++++------- .../server/openfeature/DevCycleProvider.java | 2 +- .../server/common/model/DevCycleUserTest.java | 20 ++++++------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/OpenFeature.md b/OpenFeature.md index 35b0dfc..1b5c50a 100644 --- a/OpenFeature.md +++ b/OpenFeature.md @@ -25,7 +25,7 @@ api.setProvider(devCycleClient.getOpenFeatureProvider()); Client openFeatureClient = api.getClient(); // Create the evaluation context to use for fetching variable values -EvaluationContext context = new ImmutableContext("user-1234"); +EvaluationContext context = new MutableContext("user-1234"); // Retrieve a boolean flag from the OpenFeature client Boolean variableValue = openFeatureClient.getBooleanValue(VARIABLE_KEY, false, context); @@ -43,23 +43,21 @@ The provider will automatically translate known `DevCycleUser` properties from t For example all these properties will be set on the `DevCycleUser`: ```java -Map attributes = new LinkedHashMap<>(); -attributes.put("email", new Value("email@devcycle.com")); -attributes.put("name", new Value("name")); -attributes.put("country", new Value("CA")); -attributes.put("language", new Value("en")); -attributes.put("appVersion", new Value("1.0.11")); -attributes.put("appBuild", new Value(1000)); +MutableContext context = new MutableContext("test-1234"); +context.add("email", "email@devcycle.com"); +context.add("name", "name"); +context.add("country", "CA"); +context.add("language", "en"); +context.add("appVersion", "1.0.11"); +context.add("appBuild", 1000); Map customData = new LinkedHashMap<>(); customData.put("custom", "value"); -attributes.put("customData", new Value(Structure.mapToStructure(customData))); +context.add("customData", Structure.mapToStructure(customData)); Map privateCustomData = new LinkedHashMap<>(); privateCustomData.put("private", "data"); -attributes.put("privateCustomData", new Value(Structure.mapToStructure(privateCustomData))); - -EvaluationContext context = new ImmutableContext("test-1234", attributes); +context.add("privateCustomData", Structure.mapToStructure(privateCustomData)); ``` Context properties that are not known `DevCycleUser` properties will be automatically diff --git a/src/examples/java/com/devcycle/examples/OpenFeatureExample.java b/src/examples/java/com/devcycle/examples/OpenFeatureExample.java index 96a04b0..9e671cc 100644 --- a/src/examples/java/com/devcycle/examples/OpenFeatureExample.java +++ b/src/examples/java/com/devcycle/examples/OpenFeatureExample.java @@ -35,16 +35,24 @@ public static void main(String[] args) throws InterruptedException { Client openFeatureClient = api.getClient(); // Create the evaluation context to use for fetching variable values - Map attributes = new LinkedHashMap<>(); - attributes.put("email", new Value("test-user@domain.com")); - attributes.put("name", new Value("Test User")); - attributes.put("language", new Value("en")); - attributes.put("country", new Value("CA")); - attributes.put("appVersion", new Value("1.0.0")); - attributes.put("appBuild", new Value("1")); - attributes.put("deviceModel", new Value("Macbook")); - - EvaluationContext context = new ImmutableContext("test-1234", attributes); + MutableContext context = new MutableContext("test-1234"); + context.add("email", "test-user@domain.com"); + context.add("name", "Test User"); + context.add("language", "en"); + context.add("country", "CA"); + context.add("appVersion", "1.0.0"); + context.add("appBuild", "1"); + context.add("deviceModel", "Macbook"); + + // Add Devcycle Custom Data values + Map customData = new LinkedHashMap<>(); + customData.put("custom", "value"); + context.add("customData", Structure.mapToStructure(customData)); + + // Add Devcycle Private Custom Data values + Map privateCustomData = new LinkedHashMap<>(); + privateCustomData.put("private", "data"); + context.add("privateCustomData", Structure.mapToStructure(privateCustomData)); // The default value can be of type string, boolean, number, or JSON Boolean defaultValue = false; @@ -68,7 +76,6 @@ public static void main(String[] args) throws InterruptedException { Value jsonObject = openFeatureClient.getObjectValue("test-json-variable", new Value(Structure.mapToStructure(defaultJsonData)), context); System.out.println(jsonObject.toString()); - // Retrieving a string variable along with the resolution details FlagEvaluationDetails details = openFeatureClient.getStringDetails("doesnt-exist", "default", context); System.out.println("Value: " + details.getValue()); diff --git a/src/main/java/com/devcycle/sdk/server/openfeature/DevCycleProvider.java b/src/main/java/com/devcycle/sdk/server/openfeature/DevCycleProvider.java index 2a0a508..3590d7a 100644 --- a/src/main/java/com/devcycle/sdk/server/openfeature/DevCycleProvider.java +++ b/src/main/java/com/devcycle/sdk/server/openfeature/DevCycleProvider.java @@ -10,7 +10,7 @@ import java.util.Map; public class DevCycleProvider implements FeatureProvider { - private static final String PROVIDER_NAME = "DevCycleProvider"; + private static final String PROVIDER_NAME = "DevCycle"; private final IDevCycleClient devcycleClient; diff --git a/src/test/java/com/devcycle/sdk/server/common/model/DevCycleUserTest.java b/src/test/java/com/devcycle/sdk/server/common/model/DevCycleUserTest.java index 1953c62..99a4d55 100644 --- a/src/test/java/com/devcycle/sdk/server/common/model/DevCycleUserTest.java +++ b/src/test/java/com/devcycle/sdk/server/common/model/DevCycleUserTest.java @@ -1,7 +1,7 @@ package com.devcycle.sdk.server.common.model; import dev.openfeature.sdk.EvaluationContext; -import dev.openfeature.sdk.ImmutableContext; +import dev.openfeature.sdk.MutableContext; import dev.openfeature.sdk.Structure; import dev.openfeature.sdk.Value; import dev.openfeature.sdk.exceptions.TargetingKeyMissingError; @@ -14,7 +14,7 @@ public class DevCycleUserTest { @Test public void testCreateUserNoUserID() { - EvaluationContext ctx = new ImmutableContext(); + MutableContext ctx = new MutableContext(); try { DevCycleUser.fromEvaluationContext(ctx); @@ -24,7 +24,7 @@ public void testCreateUserNoUserID() { } Map attribs = new LinkedHashMap(); - ctx = new ImmutableContext(null, attribs); + ctx = new MutableContext(null, attribs); try { DevCycleUser.fromEvaluationContext(ctx); @@ -35,7 +35,7 @@ public void testCreateUserNoUserID() { attribs = new LinkedHashMap(); attribs.put("user_id", new Value(999)); - ctx = new ImmutableContext(null, attribs); + ctx = new MutableContext(null, attribs); try { DevCycleUser.fromEvaluationContext(ctx); @@ -47,7 +47,7 @@ public void testCreateUserNoUserID() { @Test public void testCreateUserOnlyUserId() { - EvaluationContext ctx = new ImmutableContext("test-1234"); + EvaluationContext ctx = new MutableContext("test-1234"); DevCycleUser user = DevCycleUser.fromEvaluationContext(ctx); Assert.assertEquals(user.getUserId(), "test-1234"); @@ -55,17 +55,17 @@ public void testCreateUserOnlyUserId() { apiAttrs.put("user_id", new Value("test-6789")); // ensure fallback to user_id when target key is null - ctx = new ImmutableContext(null, apiAttrs); + ctx = new MutableContext(null, apiAttrs); user = DevCycleUser.fromEvaluationContext(ctx); Assert.assertEquals(user.getUserId(), "test-6789"); // ensure fallback to user_id when target key is empty - ctx = new ImmutableContext("", apiAttrs); + ctx = new MutableContext("", apiAttrs); user = DevCycleUser.fromEvaluationContext(ctx); Assert.assertEquals(user.getUserId(), "test-6789"); // ensure target key takes precedence over user_id - ctx = new ImmutableContext("user-4567", apiAttrs); + ctx = new MutableContext("user-4567", apiAttrs); user = DevCycleUser.fromEvaluationContext(ctx); Assert.assertEquals(user.getUserId(), "user-4567"); } @@ -81,7 +81,7 @@ public void testCreateUserWithAttributes() { apiAttrs.put("appVersion", new Value("1.0.0")); apiAttrs.put("appBuild", new Value("1")); - EvaluationContext ctx = new ImmutableContext("test-1234", apiAttrs); + EvaluationContext ctx = new MutableContext("test-1234", apiAttrs); DevCycleUser user = DevCycleUser.fromEvaluationContext(ctx); Assert.assertEquals(user.getUserId(), "test-1234"); @@ -112,7 +112,7 @@ public void testCreateUserWithCustomData() { apiAttrs.put("privateCustomData", new Value(Structure.mapToStructure(privateCustomData))); - EvaluationContext ctx = new ImmutableContext("test-1234", apiAttrs); + EvaluationContext ctx = new MutableContext("test-1234", apiAttrs); DevCycleUser user = DevCycleUser.fromEvaluationContext(ctx); Assert.assertEquals(user.getUserId(), "test-1234");