Skip to content

Commit d581c33

Browse files
committed
Merge pull request #19 from jhorstmann/refactor-null-handling-get-rid-of-warning-in-log
Refactored NULL handling to get rid of logged warning message
2 parents 44b7b1c + 8481182 commit d581c33

File tree

7 files changed

+27
-52
lines changed

7 files changed

+27
-52
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>de.zalando</groupId>
66
<artifactId>zalando-sprocwrapper</artifactId>
7-
<version>1.3.0</version>
7+
<version>1.3.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<name>Stored Procedure Wrapper</name>
1010
<description>Library to make PostgreSQL stored procedures available through simple Java "*SProcService" interfaces including automatic object serialization and deserialization (using typemapper and convention-over-configuration). Supports sharding, advisory locking, statement timeouts and PostgreSQL types such as enums and hstore.</description>

src/main/java/de/zalando/typemapper/core/Mapping.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ public FieldMapper getFieldMapper() throws NotsupportedTypeException, Instantiat
194194
}
195195

196196
fieldMapper = FieldMapperRegister.getMapperForClass(getFieldClass());
197-
if (fieldMapper == null) {
198-
throw new NotsupportedTypeException("Could not find mapper for type " + getFieldClass());
199-
}
200197
}
201198

202199
return fieldMapper;

src/main/java/de/zalando/typemapper/core/TypeMapper.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,11 @@ private void fillObject(final Object result, final ResultTree tree) throws SQLEx
154154
// TODO pribeiro we need to distinguish between null value and a mapping not defined in the tree
155155
if (node == null) {
156156

157-
// this may be okay - if any return value is NULL, we will reach this path.
158-
// to classify and mark this as an error, we need more information.
159-
LOG.trace("Could not map property {} of class {}: field not in result tree, field may be nullable.",
160-
mapping.getName(), resultClass.getSimpleName());
161-
continue;
162-
}
163-
164-
if (DbResultNodeType.MAP != node.getNodeType() && node.getValue() == null) {
165157
if (mapping.isOptionalField()) {
166158
mapping.map(result, null);
167159
}
160+
161+
continue;
168162
}
169163

170164
// TODO pribeiro we should use polymorphism instead. Build like a chain.

src/main/java/de/zalando/typemapper/core/fieldMapper/FieldMapperRegister.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.math.BigDecimal;
44

55
import java.util.Date;
6-
import java.util.List;
76
import java.util.Map;
8-
import java.util.Set;
97
import java.util.concurrent.ConcurrentHashMap;
108

119
import de.zalando.typemapper.core.ValueTransformer;
@@ -15,6 +13,8 @@ public class FieldMapperRegister {
1513
@SuppressWarnings({ "rawtypes" })
1614
private static final Map<Class, FieldMapper> register = new ConcurrentHashMap<Class, FieldMapper>();
1715

16+
private static final NullFieldMapper NULL_FIELD_MAPPER = new NullFieldMapper();
17+
1818
static {
1919
final FieldMapper dateFieldMapper = new DateFieldMapper();
2020
FieldMapperRegister.register(Date.class, dateFieldMapper);
@@ -62,12 +62,6 @@ public class FieldMapperRegister {
6262

6363
final FieldMapper hstoreMapper = new HStoreFieldMapper();
6464
FieldMapperRegister.register(Map.class, hstoreMapper);
65-
66-
final FieldMapper nullListMapper = new NullListFieldMapper();
67-
FieldMapperRegister.register(List.class, nullListMapper);
68-
69-
final FieldMapper nullSetMapper = new NullSetFieldMapper();
70-
FieldMapperRegister.register(Set.class, nullSetMapper);
7165
}
7266

7367
@SuppressWarnings("rawtypes")
@@ -96,6 +90,10 @@ public static FieldMapper getMapperForClass(final Class clazz) {
9690
}
9791
}
9892

93+
if (fieldMapper == null) {
94+
fieldMapper = NULL_FIELD_MAPPER;
95+
}
96+
9997
return fieldMapper;
10098
}
10199

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.zalando.typemapper.core.fieldMapper;
2+
3+
/**
4+
* sentinel mapper for NULL fields returned from database. Has to be registered as last mapper in
5+
* {@link de.zalando.typemapper.core.fieldMapper.FieldMapperRegister}.
6+
*/
7+
public class NullFieldMapper implements FieldMapper {
8+
9+
@Override
10+
public Object mapField(final String string, final Class clazz) {
11+
if (string != null) {
12+
throw new IllegalStateException("Could not find mapper for type " + clazz);
13+
} else {
14+
return null;
15+
}
16+
}
17+
18+
}

src/main/java/de/zalando/typemapper/core/fieldMapper/NullListFieldMapper.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main/java/de/zalando/typemapper/core/fieldMapper/NullSetFieldMapper.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)