Skip to content

Commit

Permalink
Applying some OpenFeature feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-hoefgen committed Oct 30, 2023
1 parent 0c6534f commit 8beb28d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
22 changes: 10 additions & 12 deletions OpenFeature.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String, Value> 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<String,Object> customData = new LinkedHashMap<>();
customData.put("custom", "value");
attributes.put("customData", new Value(Structure.mapToStructure(customData)));
context.add("customData", Structure.mapToStructure(customData));

Map<String,Object> 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
Expand Down
29 changes: 18 additions & 11 deletions src/examples/java/com/devcycle/examples/OpenFeatureExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Value> 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<String,Object> customData = new LinkedHashMap<>();
customData.put("custom", "value");
context.add("customData", Structure.mapToStructure(customData));

// Add Devcycle Private Custom Data values
Map<String,Object> 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;
Expand All @@ -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<String> details = openFeatureClient.getStringDetails("doesnt-exist", "default", context);
System.out.println("Value: " + details.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +14,7 @@ public class DevCycleUserTest {

@Test
public void testCreateUserNoUserID() {
EvaluationContext ctx = new ImmutableContext();
MutableContext ctx = new MutableContext();

try {
DevCycleUser.fromEvaluationContext(ctx);
Expand All @@ -24,7 +24,7 @@ public void testCreateUserNoUserID() {
}

Map<String, Value> attribs = new LinkedHashMap();
ctx = new ImmutableContext(null, attribs);
ctx = new MutableContext(null, attribs);

try {
DevCycleUser.fromEvaluationContext(ctx);
Expand All @@ -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);
Expand All @@ -47,25 +47,25 @@ 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");

Map<String, Value> apiAttrs = new LinkedHashMap();
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");
}
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 8beb28d

Please sign in to comment.