Skip to content

Commit 619098f

Browse files
committed
Preserve null in ValueMapper except for primitive types
1 parent 9af0818 commit 619098f

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

src/main/java/org/seedstack/coffig/mapper/ValueMapper.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
*/
88
package org.seedstack.coffig.mapper;
99

10-
import java.lang.reflect.Type;
11-
import java.util.HashMap;
12-
import java.util.Map;
13-
import java.util.function.Function;
1410
import org.seedstack.coffig.TreeNode;
1511
import org.seedstack.coffig.internal.ConfigurationErrorCode;
1612
import org.seedstack.coffig.internal.ConfigurationException;
1713
import org.seedstack.coffig.node.ValueNode;
1814
import org.seedstack.coffig.spi.ConfigurationMapper;
1915

16+
import java.lang.reflect.Type;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
import java.util.function.Function;
20+
2021
public class ValueMapper implements ConfigurationMapper {
2122
private final Map<Type, Function<String, ?>> converters = new HashMap<>();
2223

@@ -47,7 +48,18 @@ public boolean canHandle(Type type) {
4748

4849
@Override
4950
public Object map(TreeNode value, Type type) {
50-
return converters.get(type).apply(value.value());
51+
String innerValue = value.value();
52+
if (type instanceof Class && ((Class<?>) type).isPrimitive()) {
53+
// Don't return a null value for primitive types (would cause NPE)
54+
return converters.get(type).apply(innerValue);
55+
} else {
56+
// For other types it's ok to return null
57+
if (innerValue == null) {
58+
return null;
59+
} else {
60+
return converters.get(type).apply(innerValue);
61+
}
62+
}
5163
}
5264

5365
@Override

src/main/java/org/seedstack/coffig/node/AbstractTreeNode.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
package org.seedstack.coffig.node;
99

10+
import org.seedstack.coffig.TreeNode;
11+
1012
import java.util.Arrays;
1113
import java.util.stream.Collectors;
12-
import org.seedstack.coffig.TreeNode;
1314

1415
abstract class AbstractTreeNode implements TreeNode {
1516
static String HIDDEN_PLACEHOLDER = "***";
@@ -28,8 +29,4 @@ public void hide() {
2829
String indent(String s) {
2930
return Arrays.stream(s.split("\n")).map(line -> " " + line).collect(Collectors.joining("\n"));
3031
}
31-
32-
String quote(String s) {
33-
return s == null ? null : s.replace("\\", "\\\\").replace("\"", "\\\"");
34-
}
3532
}

src/main/java/org/seedstack/coffig/node/ArrayNode.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,7 @@ public String toMappedString(ConfigurationMapper mapper) {
182182
if (isHidden()) {
183183
return "\"" + HIDDEN_PLACEHOLDER + "\"";
184184
} else if (children.size() > 0 && children.get(0).type() == Type.VALUE_NODE) {
185-
return children.stream().map(item -> {
186-
if (mapper != null) {
187-
return "- " + mapper.map(item, String.class);
188-
} else {
189-
return "- " + item.toString();
190-
}
191-
}).collect(joining("\n"));
185+
return children.stream().map(item -> "- " + item.toMappedString(mapper)).collect(joining("\n"));
192186
} else {
193187
return children.stream().map(item -> "-\n" + indent(item.toMappedString(mapper))).collect(joining("\n"));
194188
}

src/main/java/org/seedstack/coffig/node/MapNode.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
*/
88
package org.seedstack.coffig.node;
99

10+
import org.seedstack.coffig.TreeNode;
11+
import org.seedstack.coffig.internal.ConfigurationErrorCode;
12+
import org.seedstack.coffig.internal.ConfigurationException;
13+
import org.seedstack.coffig.internal.PropertyNotFoundException;
14+
import org.seedstack.coffig.spi.ConfigurationMapper;
15+
1016
import java.util.HashMap;
1117
import java.util.Map;
1218
import java.util.Objects;
1319
import java.util.Optional;
1420
import java.util.stream.Collectors;
1521
import java.util.stream.Stream;
1622

17-
import org.seedstack.coffig.TreeNode;
18-
import org.seedstack.coffig.internal.ConfigurationErrorCode;
19-
import org.seedstack.coffig.internal.ConfigurationException;
20-
import org.seedstack.coffig.internal.PropertyNotFoundException;
21-
import org.seedstack.coffig.spi.ConfigurationMapper;
22-
2323
public class MapNode extends AbstractTreeNode {
2424
private final Map<String, TreeNode> children;
2525

@@ -177,11 +177,7 @@ public String toMappedString(ConfigurationMapper mapper) {
177177
} else {
178178
return children.entrySet().stream().map(entry -> {
179179
if (entry.getValue().type() == Type.VALUE_NODE) {
180-
if (mapper != null) {
181-
return entry.getKey() + ": " + mapper.map(entry.getValue(), String.class);
182-
} else {
183-
return entry.getKey() + ": " + entry.getValue().toString();
184-
}
180+
return entry.getKey() + ": " + entry.getValue().toMappedString(mapper);
185181
} else {
186182
return entry.getKey() + ":\n" + indent(entry.getValue().toMappedString(mapper));
187183
}

src/main/java/org/seedstack/coffig/node/ValueNode.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,25 @@ public int hashCode() {
121121

122122
@Override
123123
public String toString() {
124-
return value == null ? "~" : "\"" + (isHidden() ? HIDDEN_PLACEHOLDER : quote(value)) + "\"";
124+
return toMappedString(null);
125125
}
126126

127127
@Override
128128
public String toMappedString(ConfigurationMapper mapper) {
129-
if (value == null) {
130-
return "~";
129+
if (isHidden()) {
130+
return "\"" + HIDDEN_PLACEHOLDER + "\"";
131131
} else {
132-
if (isHidden()) {
133-
return "\"" + HIDDEN_PLACEHOLDER + "\"";
132+
String s;
133+
if (mapper != null) {
134+
s = (String) mapper.map(this, String.class);
135+
} else {
136+
s = value;
137+
}
138+
139+
if (s == null) {
140+
return "~";
134141
} else {
135-
if (mapper != null) {
136-
return "\"" + quote(String.valueOf(mapper.map(this, String.class))) + "\"";
137-
} else {
138-
return "\"" + quote(value) + "\"";
139-
}
142+
return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
140143
}
141144
}
142145
}

0 commit comments

Comments
 (0)