-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1808 | Proposition to fix conversion logical type decimal from byt…
…es to json as string
- Loading branch information
1 parent
da6d83a
commit 5006bbc
Showing
6 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 11 additions & 3 deletions
14
...main/java/pl/allegro/tech/hermes/common/message/converter/AvroRecordToBytesConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...n/java/pl/allegro/tech/hermes/consumers/consumer/converter/DecimalToStringConversion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package pl.allegro.tech.hermes.consumers.consumer.converter; | ||
|
||
import org.apache.avro.Conversion; | ||
import org.apache.avro.Conversions; | ||
import org.apache.avro.LogicalType; | ||
import org.apache.avro.Schema; | ||
|
||
import java.math.BigDecimal; | ||
import java.nio.ByteBuffer; | ||
|
||
class DecimalToStringConversion extends Conversion<String> { | ||
private final Conversions.DecimalConversion decimalConversion = new Conversions.DecimalConversion(); | ||
|
||
@Override | ||
public Class<String> getConvertedType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public String fromBytes(ByteBuffer value, Schema schema, LogicalType type) { | ||
return decimalConversion.fromBytes(value, schema, type).toString(); | ||
} | ||
|
||
@Override | ||
public ByteBuffer toBytes(String value, Schema schema, LogicalType type) { | ||
return decimalConversion.toBytes(new BigDecimal(value), schema, type); | ||
} | ||
|
||
@Override | ||
public String getLogicalTypeName() { | ||
return "decimal"; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...va/pl/allegro/tech/hermes/consumers/consumer/converter/DecimalToStringConversionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package pl.allegro.tech.hermes.consumers.consumer.converter; | ||
|
||
import org.apache.avro.LogicalType; | ||
import org.apache.avro.LogicalTypes; | ||
import org.apache.avro.Schema; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.math.BigDecimal; | ||
import java.nio.ByteBuffer; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class DecimalToStringConversionTest { | ||
|
||
private Schema schema; | ||
private LogicalType logicalType; | ||
|
||
@Before | ||
public void setup() { | ||
schema = Schema.create(Schema.Type.BYTES); | ||
schema.addProp("logicalType", "decimal"); | ||
schema.addProp("precision", 10); | ||
schema.addProp("scale", 2); | ||
logicalType = LogicalTypes.fromSchema(schema); | ||
} | ||
|
||
@Test | ||
public void toFromBytes() { | ||
// given | ||
final String value = "19.91"; | ||
final DecimalToStringConversion conversion = new DecimalToStringConversion(); | ||
|
||
//when | ||
final ByteBuffer byteBuffer = conversion.toBytes(value, schema, logicalType); | ||
final String result = conversion.fromBytes(byteBuffer, schema, logicalType); | ||
|
||
//then | ||
Assert.assertEquals(result, value); | ||
} | ||
|
||
@Test | ||
public void fromToBytes() { | ||
// given | ||
final ByteBuffer value = ByteBuffer.wrap(new BigDecimal("19.91").unscaledValue().toByteArray()); | ||
final DecimalToStringConversion conversion = new DecimalToStringConversion(); | ||
|
||
//when | ||
final String decimal = conversion.fromBytes(value, schema, logicalType); | ||
final ByteBuffer result = conversion.toBytes(decimal, schema, logicalType); | ||
|
||
//then | ||
Assert.assertEquals(result, value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters