Skip to content

Commit 8bd640a

Browse files
committed
review comments addressed - integration test fixed;missing license added;
1 parent 786da55 commit 8bd640a

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2017-2020 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+
* https://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+
117
package io.r2dbc.postgresql.codec;
218

319
/**

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

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2017-2020 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+
* https://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+
117
package io.r2dbc.postgresql.codec;
218

319
import io.netty.buffer.ByteBuf;
@@ -34,12 +50,18 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
3450
Assert.requireNonNull(type, "type must not be null");
3551
Assert.requireNonNull(format, "format must not be null");
3652

37-
String decodedAsString = ByteBufUtils.decode(buffer);
38-
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
39-
String[] coordinatesAsString = parenRemovedVal.split(",");
40-
double x = Double.parseDouble(coordinatesAsString[0]);
41-
double y = Double.parseDouble(coordinatesAsString[1]);
42-
return new Point(x, y);
53+
if (format == FORMAT_BINARY) {
54+
double x = buffer.readDouble();
55+
double y = buffer.readDouble();
56+
return new Point(x, y);
57+
} else {
58+
String decodedAsString = ByteBufUtils.decode(buffer);
59+
String parenRemovedVal = decodedAsString.replaceAll("[()]", "");
60+
String[] coordinatesAsString = parenRemovedVal.split(",");
61+
double x = Double.parseDouble(coordinatesAsString[0]);
62+
double y = Double.parseDouble(coordinatesAsString[1]);
63+
return new Point(x, y);
64+
}
4365
}
4466

4567
/**
@@ -49,12 +71,16 @@ Point doDecode(ByteBuf buffer, PostgresqlObjectId dataType, Format format, Class
4971
@Override
5072
Parameter doEncode(Point value) {
5173
Assert.requireNonNull(value, "value must not be null");
52-
return create(POINT, FORMAT_BINARY, () -> ByteBufUtils.encode(this.byteBufAllocator, value.toString()));
74+
return create(POINT, FORMAT_BINARY, () -> this.byteBufAllocator.buffer(lengthInBytes()).writeDouble(value.getX()).writeDouble(value.getY()));
5375
}
5476

5577
@Override
5678
public Parameter encodeNull() {
5779
return createNull(POINT, FORMAT_BINARY);
5880
}
5981

82+
public int lengthInBytes() {
83+
return 16;
84+
}
85+
6086
}

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

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
/*
2+
* Copyright 2017-2020 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+
* https://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+
117
package io.r2dbc.postgresql.codec;
218

19+
import io.netty.buffer.ByteBuf;
320
import io.r2dbc.postgresql.client.Parameter;
421
import io.r2dbc.postgresql.client.ParameterAssert;
5-
import io.r2dbc.postgresql.util.ByteBufUtils;
622
import org.junit.jupiter.api.Test;
723

824
import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE;
925
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
1026
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
1127
import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT;
1228
import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR;
13-
import static io.r2dbc.postgresql.util.ByteBufUtils.encode;
1429
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
1530
import static org.assertj.core.api.Assertions.assertThat;
1631
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -60,10 +75,8 @@ void doDecodeNoFormat() {
6075
void doDecode() {
6176
PointCodec codec = new PointCodec(TEST);
6277
Point point = new Point(1.12, 2.12);
63-
String encodedValue = "(1.12,2.12)";
64-
65-
assertThat(codec.doDecode(ByteBufUtils.encode(TEST, encodedValue), POINT, FORMAT_BINARY, Point.class))
66-
.isEqualTo(point);
78+
ByteBuf pointAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12);
79+
assertThat(codec.doDecode(pointAsBinary, POINT, FORMAT_BINARY, Point.class)).isEqualTo(point);
6780
}
6881

6982
@Test
@@ -75,11 +88,12 @@ void doEncodeNoValue() {
7588
@Test
7689
void doEncode() {
7790
PointCodec codec = new PointCodec(TEST);
91+
ByteBuf pointAsBinary = TEST.buffer(codec.lengthInBytes()).writeDouble(1.12).writeDouble(2.12);
7892

7993
ParameterAssert.assertThat(codec.doEncode(new Point(1.12, 2.12)))
8094
.hasFormat(FORMAT_BINARY)
8195
.hasType(POINT.getObjectId())
82-
.hasValue(encode(TEST, "(1.12,2.12)"));
96+
.hasValue(pointAsBinary);
8397
}
8498

8599
@Test

0 commit comments

Comments
 (0)