Skip to content

Commit e7757ec

Browse files
Merge pull request open-feature#41 from rgrassian-split/evalContextIterator
iterators to get all integer, string, boolean, or structure map entries
2 parents 1911906 + a7c2ef6 commit e7757ec

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

src/main/java/dev/openfeature/javasdk/EvaluationContext.java

+16
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public <T> T getStructureAttribute(String key, Class<T> klass) {
3939
return objMapper.readValue(val, klass);
4040
}
4141

42+
public Map<String, String> getStructureAttributes() {
43+
return new HashMap<>(jsonAttributes);
44+
}
45+
4246
public void addStringAttribute(String key, String value) {
4347
stringAttributes.put(key, value);
4448
}
@@ -47,6 +51,10 @@ public String getStringAttribute(String key) {
4751
return stringAttributes.get(key);
4852
}
4953

54+
public Map<String, String> getStringAttributes() {
55+
return new HashMap<>(stringAttributes);
56+
}
57+
5058
public void addIntegerAttribute(String key, Integer value) {
5159
integerAttributes.put(key, value);
5260
}
@@ -55,6 +63,10 @@ public Integer getIntegerAttribute(String key) {
5563
return integerAttributes.get(key);
5664
}
5765

66+
public Map<String, Integer> getIntegerAttributes() {
67+
return new HashMap<>(integerAttributes);
68+
}
69+
5870
public Boolean getBooleanAttribute(String key) {
5971
return booleanAttributes.get(key);
6072
}
@@ -63,6 +75,10 @@ public void addBooleanAttribute(String key, Boolean b) {
6375
booleanAttributes.put(key, b);
6476
}
6577

78+
public Map<String, Boolean> getBooleanAttributes() {
79+
return new HashMap<>(booleanAttributes);
80+
}
81+
6682
public void addDatetimeAttribute(String key, ZonedDateTime value) {
6783
this.stringAttributes.put(key, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
6884
}

src/test/java/dev/openfeature/javasdk/EvalContextTest.java

+53-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package dev.openfeature.javasdk;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import org.junit.jupiter.api.Test;
46

57
import java.time.ZonedDateTime;
8+
import java.util.Map;
69

710
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertFalse;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
import static org.junit.jupiter.api.Assertions.fail;
814

915
public class EvalContextTest {
10-
@Specification(number="3.1",
16+
@Specification(number="3.1.1",
1117
text="The `evaluation context` structure **MUST** define an optional `targeting key` field of " +
1218
"type string, identifying the subject of the flag evaluation.")
1319
@Test void requires_targeting_key() {
@@ -16,7 +22,7 @@ public class EvalContextTest {
1622
assertEquals("targeting-key", ec.getTargetingKey());
1723
}
1824

19-
@Specification(number="3.2", text="The evaluation context MUST support the inclusion of " +
25+
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
2026
"custom fields, having keys of type `string`, and " +
2127
"values of type `boolean | string | number | datetime | structure`.")
2228
@Test void eval_context() {
@@ -36,7 +42,7 @@ public class EvalContextTest {
3642
assertEquals(dt, ec.getDatetimeAttribute("dt"));
3743
}
3844

39-
@Specification(number="3.2", text="The evaluation context MUST support the inclusion of " +
45+
@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
4046
"custom fields, having keys of type `string`, and " +
4147
"values of type `boolean | string | number | datetime | structure`.")
4248
@Test void eval_context__structure() {
@@ -59,4 +65,48 @@ public class EvalContextTest {
5965
assertEquals(2, nodeFromString.value);
6066
assertEquals(4, nodeFromString.left.value);
6167
}
68+
69+
@Specification(number="3.1.3", text="The evaluation context MUST support fetching the custom fields by key and " +
70+
"also fetching all of the keys and values.")
71+
@Test void fetch_all() {
72+
EvaluationContext ec = new EvaluationContext();
73+
74+
ec.addStringAttribute("str", "test");
75+
ec.addStringAttribute("str2", "test2");
76+
77+
ec.addBooleanAttribute("bool", true);
78+
ec.addBooleanAttribute("bool2", false);
79+
80+
ec.addIntegerAttribute("int", 4);
81+
ec.addIntegerAttribute("int2", 2);
82+
83+
ZonedDateTime dt = ZonedDateTime.now();
84+
ec.addDatetimeAttribute("dt", dt);
85+
86+
Node<Integer> n1 = new Node<>();
87+
n1.value = 4;
88+
Node<Integer> n2 = new Node<>();
89+
n2.value = 2;
90+
n2.left = n1;
91+
ec.addStructureAttribute("obj", n2);
92+
93+
Map<String, String> foundStr = ec.getStringAttributes();
94+
assertEquals(ec.getStringAttribute("str"), foundStr.get("str"));
95+
assertEquals(ec.getStringAttribute("str2"), foundStr.get("str2"));
96+
97+
Map<String, Boolean> foundBool = ec.getBooleanAttributes();
98+
assertEquals(ec.getBooleanAttribute("bool"), foundBool.get("bool"));
99+
assertEquals(ec.getBooleanAttribute("bool2"), foundBool.get("bool2"));
100+
101+
Map<String, Integer> foundInt = ec.getIntegerAttributes();
102+
assertEquals(ec.getIntegerAttribute("int"), foundInt.get("int"));
103+
assertEquals(ec.getIntegerAttribute("int2"), foundInt.get("int2"));
104+
105+
Map<String, String> foundObj = ec.getStructureAttributes();
106+
try {
107+
assertEquals(ec.getStructureAttribute("obj", Node.class), new ObjectMapper().readValue(foundObj.get("obj"), Node.class));
108+
} catch (JsonProcessingException e) {
109+
fail("Unexpected exception occurred: ", e);
110+
}
111+
}
62112
}

src/test/java/dev/openfeature/javasdk/HookSpecTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public void finallyAfter(HookContext<Boolean> ctx, Map<String, Object> hints) {
411411

412412

413413
Client client = getClient(null);
414-
client.getBooleanValue("key", false, new EvaluationContext(),
414+
client.getBooleanValue("key", false, ctx,
415415
FlagEvaluationOptions.builder()
416416
.hook(hook2)
417417
.hook(hook)

0 commit comments

Comments
 (0)