Skip to content

Commit aa14f99

Browse files
authored
support boolean bin value (#108)
1 parent e172387 commit aa14f99

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
import com.aerospike.mapper.tools.TypeMapper;
44

5+
import static com.aerospike.client.Value.UseBoolBin;
6+
57
public class BooleanMapper extends TypeMapper {
68

79
@Override
810
public Object toAerospikeFormat(Object value) {
911
if (value == null) {
1012
return null;
1113
}
14+
if (UseBoolBin) {
15+
return value;
16+
}
1217
return ((Boolean) value) ? 1 : 0;
1318
}
1419

@@ -17,6 +22,9 @@ public Object fromAerospikeFormat(Object value) {
1722
if (value == null) {
1823
return null;
1924
}
25+
if (UseBoolBin) {
26+
return value;
27+
}
2028
return !Long.valueOf(0).equals(value);
2129
}
2230
}

src/test/java/com/aerospike/mapper/DefaultFieldValuesTest.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.aerospike.mapper;
22

3+
import com.aerospike.client.Key;
4+
import com.aerospike.client.Record;
35
import com.aerospike.client.policy.WritePolicy;
46
import com.aerospike.mapper.annotations.AerospikeKey;
57
import com.aerospike.mapper.annotations.AerospikeRecord;
68
import com.aerospike.mapper.tools.AeroMapper;
79
import org.junit.jupiter.api.Test;
810

11+
import static com.aerospike.client.Value.UseBoolBin;
912
import static org.junit.jupiter.api.Assertions.*;
1013

1114
public class DefaultFieldValuesTest extends AeroMapperBaseTest {
@@ -38,7 +41,7 @@ public void testDefaultValues() {
3841
writePolicy.totalTimeout = 2000;
3942
writePolicy.socketTimeout = 100;
4043
AeroMapper mapper = new AeroMapper.Builder(client)
41-
.withWritePolicy(writePolicy).forClasses(PartialRecordsTest.DataClass.class)
44+
.withWritePolicy(writePolicy).forClasses(DefaultFieldsClass.class)
4245
.build();
4346

4447
DefaultFieldsClass obj = new DefaultFieldsClass();
@@ -63,4 +66,30 @@ public void testDefaultValues() {
6366
assertNull(dfc.bool);
6467
assertFalse(dfc.bool2);
6568
}
69+
70+
@Test
71+
public void testBooleanValue() {
72+
AeroMapper mapper = new AeroMapper.Builder(client).build();
73+
74+
DefaultFieldsClass obj = new DefaultFieldsClass();
75+
obj.key = "dfc";
76+
mapper.save(obj);
77+
78+
Key key = new Key("test", "testSet", "dfc");
79+
80+
Record record = client.get(null, key);
81+
assertTrue(record.bins.get("bool2") instanceof Long);
82+
assertEquals(0, record.getLong("bool2"));
83+
DefaultFieldsClass dfc = mapper.read(DefaultFieldsClass.class, "dfc");
84+
assertFalse(dfc.bool2);
85+
86+
UseBoolBin = true;
87+
mapper.save(obj);
88+
record = client.get(null, key);
89+
assertTrue(record.bins.get("bool2") instanceof Boolean);
90+
assertFalse(record.getBoolean("bool2"));
91+
dfc = mapper.read(DefaultFieldsClass.class, "dfc");
92+
assertFalse(dfc.bool2);
93+
UseBoolBin = false;
94+
}
6695
}

0 commit comments

Comments
 (0)