Skip to content

Commit 60fad05

Browse files
committed
Throw on invalid values
Signed-off-by: Todd Baert <toddbaert@gmail.com>
1 parent 5d26247 commit 60fad05

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,27 @@ public class Value {
1919
private final Object innerObject;
2020

2121
public Value() {
22-
this.innerObject = null;
22+
this.innerObject = null;
2323
}
2424

25-
public Value(Object value) {
26-
this.innerObject = value;
25+
/**
26+
* Construct a new Value with an Object.
27+
* @param value to be wrapped.
28+
* @throws InstantiationException if value is not a valid type
29+
* (boolean, string, int, double, list, structure, instant)
30+
*/
31+
public Value(Object value) throws InstantiationException {
32+
// integer is a special case, convert those.
33+
this.innerObject = value instanceof Integer ? ((Integer)value).doubleValue() : value;
34+
if (!this.isNull()
35+
&& !this.isBoolean()
36+
&& !this.isString()
37+
&& !this.isNumber()
38+
&& !this.isStructure()
39+
&& !this.isList()
40+
&& !this.isInstant()) {
41+
throw new InstantiationException("Invalid value type: " + value.getClass());
42+
}
2743
}
2844

2945
public Value(Value value) {

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

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dev.openfeature.javasdk;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.fail;
57

68
import java.time.Instant;
79
import java.util.ArrayList;
@@ -16,9 +18,44 @@ public class ValueTest {
1618
}
1719

1820
@Test public void objectArgShouldContainObject() {
19-
Object innerValue = new Object();
20-
Value value = new Value(innerValue);
21-
assertEquals(innerValue, value.asObject());
21+
try {
22+
// int is a special case, see intObjectArgShouldConvertToInt()
23+
List<Object> list = new ArrayList<>();
24+
list.add(true);
25+
list.add("val");
26+
list.add(.5);
27+
list.add(new Structure());
28+
list.add(new ArrayList<Value>());
29+
list.add(Instant.now());
30+
31+
int i = 0;
32+
for (Object l: list) {
33+
Value value = new Value(l);
34+
assertEquals(list.get(i), value.asObject());
35+
i++;
36+
}
37+
} catch (Exception e) {
38+
fail("No exception expected.");
39+
}
40+
}
41+
42+
@Test public void intObjectArgShouldConvertToInt() {
43+
try {
44+
Object innerValue = 1;
45+
Value value = new Value(innerValue);
46+
assertEquals(innerValue, value.asInteger());
47+
} catch (Exception e) {
48+
fail("No exception expected.");
49+
}
50+
}
51+
52+
@Test public void invalidObjectArgShouldThrow() {
53+
54+
class Something {}
55+
56+
assertThrows(InstantiationException.class, () -> {
57+
new Value(new Something());
58+
});
2259
}
2360

2461
@Test public void boolArgShouldContainBool() {

0 commit comments

Comments
 (0)