Skip to content

Commit 5f90ac8

Browse files
committed
#479 Add method for unwrapping RETURN_NULL to LeafValueHandler
1 parent 194bbc4 commit 5f90ac8

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/main/java/ch/jalu/configme/beanmapper/MapperImpl.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ public MapperImpl(@NotNull BeanDefinitionService beanDefinitionService,
122122
// Step 1: attempt simple value transformation
123123
Object exportValue = leafValueHandler.toExportValue(value, exportContext);
124124
if (exportValue != null || value == null) {
125-
return unwrapReturnNull(exportValue);
125+
return LeafValueHandler.unwrapReturnNull(exportValue);
126126
}
127127

128128
// Step 2: handle special cases like Collection
129129
exportValue = createExportValueForSpecialTypes(value, exportContext);
130130
if (exportValue != null) {
131-
return unwrapReturnNull(exportValue);
131+
return LeafValueHandler.unwrapReturnNull(exportValue);
132132
}
133133

134134
// Step 3: treat as bean
@@ -195,10 +195,6 @@ public MapperImpl(@NotNull BeanDefinitionService beanDefinitionService,
195195
return null;
196196
}
197197

198-
protected static @Nullable Object unwrapReturnNull(@Nullable Object o) {
199-
return o == LeafValueHandler.RETURN_NULL ? null : o;
200-
}
201-
202198
// ---------
203199
// Bean mapping
204200
// ---------

src/main/java/ch/jalu/configme/beanmapper/leafvaluehandler/LeafValueHandler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ public interface LeafValueHandler {
4545
*/
4646
@Nullable Object toExportValue(@Nullable Object value, @NotNull ExportContext exportContext);
4747

48+
/**
49+
* Returns null if the object is {@link #RETURN_NULL}, otherwise the given object. Used to process return values
50+
* from methods like {@link #toExportValue}, where {@code null} means the instance doesn't support the value,
51+
* while {@link #RETURN_NULL} means null should be used as export value.
52+
*
53+
* @param object the object to potentially unwrap
54+
* @param <T> the object type
55+
* @return null, or the provided object
56+
*/
57+
static <T> @Nullable T unwrapReturnNull(@Nullable T object) {
58+
return object == RETURN_NULL ? null : object;
59+
}
4860
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ch.jalu.configme.beanmapper.leafvaluehandler;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.TimeUnit;
6+
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.hamcrest.Matchers.equalTo;
9+
import static org.hamcrest.Matchers.nullValue;
10+
11+
/**
12+
* Test for {@link LeafValueHandler}.
13+
*/
14+
class LeafValueHandlerTest {
15+
16+
@Test
17+
void shouldUnwrapReturnNull() {
18+
// given / when / then
19+
assertThat(LeafValueHandler.unwrapReturnNull(null), nullValue());
20+
assertThat(LeafValueHandler.unwrapReturnNull(LeafValueHandler.RETURN_NULL), nullValue());
21+
22+
assertThat(LeafValueHandler.unwrapReturnNull("null"), equalTo("null"));
23+
assertThat(LeafValueHandler.unwrapReturnNull(TimeUnit.SECONDS), equalTo(TimeUnit.SECONDS));
24+
assertThat(LeafValueHandler.unwrapReturnNull(12), equalTo(12));
25+
}
26+
}

0 commit comments

Comments
 (0)