-
Notifications
You must be signed in to change notification settings - Fork 43
fix: null handling with Structure, Value #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
419b6f7
e3da4c1
df8498a
6376415
5f8528e
a283d7e
4fcdc38
2091fb4
42e5913
0bdfd30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@SuppressWarnings({ "PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType" }) | ||
abstract class AbstractStructure implements Structure { | ||
|
||
protected final Map<String, Value> attributes; | ||
|
||
AbstractStructure() { | ||
this.attributes = new HashMap<>(); | ||
} | ||
|
||
AbstractStructure(Map<String, Value> attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
/** | ||
* Get all values as their underlying primitives types. | ||
* | ||
* @return all attributes on the structure into a Map | ||
*/ | ||
@Override | ||
public Map<String, Object> asObjectMap() { | ||
return attributes | ||
.entrySet() | ||
.stream() | ||
// custom collector, workaround for Collectors.toMap in JDK8 | ||
// https://bugs.openjdk.org/browse/JDK-8148463 | ||
.collect(HashMap::new, | ||
(accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())), | ||
HashMap::putAll); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,12 +48,17 @@ public interface Structure { | |
Map<String, Object> asObjectMap(); | ||
|
||
/** | ||
* convertValue is converting the object type Value in a primitive type. | ||
* Converts the Value into its equivalent primitive type. | ||
* | ||
* @param value - Value object to convert | ||
* @return an Object containing the primitive type. | ||
* @return an Object containing the primitive type, or null. | ||
*/ | ||
default Object convertValue(Value value) { | ||
|
||
if (value == null || value.isNull()) { | ||
return null; | ||
} | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (value.isBoolean()) { | ||
return value.asBoolean(); | ||
} | ||
|
@@ -85,15 +90,14 @@ default Object convertValue(Value value) { | |
if (value.isStructure()) { | ||
Structure s = value.asStructure(); | ||
return s.asMap() | ||
.keySet() | ||
.entrySet() | ||
.stream() | ||
.collect( | ||
Collectors.toMap( | ||
key -> key, | ||
key -> convertValue(s.getValue(key)) | ||
) | ||
); | ||
.collect(HashMap::new, | ||
(accumulated, entry) -> accumulated.put(entry.getKey(), | ||
convertValue(entry.getValue())), | ||
HashMap::putAll); | ||
} | ||
|
||
throw new ValueNotConvertableError(); | ||
} | ||
|
||
|
@@ -134,7 +138,9 @@ default <T extends Structure> Map<String, Value> merge(Function<Map<String, Valu | |
*/ | ||
static Structure mapToStructure(Map<String, Object> map) { | ||
return new MutableStructure(map.entrySet().stream() | ||
.filter(e -> e.getValue() != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were FILTERING null values before. This resulted in maps with null entries not having converted (empty) |
||
.collect(Collectors.toMap(Map.Entry::getKey, e -> objectToValue(e.getValue())))); | ||
.collect(HashMap::new, | ||
(accumulated, entry) -> accumulated.put(entry.getKey(), | ||
objectToValue(entry.getValue())), | ||
HashMap::putAll)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,20 @@ void mapToStructureTest() { | |
assertEquals(new Value(Instant.ofEpochSecond(0)), res.getValue("Instant")); | ||
assertEquals(new HashMap<>(), res.getValue("Map").asStructure().asMap()); | ||
assertEquals(new Value(immutableContext), res.getValue("ImmutableContext")); | ||
assertNull(res.getValue("nullKey")); | ||
assertEquals(new Value(), res.getValue("nullKey")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified assertion associated with this change. |
||
} | ||
|
||
@Test | ||
void asObjectHandlesNullValue() { | ||
Map<String, Value> map = new HashMap<>(); | ||
map.put("null", new Value((String)null)); | ||
ImmutableStructure structure = new ImmutableStructure(map); | ||
assertNull(structure.asObjectMap().get("null")); | ||
} | ||
|
||
@Test | ||
void convertValueHandlesNullValue() { | ||
ImmutableStructure structure = new ImmutableStructure(); | ||
assertNull(structure.convertValue(new Value((String)null))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was common to both the
MutableContext
andImmutableContext
, so I've extracted it to an abstract.