Skip to content

Commit b228e10

Browse files
authored
Fix money text decoding for negative values (#1359)
Fixes #1358 Signed-off-by: Thomas Segismont <[email protected]>
1 parent 4842084 commit b228e10

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/DataTypeCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,10 +1507,10 @@ private static Inet textDecodeInet(int index, int len, ByteBuf buff) {
15071507

15081508
private static Money textDecodeMoney(int index, int len, ByteBuf buff) {
15091509
String s = textDecodeVARCHAR(index, len, buff);
1510-
s = s.substring(1);
1510+
boolean negative = s.charAt(0) == '-';
15111511
long integerPart = 0;
15121512
int decimalPart = 0;
1513-
int idx = 0;
1513+
int idx = negative ? 2 : 1;
15141514
char c;
15151515
while (idx < s.length() && (c = s.charAt(idx++)) != '.') {
15161516
if (c >= '0' && c <= '9') {
@@ -1523,7 +1523,7 @@ private static Money textDecodeMoney(int index, int len, ByteBuf buff) {
15231523
decimalPart = decimalPart * 10 + (c - '0');
15241524
}
15251525
}
1526-
return new Money(integerPart, decimalPart);
1526+
return new Money(negative ? -integerPart : integerPart, decimalPart);
15271527
}
15281528

15291529
/**

vertx-pg-client/src/test/java/io/vertx/pgclient/data/MonetaryTypeSimpleCodecTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ public void testMoney(TestContext ctx) {
2525

2626
@Test
2727
public void testNegativeMoney(TestContext ctx) {
28-
// Does not look possible with text format
29-
Money expected = new Money(1234, 56);
28+
Money expected = new Money(-1234, 56);
3029
testDecodeGeneric(ctx, "-1234.56", "MONEY", "money", Tuple::getValue, Row::getValue, expected);
3130
}
3231

3332
@Test
3433
public void testMoneyArray(TestContext ctx) {
35-
Money expected = new Money(1234, 56);
36-
testDecodeGenericArray(ctx, "ARRAY ['1234.56' :: MONEY]", "money", Tuple::getValue, Row::getValue, expected);
34+
Money[] expected = {new Money(1234, 56), new Money(-1234, 56)};
35+
testDecodeGenericArray(ctx, "ARRAY ['1234.56' :: MONEY , '-1234.56' :: MONEY]", "money", Tuple::getValue, Row::getValue, expected);
3736
}
3837
}

0 commit comments

Comments
 (0)