Skip to content

Commit 8da6b64

Browse files
authored
Handle null values in EnumMapper (#122)
1 parent 2a9d037 commit 8da6b64

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

src/main/java/com/aerospike/mapper/tools/mappers/EnumMapper.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public class EnumMapper extends TypeMapper {
1414
public EnumMapper(Class<? extends Enum<?>> clazz, String enumField) {
1515
this.clazz = clazz;
1616
this.enumField = enumField;
17-
if (!enumField.equals("")) {
17+
if (!enumField.isEmpty()) {
1818
try {
1919
this.enumRequestedField = clazz.getDeclaredField(enumField);
2020
this.enumRequestedField.setAccessible(true);
2121
} catch (NoSuchFieldException e) {
22-
throw new AerospikeException("Cannot Map requested enum, issue with the requested enumField.");
22+
throw toAerospikeException(e);
2323
}
2424
} else {
2525
this.enumRequestedField = null;
@@ -28,11 +28,19 @@ public EnumMapper(Class<? extends Enum<?>> clazz, String enumField) {
2828

2929
@Override
3030
public Object toAerospikeFormat(Object value) {
31-
if (!enumField.equals("")) {
31+
if (value == null) {
32+
return null;
33+
}
34+
35+
if (!enumField.isEmpty()) {
36+
if (enumRequestedField == null) {
37+
return null;
38+
}
3239
try {
33-
return enumRequestedField.get(value).toString();
40+
Object enumValue = enumRequestedField.get(value);
41+
return enumValue == null ? null : enumValue.toString();
3442
} catch (IllegalAccessException e) {
35-
throw new AerospikeException("Cannot Map requested enum, issue with the requested enumField.");
43+
throw toAerospikeException(e);
3644
}
3745
}
3846
return value.toString();
@@ -47,15 +55,15 @@ public Object fromAerospikeFormat(Object value) {
4755
String stringValue = (String) value;
4856
Enum<?>[] constants = clazz.getEnumConstants();
4957

50-
if (!enumField.equals("")) {
58+
if (!enumField.isEmpty()) {
5159
try {
5260
for (Enum<?> thisEnum : constants) {
5361
if (enumRequestedField.get(thisEnum).equals(stringValue)) {
5462
return thisEnum;
5563
}
5664
}
5765
} catch (IllegalAccessException e) {
58-
throw new AerospikeException("Cannot Map requested enum, issue with the requested enumField.");
66+
throw toAerospikeException(e);
5967
}
6068
} else {
6169
for (Enum<?> thisEnum : constants) {
@@ -64,6 +72,10 @@ public Object fromAerospikeFormat(Object value) {
6472
}
6573
}
6674
}
67-
throw new AerospikeException(String.format("Enum value of \"%s\" not found in type %s", stringValue, clazz.toString()));
75+
throw new AerospikeException(String.format("Enum value of \"%s\" not found in type %s", stringValue, clazz));
76+
}
77+
78+
private AerospikeException toAerospikeException(Exception e) {
79+
return new AerospikeException("Cannot Map requested enum, issue with the requested enumField.", e);
6880
}
6981
}
Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
11
package com.aerospike.mapper;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
5-
import org.junit.jupiter.api.Test;
6-
73
import com.aerospike.mapper.annotations.AerospikeEnum;
84
import com.aerospike.mapper.annotations.AerospikeKey;
95
import com.aerospike.mapper.annotations.AerospikeRecord;
106
import com.aerospike.mapper.tools.AeroMapper;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
1110

1211
public class AeroMapperEnumTest extends AeroMapperBaseTest {
1312

13+
@Test
14+
public void runTest() {
15+
A a1 = new A(1, "a", 10, Status.MARRIED, Country.ARGENTINA, Country.ARGENTINA);
16+
A a2 = new A(2, "b", 20, Status.SINGLE, Country.DENMARK, Country.DENMARK);
17+
A a3 = new A(3, "c", 30, Status.COMPLICATED, Country.UNITED_STATES, Country.UNITED_STATES);
18+
A a4 = new A(4, "d", 40, null, null, null);
19+
20+
AeroMapper mapper = new AeroMapper.Builder(client).build();
21+
mapper.save(a1);
22+
mapper.save(a2);
23+
mapper.save(a3);
24+
mapper.save(a4);
25+
26+
A a11 = mapper.read(A.class, 1);
27+
A a12 = mapper.read(A.class, 2);
28+
A a13 = mapper.read(A.class, 3);
29+
A a14 = mapper.read(A.class, 4);
30+
31+
assertEquals(a1.id, a11.id);
32+
assertEquals(a1.status, a11.status);
33+
assertEquals(a1.country, a11.country);
34+
35+
assertEquals(a2.name, a12.name);
36+
assertEquals(a2.country, a12.country);
37+
assertEquals(a2.countryAnno, a12.countryAnno);
38+
39+
assertEquals(a3.age, a13.age);
40+
assertEquals(a3.country, a13.country);
41+
assertEquals(a3.countryAnno, a13.countryAnno);
42+
43+
assertEquals(a4.age, a14.age);
44+
assertEquals(a4.country, a14.country);
45+
assertEquals(a4.countryAnno, a14.countryAnno);
46+
}
47+
1448
enum Status {
1549
MARRIED,
1650
SINGLE,
@@ -41,10 +75,9 @@ public static class A {
4175
public String name;
4276
public int age;
4377
public Status status;
44-
public Country country;
4578
@AerospikeEnum(enumField = "countryCode")
4679
public Country countryAnno;
47-
80+
private Country country;
4881

4982
public A() {
5083
}
@@ -59,29 +92,4 @@ public A(int id, String name, int age, Status status, Country country, Country c
5992
this.countryAnno = countryAnno;
6093
}
6194
}
62-
63-
@Test
64-
public void runTest() {
65-
A a1 = new A(1, "a", 10, Status.MARRIED, Country.ARGENTINA, Country.ARGENTINA);
66-
A a2 = new A(2, "b", 20, Status.SINGLE, Country.DENMARK, Country.DENMARK);
67-
A a3 = new A(3, "c", 30, Status.COMPLICATED, Country.UNITED_STATES, Country.UNITED_STATES);
68-
69-
AeroMapper mapper = new AeroMapper.Builder(client).build();
70-
mapper.save(a1);
71-
mapper.save(a2);
72-
mapper.save(a3);
73-
74-
A a11 = mapper.read(A.class, 1);
75-
A a12 = mapper.read(A.class, 2);
76-
A a13 = mapper.read(A.class, 3);
77-
78-
assertEquals(a1.id, a11.id);
79-
assertEquals(a1.status, a11.status);
80-
assertEquals(a1.country, a11.country);
81-
assertEquals(a2.name, a12.name);
82-
assertEquals(a2.country, a12.country);
83-
assertEquals(a2.countryAnno, a12.countryAnno);
84-
assertEquals(a3.age, a13.age);
85-
assertEquals(a3.countryAnno, a13.countryAnno);
86-
}
8795
}

0 commit comments

Comments
 (0)