From a6b9364ebf649679a739bfe4988899c4d9186f76 Mon Sep 17 00:00:00 2001 From: Carl Gieringer Date: Thu, 4 Oct 2018 12:50:08 -0400 Subject: [PATCH] NIFI-5664 Support ArrayList in DataTypeUtils#toArray NIFI-5664 Generalize to handling List This closes #3049 Signed-off-by: Mike Thomsen --- .../serialization/record/util/DataTypeUtils.java | 5 +++++ .../serialization/record/TestDataTypeUtils.java | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java index 687d9ede1a50..336a70d11fea 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java @@ -339,6 +339,11 @@ public static Object[] toArray(final Object value, final String fieldName, final return dest; } + if (value instanceof List) { + final List list = (List)value; + return list.toArray(); + } + throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " to Object Array for field " + fieldName); } diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java index a8bc28d04e00..5f15fc2d046b 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java @@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -168,6 +169,18 @@ public void testConvertRecordFieldToObject() { } + @Test + public void testToArray() { + final List list = Arrays.asList("Seven", "Eleven", "Thirteen"); + + final Object[] array = DataTypeUtils.toArray(list, "list", null); + + assertEquals(list.size(), array.length); + for (int i = 0; i < list.size(); i++) { + assertEquals(list.get(i), array[i]); + } + } + @Test public void testStringToBytes() { Object bytes = DataTypeUtils.convertType("Hello", RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType()),null, StandardCharsets.UTF_8);