Skip to content

Commit f5a49ea

Browse files
committed
Drop multi-map implementation of context for a single type-annotated map
1 parent d71bf08 commit f5a49ea

File tree

4 files changed

+61
-58
lines changed

4 files changed

+61
-58
lines changed

pom.xml

-7
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@
5454
<scope>provided</scope>
5555
</dependency>
5656

57-
58-
<dependency>
59-
<groupId>com.fasterxml.jackson.core</groupId>
60-
<artifactId>jackson-databind</artifactId>
61-
<version>2.13.2.1</version>
62-
</dependency>
63-
6457
<dependency>
6558
<groupId>org.slf4j</groupId>
6659
<artifactId>slf4j-api</artifactId>

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

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.openfeature.javasdk;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import dev.openfeature.javasdk.internal.Pair;
44
import lombok.*;
55

66
import java.time.ZonedDateTime;
@@ -11,80 +11,90 @@
1111
@ToString @EqualsAndHashCode
1212
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
1313
public class EvaluationContext {
14-
@EqualsAndHashCode.Exclude private final ObjectMapper objMapper;
1514
@Setter @Getter private String targetingKey;
16-
private final Map<String, Integer> integerAttributes;
17-
private final Map<String, String> stringAttributes;
18-
private final Map<String, Boolean> booleanAttributes;
19-
final Map<String, String> jsonAttributes;
15+
private final Map<String, dev.openfeature.javasdk.internal.Pair<FlagValueType, Object>> attributes;
2016

2117
public EvaluationContext() {
22-
objMapper = new ObjectMapper();
2318
this.targetingKey = "";
24-
this.integerAttributes = new HashMap<>();
25-
this.stringAttributes = new HashMap<>();
26-
booleanAttributes = new HashMap<>();
27-
jsonAttributes = new HashMap<>();
19+
this.attributes = new HashMap<>();
2820
}
2921

3022
// TODO Not sure if I should have sneakythrows or checked exceptions here..
3123
@SneakyThrows
3224
public <T> void addStructureAttribute(String key, T value) {
33-
jsonAttributes.put(key, objMapper.writeValueAsString(value));
25+
attributes.put(key, new Pair<>(FlagValueType.OBJECT, value));
3426
}
3527

3628
@SneakyThrows
37-
public <T> T getStructureAttribute(String key, Class<T> klass) {
38-
String val = jsonAttributes.get(key);
39-
return objMapper.readValue(val, klass);
29+
public <T> T getStructureAttribute(String key) {
30+
return getAttributeByType(key, FlagValueType.OBJECT);
4031
}
4132

4233
public Map<String, String> getStructureAttributes() {
43-
return new HashMap<>(jsonAttributes);
34+
return getAttributesByType(FlagValueType.OBJECT);
4435
}
4536

4637
public void addStringAttribute(String key, String value) {
47-
stringAttributes.put(key, value);
38+
attributes.put(key, new Pair<>(FlagValueType.STRING, value));
4839
}
4940

5041
public String getStringAttribute(String key) {
51-
return stringAttributes.get(key);
42+
return getAttributeByType(key, FlagValueType.STRING);
43+
}
44+
45+
private <T> Map<String, T> getAttributesByType(FlagValueType type) {
46+
HashMap<String, T> hm = new HashMap<>();
47+
for (Map.Entry<String, Pair<FlagValueType, Object>> entry : attributes.entrySet()) {
48+
String key = entry.getKey();
49+
if (entry.getValue().getFirst() == type) {
50+
hm.put(key, (T) entry.getValue().getSecond());
51+
}
52+
}
53+
return hm;
54+
}
55+
56+
private <T> T getAttributeByType(String key, FlagValueType type) {
57+
Pair<FlagValueType, Object> val = attributes.get(key);
58+
if (val.getFirst() == type) {
59+
return (T) val.getSecond();
60+
}
61+
return null;
5262
}
5363

5464
public Map<String, String> getStringAttributes() {
55-
return new HashMap<>(stringAttributes);
65+
return getAttributesByType(FlagValueType.STRING);
5666
}
5767

5868
public void addIntegerAttribute(String key, Integer value) {
59-
integerAttributes.put(key, value);
69+
attributes.put(key, new Pair<>(FlagValueType.INTEGER, value));
6070
}
6171

6272
public Integer getIntegerAttribute(String key) {
63-
return integerAttributes.get(key);
73+
return getAttributeByType(key, FlagValueType.INTEGER);
6474
}
6575

6676
public Map<String, Integer> getIntegerAttributes() {
67-
return new HashMap<>(integerAttributes);
77+
return getAttributesByType(FlagValueType.INTEGER);
6878
}
6979

7080
public Boolean getBooleanAttribute(String key) {
71-
return booleanAttributes.get(key);
81+
return getAttributeByType(key, FlagValueType.BOOLEAN);
7282
}
7383

7484
public void addBooleanAttribute(String key, Boolean b) {
75-
booleanAttributes.put(key, b);
85+
attributes.put(key, new Pair<>(FlagValueType.BOOLEAN, b));
7686
}
7787

7888
public Map<String, Boolean> getBooleanAttributes() {
79-
return new HashMap<>(booleanAttributes);
89+
return getAttributesByType(FlagValueType.BOOLEAN);
8090
}
8191

8292
public void addDatetimeAttribute(String key, ZonedDateTime value) {
83-
this.stringAttributes.put(key, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
93+
attributes.put(key, new Pair<>(FlagValueType.STRING, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)));
8494
}
8595

8696
public ZonedDateTime getDatetimeAttribute(String key) {
87-
String attr = this.stringAttributes.get(key);
97+
String attr = getAttributeByType(key, FlagValueType.STRING);
8898
if (attr == null) {
8999
return null;
90100
}
@@ -96,18 +106,8 @@ public ZonedDateTime getDatetimeAttribute(String key) {
96106
*/
97107
public static EvaluationContext merge(EvaluationContext ctx1, EvaluationContext ctx2) {
98108
EvaluationContext ec = new EvaluationContext();
99-
100-
ec.stringAttributes.putAll(ctx1.stringAttributes);
101-
ec.stringAttributes.putAll(ctx2.stringAttributes);
102-
103-
ec.integerAttributes.putAll(ctx1.integerAttributes);
104-
ec.integerAttributes.putAll(ctx2.integerAttributes);
105-
106-
ec.booleanAttributes.putAll(ctx1.booleanAttributes);
107-
ec.booleanAttributes.putAll(ctx2.booleanAttributes);
108-
109-
ec.jsonAttributes.putAll(ctx1.jsonAttributes);
110-
ec.jsonAttributes.putAll(ctx2.jsonAttributes);
109+
ec.attributes.putAll(ctx1.attributes);
110+
ec.attributes.putAll(ctx2.attributes);
111111

112112
if (ctx1.getTargetingKey() != null) {
113113
ec.setTargetingKey(ctx1.getTargetingKey());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.openfeature.javasdk.internal;
2+
3+
import lombok.Value;
4+
5+
@Value
6+
public class Pair<K,V> {
7+
private K first;
8+
private V second;
9+
}

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

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dev.openfeature.javasdk;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
53
import org.junit.jupiter.api.Test;
64

75
import java.time.ZonedDateTime;
@@ -55,11 +53,7 @@ public class EvalContextTest {
5553
EvaluationContext ec = new EvaluationContext();
5654
ec.addStructureAttribute("obj", n2);
5755

58-
59-
String stringyObject = ec.jsonAttributes.get("obj");
60-
assertEquals("{\"left\":{\"left\":null,\"right\":null,\"value\":4},\"right\":null,\"value\":2}", stringyObject);
61-
62-
Node nodeFromString = ec.getStructureAttribute("obj", Node.class);
56+
Node nodeFromString = ec.getStructureAttribute("obj");
6357
assertEquals(n2, nodeFromString);
6458
assertEquals(n1, nodeFromString.left);
6559
assertEquals(2, nodeFromString.value);
@@ -102,10 +96,17 @@ public class EvalContextTest {
10296
assertEquals(ec.getIntegerAttribute("int2"), foundInt.get("int2"));
10397

10498
Map<String, String> foundObj = ec.getStructureAttributes();
105-
try {
106-
assertEquals(ec.getStructureAttribute("obj", Node.class), new ObjectMapper().readValue(foundObj.get("obj"), Node.class));
107-
} catch (JsonProcessingException e) {
108-
fail("Unexpected exception occurred: ", e);
109-
}
99+
assertEquals(ec.<Node>getStructureAttribute("obj"), n2);
100+
}
101+
102+
@Specification(number="3.1.4", text="The evaluation context fields MUST have an unique key.")
103+
@Test void unique_key_across_types() {
104+
EvaluationContext ec = new EvaluationContext();
105+
ec.addStringAttribute("key", "val");
106+
ec.addStringAttribute("key", "val2");
107+
assertEquals("val2", ec.getStringAttribute("key"));
108+
ec.addIntegerAttribute("key", 3);
109+
assertEquals(null, ec.getStringAttribute("key"));
110+
assertEquals(3, ec.getIntegerAttribute("key"));
110111
}
111112
}

0 commit comments

Comments
 (0)