@@ -22,6 +22,7 @@ import java.io.Writer;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.YearMonth;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
@@ -83,6 +84,8 @@ public final class OtherConverters {
|
||||
converters.add(MapToJsonObject.INSTANCE);
|
||||
converters.add(JsonArrayToCouchbaseList.INSTANCE);
|
||||
converters.add(CouchbaseListToJsonArray.INSTANCE);
|
||||
converters.add(YearMonthToStringConverter.INSTANCE);
|
||||
converters.add(StringToYearMonthConverter.INSTANCE);
|
||||
// EnumToObject, IntegerToEnumConverterFactory and StringToEnumConverterFactory are
|
||||
// registered in
|
||||
// {@link org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration#customConversions(
|
||||
@@ -329,4 +332,26 @@ public final class OtherConverters {
|
||||
}
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public enum YearMonthToStringConverter implements Converter<YearMonth, String> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public String convert(YearMonth source) {
|
||||
return source.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public enum StringToYearMonthConverter implements Converter<String, YearMonth> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public YearMonth convert(String source) {
|
||||
return YearMonth.parse(source);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.text.ChoiceFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.YearMonth;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -288,6 +289,28 @@ public class MappingCouchbaseConverterTests {
|
||||
assertThat(user.getId()).isEqualTo("001");
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesYearMonth() {
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
YearMonthEntity entity = new YearMonthEntity(YearMonth.parse("2023-04"));
|
||||
|
||||
converter.write(entity, converted);
|
||||
Map<String, Object> result = converted.export();
|
||||
assertThat(result.get("_class")).isEqualTo(entity.getClass().getName());
|
||||
assertThat(result.get("attr0")).isEqualTo(entity.attr0.toString());
|
||||
assertThat(converted.getId()).isEqualTo(BaseEntity.ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsYearMonth() {
|
||||
CouchbaseDocument source = new CouchbaseDocument();
|
||||
source.put("_class", YearMonthEntity.class.getName());
|
||||
source.put("attr0", "2023-04");
|
||||
|
||||
YearMonthEntity converted = converter.read(YearMonthEntity.class, source);
|
||||
assertThat(converted.attr0).isEqualTo(YearMonth.parse((String) source.get("attr0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesUninitializedValues() {
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
@@ -786,6 +809,15 @@ public class MappingCouchbaseConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class YearMonthEntity extends BaseEntity {
|
||||
private YearMonth attr0;
|
||||
|
||||
public YearMonthEntity(YearMonth attr0) {
|
||||
this.attr0 = attr0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static class BigDecimalEntity extends BaseEntity {
|
||||
private BigDecimal attr0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user