Skip to content

Commit 4cbb87f

Browse files
committed
Support chaining evaluation context additions.
Refs #29
1 parent efc1c04 commit 4cbb87f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public EvaluationContext() {
2121

2222
// TODO Not sure if I should have sneakythrows or checked exceptions here..
2323
@SneakyThrows
24-
public <T> void addStructureAttribute(String key, T value) {
24+
public <T> EvaluationContext addStructureAttribute(String key, T value) {
2525
attributes.put(key, new Pair<>(FlagValueType.OBJECT, value));
26+
return this;
2627
}
2728

2829
@SneakyThrows
@@ -34,8 +35,9 @@ public Map<String, String> getStructureAttributes() {
3435
return getAttributesByType(FlagValueType.OBJECT);
3536
}
3637

37-
public void addStringAttribute(String key, String value) {
38+
public EvaluationContext addStringAttribute(String key, String value) {
3839
attributes.put(key, new Pair<>(FlagValueType.STRING, value));
40+
return this;
3941
}
4042

4143
public String getStringAttribute(String key) {
@@ -68,8 +70,9 @@ public Map<String, String> getStringAttributes() {
6870
return getAttributesByType(FlagValueType.STRING);
6971
}
7072

71-
public void addIntegerAttribute(String key, Integer value) {
73+
public EvaluationContext addIntegerAttribute(String key, Integer value) {
7274
attributes.put(key, new Pair<>(FlagValueType.INTEGER, value));
75+
return this;
7376
}
7477

7578
public Integer getIntegerAttribute(String key) {
@@ -84,16 +87,18 @@ public Boolean getBooleanAttribute(String key) {
8487
return getAttributeByType(key, FlagValueType.BOOLEAN);
8588
}
8689

87-
public void addBooleanAttribute(String key, Boolean b) {
90+
public EvaluationContext addBooleanAttribute(String key, Boolean b) {
8891
attributes.put(key, new Pair<>(FlagValueType.BOOLEAN, b));
92+
return this;
8993
}
9094

9195
public Map<String, Boolean> getBooleanAttributes() {
9296
return getAttributesByType(FlagValueType.BOOLEAN);
9397
}
9498

95-
public void addDatetimeAttribute(String key, ZonedDateTime value) {
99+
public EvaluationContext addDatetimeAttribute(String key, ZonedDateTime value) {
96100
attributes.put(key, new Pair<>(FlagValueType.STRING, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)));
101+
return this;
97102
}
98103

99104
public ZonedDateTime getDatetimeAttribute(String key) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,13 @@ public class EvalContextTest {
109109
assertEquals(null, ec.getStringAttribute("key"));
110110
assertEquals(3, ec.getIntegerAttribute("key"));
111111
}
112+
113+
@Test void can_chain_attribute_addition() {
114+
EvaluationContext ec = new EvaluationContext();
115+
EvaluationContext out = ec.addStructureAttribute("str", "test")
116+
.addIntegerAttribute("int", 4)
117+
.addBooleanAttribute("bool", false)
118+
.addStructureAttribute("str", new Node<Integer>());
119+
assertEquals(EvaluationContext.class, out.getClass());
120+
}
112121
}

0 commit comments

Comments
 (0)