Skip to content

Commit f36b641

Browse files
committed
Add proxy methods context->structure
Signed-off-by: Todd Baert <toddbaert@gmail.com>
1 parent 360d2b7 commit f36b641

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev.openfeature.javasdk;
22

3+
import java.time.ZonedDateTime;
4+
import java.util.List;
5+
36
import lombok.EqualsAndHashCode;
47
import lombok.Getter;
58
import lombok.Setter;
@@ -49,4 +52,41 @@ public static EvaluationContext merge(EvaluationContext ctx1, EvaluationContext
4952

5053
return ec;
5154
}
55+
56+
// overrides so we can use "add" methods and still return EvaluationContext, not superclass
57+
@Override
58+
public EvaluationContext add(String key, Boolean value) {
59+
return (EvaluationContext)super.add(key, value);
60+
}
61+
62+
@Override
63+
public EvaluationContext add(String key, String value) {
64+
return (EvaluationContext)super.add(key, value);
65+
}
66+
67+
@Override
68+
public EvaluationContext add(String key, Integer value) {
69+
return (EvaluationContext)super.add(key, value);
70+
}
71+
72+
@Override
73+
public EvaluationContext add(String key, Double value) {
74+
return (EvaluationContext)super.add(key, value);
75+
}
76+
77+
@Override
78+
public EvaluationContext add(String key, ZonedDateTime value) {
79+
return (EvaluationContext)super.add(key, value);
80+
}
81+
82+
@Override
83+
public EvaluationContext add(String key, Structure value) {
84+
return (EvaluationContext)super.add(key, value);
85+
}
86+
87+
@Override
88+
public <T> EvaluationContext add(String key, List<T> value) {
89+
return (EvaluationContext)super.add(key, value);
90+
}
91+
5292
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ public Structure add(String key, Double value) {
8787
return this;
8888
}
8989

90+
/**
91+
* Add date-time relevant key.
92+
*
93+
* @param key feature key
94+
* @param value date-time value
95+
* @return Structure
96+
*/
9097
public Structure add(String key, ZonedDateTime value) {
91-
attributes.put(key, new Pair<>(FlagValueType.STRING, value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)));
98+
attributes.put(key, new Pair<>(FlagValueType.STRING, value != null
99+
? value.format(DateTimeFormatter.ISO_ZONED_DATE_TIME) : null));
92100
return this;
93101
}
94102

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,30 @@ public class EvalContextTest {
107107

108108
@Test void can_chain_attribute_addition() {
109109
EvaluationContext ec = new EvaluationContext();
110-
Structure out = ec.add("str", "test")
110+
EvaluationContext out = ec.add("str", "test")
111111
.add("int", 4)
112112
.add("bool", false)
113113
.add("str", new Structure());
114114
assertEquals(EvaluationContext.class, out.getClass());
115115
}
116116

117+
@Test void can_add_key_with_null() {
118+
EvaluationContext ec = new EvaluationContext()
119+
.add("Boolean", (Boolean)null)
120+
.add("String", (String)null)
121+
.add("Double", (Double)null)
122+
.add("Structure", (Structure)null)
123+
.add("List", (List<Object>)null)
124+
.add("ZonedDateTime", (ZonedDateTime)null);
125+
assertEquals(6, ec.getAllAttributes().size());
126+
assertEquals(null, ec.getBooleanAttribute("Boolean"));
127+
assertEquals(null, ec.getStringAttribute("String"));
128+
assertEquals(null, ec.getDoubleAttribute("Double"));
129+
assertEquals(null, ec.getStructureAttribute("Structure"));
130+
assertEquals(null, ec.getDoubleAttribute("List"));
131+
assertEquals(null, ec.getStructureAttribute("ZonedDateTime"));
132+
}
133+
117134
@Test void merge_targeting_key() {
118135
String key1 = "key1";
119136
EvaluationContext ctx1 = new EvaluationContext(key1);

0 commit comments

Comments
 (0)