Skip to content

Commit

Permalink
Changed BooleanCodec/Test
Browse files Browse the repository at this point in the history
  • Loading branch information
svats0001 committed Oct 21, 2024
1 parent acff429 commit c091335
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ private BooleanCodec() {
@Override
public Boolean decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> target, boolean binary,
CodecContext context) {
return binary || metadata.getType() == MySqlType.BIT ? value.readBoolean() : value.readByte() != '0';
MySqlType dataType = metadata.getType();
if (dataType == MySqlType.VARCHAR) {
if (value.isReadable()) {
String stringVal = value.toString(metadata.getCharCollation(context).getCharset());
if (stringVal.equalsIgnoreCase("true") || stringVal.equals("1")) {
return true;
} else if (stringVal.equalsIgnoreCase("false") || stringVal.equals("0")) {
return false;
}
}
}
return binary || dataType == MySqlType.BIT ? value.readBoolean() : value.readByte() != '0';
}

@Override
Expand All @@ -54,8 +65,8 @@ public MySqlParameter encode(Object value, CodecContext context) {
@Override
public boolean doCanDecode(MySqlReadableMetadata metadata) {
MySqlType type = metadata.getType();
return (type == MySqlType.BIT || type == MySqlType.TINYINT) &&
Integer.valueOf(1).equals(metadata.getPrecision());
return ((type == MySqlType.BIT || type == MySqlType.TINYINT) &&
Integer.valueOf(1).equals(metadata.getPrecision())) || type == MySqlType.VARCHAR;
}

private static final class BooleanMySqlParameter extends AbstractMySqlParameter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@

package io.asyncer.r2dbc.mysql.codec;

import io.asyncer.r2dbc.mysql.ConnectionContextTest;
import io.asyncer.r2dbc.mysql.constant.MySqlType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.Charset;
import java.util.Arrays;

import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link BooleanCodec}.
*/
Expand Down Expand Up @@ -55,4 +61,30 @@ public ByteBuf[] binaryParameters(Charset charset) {
public ByteBuf sized(ByteBuf value) {
return value;
}

@Test
void decodeString() {
Codec<Boolean> codec = getCodec();
Charset c = ConnectionContextTest.mock().getClientCollation().getCharset();
Decoding d1 = new Decoding(Unpooled.copiedBuffer("true", c), "true", MySqlType.VARCHAR);
Decoding d2 = new Decoding(Unpooled.copiedBuffer("false", c), "false", MySqlType.VARCHAR);
Decoding d3 = new Decoding(Unpooled.copiedBuffer("1", c), "1", MySqlType.VARCHAR);
Decoding d4 = new Decoding(Unpooled.copiedBuffer("0", c), "0", MySqlType.VARCHAR);

assertThat(codec.decode(d1.content(), d1.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d1)
.isEqualTo(true);

assertThat(codec.decode(d2.content(), d2.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d2)
.isEqualTo(false);

assertThat(codec.decode(d3.content(), d3.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d3)
.isEqualTo(true);

assertThat(codec.decode(d4.content(), d4.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d4)
.isEqualTo(false);
}
}

0 comments on commit c091335

Please sign in to comment.