Skip to content

Commit 455e9a5

Browse files
uaihebertmp911de
authored andcommitted
#41 - Add R2DBC converters.
Original pull request: #65.
1 parent feabe47 commit 455e9a5

File tree

2 files changed

+345
-0
lines changed

2 files changed

+345
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.r2dbc.repository.query;
18+
19+
import io.r2dbc.spi.Row;
20+
import org.springframework.core.convert.converter.Converter;
21+
import org.springframework.core.convert.converter.ConverterFactory;
22+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToOffsetDateTimeConverter;
23+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToStringConverter;
24+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToUuidConverter;
25+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToZonedDateTimeConverter;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.NumberUtils;
28+
29+
import java.time.LocalDate;
30+
import java.time.LocalDateTime;
31+
import java.time.LocalTime;
32+
import java.time.OffsetDateTime;
33+
import java.time.ZonedDateTime;
34+
import java.util.ArrayList;
35+
import java.util.Collection;
36+
import java.util.List;
37+
import java.util.UUID;
38+
39+
/**
40+
* Base class for row data converter.
41+
*
42+
* @author Hebert Coelho
43+
*/
44+
abstract class RowDataConverter {
45+
private RowDataConverter() {
46+
}
47+
48+
/**
49+
* @return A list of the registered converters
50+
*/
51+
static Collection<Object> getConvertersToRegister() {
52+
List<Object> converters = new ArrayList<>();
53+
54+
converters.add(RowToBooleanConverter.INSTANCE);
55+
converters.add(RowToLocalDateConverter.INSTANCE);
56+
converters.add(RowToLocalDateTimeConverter.INSTANCE);
57+
converters.add(RowToLocalTimeConverter.INSTANCE);
58+
converters.add(RowToOffsetDateTimeConverter.INSTANCE);
59+
converters.add(RowToStringConverter.INSTANCE);
60+
converters.add(RowToUuidConverter.INSTANCE);
61+
converters.add(RowToZonedDateTimeConverter.INSTANCE);
62+
63+
return converters;
64+
}
65+
66+
/**
67+
* Simple singleton to convert {@link Row}s to their {@link Boolean} representation.
68+
*
69+
* @author Hebert Coelho
70+
*/
71+
public enum RowToBooleanConverter implements Converter<Row, Boolean> {
72+
INSTANCE;
73+
74+
@Override
75+
public Boolean convert(Row row) {
76+
return row.get(0, Boolean.class);
77+
}
78+
}
79+
80+
/**
81+
* Simple singleton to convert {@link Row}s to their {@link LocalDate} representation.
82+
*
83+
* @author Hebert Coelho
84+
*/
85+
public enum RowToLocalDateConverter implements Converter<Row, LocalDate> {
86+
INSTANCE;
87+
88+
@Override
89+
public LocalDate convert(Row row) {
90+
return row.get(0, LocalDate.class);
91+
}
92+
}
93+
94+
/**
95+
* Simple singleton to convert {@link Row}s to their {@link LocalDateTime} representation.
96+
*
97+
* @author Hebert Coelho
98+
*/
99+
public enum RowToLocalDateTimeConverter implements Converter<Row, LocalDateTime> {
100+
INSTANCE;
101+
102+
@Override
103+
public LocalDateTime convert(Row row) {
104+
return row.get(0, LocalDateTime.class);
105+
}
106+
}
107+
108+
/**
109+
* Simple singleton to convert {@link Row}s to their {@link LocalTime} representation.
110+
*
111+
* @author Hebert Coelho
112+
*/
113+
public enum RowToLocalTimeConverter implements Converter<Row, LocalTime> {
114+
INSTANCE;
115+
116+
@Override
117+
public LocalTime convert(Row row) {
118+
return row.get(0, LocalTime.class);
119+
}
120+
}
121+
122+
/**
123+
* Singleton converter factory to convert the first column of a {@link Row} to a {@link Number}.
124+
* <p>
125+
* Support Number classes including Byte, Short, Integer, Float, Double, Long, BigInteger, BigDecimal. This class
126+
* delegates to {@link NumberUtils#convertNumberToTargetClass(Number, Class)} to perform the conversion.
127+
*
128+
* @see Byte
129+
* @see Short
130+
* @see Integer
131+
* @see Long
132+
* @see java.math.BigInteger
133+
* @see Float
134+
* @see Double
135+
* @see java.math.BigDecimal
136+
*
137+
* @author Hebert Coelho
138+
*/
139+
public enum RowToNumberConverterFactory implements ConverterFactory<Row, Number> {
140+
INSTANCE;
141+
142+
@Override
143+
public <T extends Number> Converter<Row, T> getConverter(Class<T> targetType) {
144+
Assert.notNull(targetType, "Target type must not be null");
145+
return new RowToNumber<>(targetType);
146+
}
147+
148+
private static final class RowToNumber<T extends Number> implements Converter<Row, T> {
149+
private final Class<T> targetType;
150+
151+
RowToNumber(Class<T> targetType) {
152+
this.targetType = targetType;
153+
}
154+
155+
@Override
156+
public T convert(Row source) {
157+
158+
Object object = source.get(0, targetType);
159+
160+
return (object != null ? NumberUtils.convertNumberToTargetClass((Number) object, this.targetType) : null);
161+
}
162+
}
163+
164+
/**
165+
* Simple singleton to convert {@link Row}s to their {@link OffsetDateTime} representation.
166+
*
167+
* @author Hebert Coelho
168+
*/
169+
public enum RowToOffsetDateTimeConverter implements Converter<Row, OffsetDateTime> {
170+
INSTANCE;
171+
172+
@Override
173+
public OffsetDateTime convert(Row row) {
174+
return row.get(0, OffsetDateTime.class);
175+
}
176+
}
177+
178+
/**
179+
* Simple singleton to convert {@link Row}s to their {@link String} representation.
180+
*
181+
* @author Hebert Coelho
182+
*/
183+
public enum RowToStringConverter implements Converter<Row, String> {
184+
INSTANCE;
185+
186+
@Override
187+
public String convert(Row row) {
188+
return row.get(0, String.class);
189+
}
190+
}
191+
192+
/**
193+
* Simple singleton to convert {@link Row}s to their {@link UUID} representation.
194+
*
195+
* @author Hebert Coelho
196+
*/
197+
public enum RowToUuidConverter implements Converter<Row, UUID> {
198+
INSTANCE;
199+
200+
@Override
201+
public UUID convert(Row row) {
202+
return row.get(0, UUID.class);
203+
}
204+
}
205+
206+
/**
207+
* Simple singleton to convert {@link Row}s to their {@link ZonedDateTime} representation.
208+
*
209+
* @author Hebert Coelho
210+
*/
211+
public enum RowToZonedDateTimeConverter implements Converter<Row, ZonedDateTime> {
212+
INSTANCE;
213+
214+
@Override
215+
public ZonedDateTime convert(Row row) {
216+
return row.get(0, ZonedDateTime.class);
217+
}
218+
}
219+
}
220+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.springframework.data.r2dbc.repository.query;
2+
3+
import io.r2dbc.spi.Row;
4+
import org.junit.Test;
5+
import org.springframework.core.convert.converter.Converter;
6+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToBooleanConverter;
7+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToLocalDateConverter;
8+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToLocalDateTimeConverter;
9+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToLocalTimeConverter;
10+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory;
11+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToOffsetDateTimeConverter;
12+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToStringConverter;
13+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToUuidConverter;
14+
import org.springframework.data.r2dbc.repository.query.RowDataConverter.RowToNumberConverterFactory.RowToZonedDateTimeConverter;
15+
16+
import java.time.LocalDate;
17+
import java.time.LocalDateTime;
18+
import java.time.LocalTime;
19+
import java.time.OffsetDateTime;
20+
import java.time.ZonedDateTime;
21+
import java.util.UUID;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25+
import static org.junit.Assert.assertTrue;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
28+
29+
public class RowDataConverterTests {
30+
private static final int TOTAL_REGISTERED_CONVERTERS = 8;
31+
32+
@Test
33+
public void isReturningAllCreatedConverts() {
34+
assertThat(RowDataConverter.getConvertersToRegister().size())
35+
.isEqualTo(TOTAL_REGISTERED_CONVERTERS);
36+
}
37+
38+
@Test
39+
public void isConvertingBoolean() {
40+
Row row = mock(Row.class);
41+
when(row.get(0, Boolean.class)).thenReturn(true);
42+
43+
assertTrue(RowToBooleanConverter.INSTANCE.convert(row));
44+
}
45+
46+
@Test
47+
public void isConvertingLocalDate() {
48+
LocalDate now = LocalDate.now();
49+
Row row = mock(Row.class);
50+
when(row.get(0, LocalDate.class)).thenReturn(now);
51+
52+
assertThat(RowToLocalDateConverter.INSTANCE.convert(row)).isEqualTo(now);
53+
}
54+
55+
@Test
56+
public void isConvertingLocalDateTime() {
57+
LocalDateTime now = LocalDateTime.now();
58+
Row row = mock(Row.class);
59+
when(row.get(0, LocalDateTime.class)).thenReturn(now);
60+
61+
assertThat(RowToLocalDateTimeConverter.INSTANCE.convert(row)).isEqualTo(now);
62+
}
63+
64+
@Test
65+
public void isConvertingLocalTime() {
66+
LocalTime now = LocalTime.now();
67+
Row row = mock(Row.class);
68+
when(row.get(0, LocalTime.class)).thenReturn(now);
69+
70+
assertThat(RowToLocalTimeConverter.INSTANCE.convert(row)).isEqualTo(now);
71+
}
72+
73+
@Test
74+
public void isConvertingOffsetDateTime() {
75+
OffsetDateTime now = OffsetDateTime.now();
76+
Row row = mock(Row.class);
77+
when(row.get(0, OffsetDateTime.class)).thenReturn(now);
78+
79+
assertThat(RowToOffsetDateTimeConverter.INSTANCE.convert(row)).isEqualTo(now);
80+
}
81+
82+
@Test
83+
public void isConvertingString() {
84+
String value = "aValue";
85+
Row row = mock(Row.class);
86+
when(row.get(0, String.class)).thenReturn(value);
87+
88+
assertThat(RowToStringConverter.INSTANCE.convert(row)).isEqualTo(value);
89+
}
90+
91+
@Test
92+
public void isConvertingUUID() {
93+
UUID value = UUID.randomUUID();
94+
Row row = mock(Row.class);
95+
when(row.get(0, UUID.class)).thenReturn(value);
96+
97+
assertThat(RowToUuidConverter.INSTANCE.convert(row)).isEqualTo(value);
98+
}
99+
100+
@Test
101+
public void isConvertingZonedDateTime() {
102+
ZonedDateTime now = ZonedDateTime.now();
103+
Row row = mock(Row.class);
104+
when(row.get(0, ZonedDateTime.class)).thenReturn(now);
105+
106+
assertThat(RowToZonedDateTimeConverter.INSTANCE.convert(row)).isEqualTo(now);
107+
}
108+
109+
@Test
110+
public void isConvertingNumber() {
111+
Row row = mock(Row.class);
112+
when(row.get(0, Integer.class)).thenReturn(33);
113+
114+
final Converter<Row, Integer> converter = RowToNumberConverterFactory.INSTANCE.getConverter(Integer.class);
115+
116+
assertThat(converter.convert(row)).isEqualTo(33);
117+
}
118+
119+
@Test
120+
public void isRaisingExceptionForInvalidNumber() {
121+
assertThatIllegalArgumentException().isThrownBy(
122+
() -> RowToNumberConverterFactory.INSTANCE.getConverter(null)
123+
);
124+
}
125+
}

0 commit comments

Comments
 (0)