Skip to content

Commit 9999557

Browse files
committed
Add YearMonth converter. (#1722)
Closes #1720.
1 parent ed7d6b2 commit 9999557

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/main/java/org/springframework/data/couchbase/core/convert/OtherConverters.java

+25
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.math.BigDecimal;
2323
import java.math.BigInteger;
2424
import java.nio.charset.StandardCharsets;
25+
import java.time.YearMonth;
2526
import java.util.ArrayList;
2627
import java.util.Collection;
2728
import java.util.HashMap;
@@ -83,6 +84,8 @@ private OtherConverters() {}
8384
converters.add(MapToJsonObject.INSTANCE);
8485
converters.add(JsonArrayToCouchbaseList.INSTANCE);
8586
converters.add(CouchbaseListToJsonArray.INSTANCE);
87+
converters.add(YearMonthToStringConverter.INSTANCE);
88+
converters.add(StringToYearMonthConverter.INSTANCE);
8689
// EnumToObject, IntegerToEnumConverterFactory and StringToEnumConverterFactory are
8790
// registered in
8891
// {@link org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration#customConversions(
@@ -329,4 +332,26 @@ public JsonArray convert(CouchbaseList source) {
329332
}
330333
}
331334

335+
@WritingConverter
336+
public enum YearMonthToStringConverter implements Converter<YearMonth, String> {
337+
338+
INSTANCE;
339+
340+
@Override
341+
public String convert(YearMonth source) {
342+
return source.toString();
343+
}
344+
}
345+
346+
@ReadingConverter
347+
public enum StringToYearMonthConverter implements Converter<String, YearMonth> {
348+
349+
INSTANCE;
350+
351+
@Override
352+
public YearMonth convert(String source) {
353+
return YearMonth.parse(source);
354+
}
355+
}
356+
332357
}

src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.math.BigInteger;
2626
import java.text.ChoiceFormat;
2727
import java.time.LocalDateTime;
28+
import java.time.YearMonth;
2829
import java.time.temporal.ChronoUnit;
2930
import java.util.ArrayList;
3031
import java.util.Arrays;
@@ -288,6 +289,28 @@ void readsID() {
288289
assertThat(user.getId()).isEqualTo("001");
289290
}
290291

292+
@Test
293+
void writesYearMonth() {
294+
CouchbaseDocument converted = new CouchbaseDocument();
295+
YearMonthEntity entity = new YearMonthEntity(YearMonth.parse("2023-04"));
296+
297+
converter.write(entity, converted);
298+
Map<String, Object> result = converted.export();
299+
assertThat(result.get("_class")).isEqualTo(entity.getClass().getName());
300+
assertThat(result.get("attr0")).isEqualTo(entity.attr0.toString());
301+
assertThat(converted.getId()).isEqualTo(BaseEntity.ID);
302+
}
303+
304+
@Test
305+
void readsYearMonth() {
306+
CouchbaseDocument source = new CouchbaseDocument();
307+
source.put("_class", YearMonthEntity.class.getName());
308+
source.put("attr0", "2023-04");
309+
310+
YearMonthEntity converted = converter.read(YearMonthEntity.class, source);
311+
assertThat(converted.attr0).isEqualTo(YearMonth.parse((String) source.get("attr0")));
312+
}
313+
291314
@Test
292315
void writesUninitializedValues() {
293316
CouchbaseDocument converted = new CouchbaseDocument();
@@ -786,6 +809,15 @@ public BigIntegerEntity(BigInteger attr0) {
786809
}
787810
}
788811

812+
static class YearMonthEntity extends BaseEntity {
813+
private YearMonth attr0;
814+
815+
public YearMonthEntity(YearMonth attr0) {
816+
this.attr0 = attr0;
817+
}
818+
}
819+
820+
789821
static class BigDecimalEntity extends BaseEntity {
790822
private BigDecimal attr0;
791823

0 commit comments

Comments
 (0)