Skip to content

Commit 3948f06

Browse files
committed
Refactor StringCodec
Introduce constructor accepting the default and array type for character codecs. [#488] Signed-off-by: Mark Paluch <[email protected]>
1 parent 70df7c7 commit 3948f06

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/main/java/io/r2dbc/postgresql/codec/StringCodec.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.EnumSet;
2828
import java.util.Set;
2929
import java.util.stream.Collectors;
30+
import java.util.stream.Stream;
3031

3132
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.BPCHAR;
3233
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.CHAR;
@@ -48,22 +49,32 @@ final class StringCodec extends AbstractCodec<String> implements ArrayCodecDeleg
4849

4950
private final ByteBufAllocator byteBufAllocator;
5051

52+
private final PostgresTypeIdentifier defaultType;
53+
54+
private final PostgresTypeIdentifier arrayType;
55+
5156
StringCodec(ByteBufAllocator byteBufAllocator) {
57+
this(byteBufAllocator, VARCHAR, VARCHAR_ARRAY);
58+
}
59+
60+
StringCodec(ByteBufAllocator byteBufAllocator, PostgresTypeIdentifier defaultType, PostgresTypeIdentifier arrayType) {
5261
super(String.class);
5362
this.byteBufAllocator = Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null");
63+
this.defaultType = Assert.requireNonNull(defaultType, "defaultType must not be null");
64+
this.arrayType = Assert.requireNonNull(arrayType, "arrayType must not be null");
5465
}
5566

5667
@Override
5768
public EncodedParameter encodeNull() {
58-
return createNull(FORMAT_TEXT, VARCHAR);
69+
return createNull(FORMAT_TEXT, this.defaultType);
5970
}
6071

6172
@Override
6273
boolean doCanDecode(PostgresqlObjectId type, Format format) {
6374
Assert.requireNonNull(format, "format must not be null");
6475
Assert.requireNonNull(type, "type must not be null");
6576

66-
return SUPPORTED_TYPES.contains(type);
77+
return this.defaultType == type || SUPPORTED_TYPES.contains(type);
6778
}
6879

6980
@Override
@@ -75,7 +86,7 @@ String doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, @Nullable Forma
7586

7687
@Override
7788
EncodedParameter doEncode(String value) {
78-
return doEncode(value, VARCHAR);
89+
return doEncode(value, this.defaultType);
7990
}
8091

8192
@Override
@@ -94,12 +105,12 @@ public String encodeToText(String value) {
94105

95106
@Override
96107
public PostgresTypeIdentifier getArrayDataType() {
97-
return VARCHAR_ARRAY;
108+
return this.arrayType;
98109
}
99110

100111
@Override
101112
public Iterable<PostgresTypeIdentifier> getDataTypes() {
102-
return SUPPORTED_TYPES.stream().map(PostgresTypeIdentifier.class::cast).collect(Collectors.toList());
113+
return Stream.concat(Stream.of(this.defaultType), SUPPORTED_TYPES.stream()).map(PostgresTypeIdentifier.class::cast).collect(Collectors.toSet());
103114
}
104115

105116
private static class StringDecoder implements Codec<String>, Decoder<String> {

0 commit comments

Comments
 (0)