Skip to content

Commit b6014c6

Browse files
GarionSnebhale
authored andcommitted
All TEXT Values for Boolean
Previously, the BooleanCodec only decoded Java-standard representations of boolean values in data from the server. However, PostgreSQL has other representations that were not decoded. This change adds support for more, and Hopefully all, boolean representations. [#2]
1 parent db2fbb7 commit b6014c6

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ boolean doCanDecode(Format format, PostgresqlObjectId type) {
5555
Boolean doDecode(ByteBuf byteBuf, @Nullable Format format, @Nullable Class<? extends Boolean> type) {
5656
Objects.requireNonNull(byteBuf, "byteBuf must not be null");
5757

58-
return Boolean.valueOf(ByteBufUtils.decode(byteBuf));
58+
String decoded = ByteBufUtils.decode(byteBuf);
59+
return "1".equals(decoded) || "true".equalsIgnoreCase(decoded)
60+
|| "t".equalsIgnoreCase(decoded) || "yes".equalsIgnoreCase(decoded)
61+
|| "y".equalsIgnoreCase(decoded) || "on".equalsIgnoreCase(decoded);
5962
}
6063

6164
@Override

src/test/java/io/r2dbc/postgresql/codec/BooleanCodecTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import io.r2dbc.postgresql.client.Parameter;
2020
import org.junit.jupiter.api.Test;
2121

22+
import java.util.Arrays;
23+
2224
import static io.r2dbc.postgresql.message.Format.BINARY;
2325
import static io.r2dbc.postgresql.message.Format.TEXT;
2426
import static io.r2dbc.postgresql.type.PostgresqlObjectId.BOOL;
@@ -41,8 +43,10 @@ void constructorNoByteBufAllocator() {
4143
void decode() {
4244
BooleanCodec codec = new BooleanCodec(TEST);
4345

44-
assertThat(codec.decode(encode(TEST, "TRUE"), TEXT, Boolean.class)).isTrue();
45-
assertThat(codec.decode(encode(TEST, "FALSE"), TEXT, Boolean.class)).isFalse();
46+
Arrays.asList("1", "True", "T", "Yes", "Y", "On")
47+
.forEach(input -> assertThat(codec.decode(encode(TEST, input), TEXT, Boolean.class)).isTrue());
48+
Arrays.asList("0", "False", "F", "No", "N", "Off")
49+
.forEach(input -> assertThat(codec.decode(encode(TEST, input), TEXT, Boolean.class)).isFalse());
4650
}
4751

4852
@Test

0 commit comments

Comments
 (0)