You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by al...@apache.org on 2018/05/10 15:50:23 UTC
[31/50] nifi git commit: NIFI-4846: AvroTypeUtil to support more
input types for logical decimal conversion
NIFI-4846: AvroTypeUtil to support more input types for logical decimal conversion
Signed-off-by: Matthew Burgess <ma...@apache.org>
This closes #2451
Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/d17b9978
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/d17b9978
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/d17b9978
Branch: refs/heads/HDF-3.1-maint
Commit: d17b99785e508cf7d3d3b7a57d643fe5f2660a4d
Parents: 6d84478
Author: Koji Kawamura <ij...@apache.org>
Authored: Tue Feb 6 16:36:15 2018 +0900
Committer: Matt Gilman <ma...@gmail.com>
Committed: Wed Feb 7 20:23:32 2018 -0500
----------------------------------------------------------------------
.../java/org/apache/nifi/avro/AvroTypeUtil.java | 11 +++++++
.../org/apache/nifi/avro/TestAvroTypeUtil.java | 30 ++++++++++++++++++--
2 files changed, 39 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/nifi/blob/d17b9978/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java
----------------------------------------------------------------------
diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java
index f7b8d9b..c4f69d4 100644
--- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java
+++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/avro/AvroTypeUtil.java
@@ -533,8 +533,19 @@ public class AvroTypeUtil {
final BigDecimal rawDecimal;
if (rawValue instanceof BigDecimal) {
rawDecimal = (BigDecimal) rawValue;
+
} else if (rawValue instanceof Double) {
rawDecimal = BigDecimal.valueOf((Double) rawValue);
+
+ } else if (rawValue instanceof String) {
+ rawDecimal = new BigDecimal((String) rawValue);
+
+ } else if (rawValue instanceof Integer) {
+ rawDecimal = new BigDecimal((Integer) rawValue);
+
+ } else if (rawValue instanceof Long) {
+ rawDecimal = new BigDecimal((Long) rawValue);
+
} else {
throw new IllegalTypeConversionException("Cannot convert value " + rawValue + " of type " + rawValue.getClass() + " to a logical decimal");
}
http://git-wip-us.apache.org/repos/asf/nifi/blob/d17b9978/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java
----------------------------------------------------------------------
diff --git a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java
index d644f7c..d24488e 100644
--- a/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java
+++ b/nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/test/java/org/apache/nifi/avro/TestAvroTypeUtil.java
@@ -19,6 +19,7 @@ package org.apache.nifi.avro;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.IOException;
import java.math.BigDecimal;
@@ -291,14 +292,39 @@ public class TestAvroTypeUtil {
expects.put(new BigDecimal("0.123456789012345678"), "0.12345679");
+ // String to BigDecimal
+ expects.put("123", "123.00000000");
+ expects.put("1234567890.12345678", "1234567890.12345678");
+ expects.put("123456789012345678", "123456789012345678.00000000");
+ expects.put("0.1234567890123456", "0.12345679");
+ expects.put("Not a number", "java.lang.NumberFormatException");
+
+ // Integer to BigDecimal
+ expects.put(123, "123.00000000");
+ expects.put(-1234567, "-1234567.00000000");
+
+ // Long to BigDecimal
+ expects.put(123L, "123.00000000");
+ expects.put(123456789012345678L, "123456789012345678.00000000");
+
expects.forEach((rawValue, expect) -> {
- final Object convertedValue = AvroTypeUtil.convertToAvroObject(rawValue, fieldSchema);
+ final Object convertedValue;
+ try {
+ convertedValue = AvroTypeUtil.convertToAvroObject(rawValue, fieldSchema);
+ } catch (Exception e) {
+ if (expect.equals(e.getClass().getCanonicalName())) {
+ // Expected behavior.
+ return;
+ }
+ fail(String.format("Unexpected exception, %s with %s %s while expecting %s", e, rawValue.getClass().getSimpleName(), rawValue, expect));
+ return;
+ }
assertTrue(convertedValue instanceof ByteBuffer);
final ByteBuffer serializedBytes = (ByteBuffer) convertedValue;
final BigDecimal bigDecimal = new Conversions.DecimalConversion().fromBytes(serializedBytes, fieldSchema, decimalType);
- assertEquals(String.format("%s should be converted to %s", rawValue, expect), expect, bigDecimal.toString());
+ assertEquals(String.format("%s %s should be converted to %s", rawValue.getClass().getSimpleName(), rawValue, expect), expect, bigDecimal.toString());
});
}