ableegoldman commented on a change in pull request #6592:
URL: https://github.com/apache/kafka/pull/6592#discussion_r630605834



##########
File path: 
clients/src/test/java/org/apache/kafka/common/serialization/SerializationTest.java
##########
@@ -106,6 +110,190 @@ public void stringSerdeShouldSupportDifferentEncodings() {
         }
     }
 
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldReturnEmptyCollection() {
+        List<Integer> testData = Arrays.asList();
+        Serde<List<Integer>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Integer());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get empty collection after serialization and 
deserialization on an empty list");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldReturnNull() {
+        List<Integer> testData = null;
+        Serde<List<Integer>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Integer());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get null after serialization and deserialization on an 
empty list");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripIntPrimitiveInput() {
+        List<Integer> testData = Arrays.asList(1, 2, 3);
+        Serde<List<Integer>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Integer());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of integer primitives after 
serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void 
listSerdeSerializerShouldReturnByteArrayOfFixedSizeForIntPrimitiveInput() {
+        List<Integer> testData = Arrays.asList(1, 2, 3);
+        Serde<List<Integer>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Integer());
+        assertEquals(21, listSerde.serializer().serialize(topic, 
testData).length,
+            "Should get length of 21 bytes after serialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripShortPrimitiveInput() {
+        List<Short> testData = Arrays.asList((short) 1, (short) 2, (short) 3);
+        Serde<List<Short>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Short());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of short primitives after 
serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void 
listSerdeSerializerShouldReturnByteArrayOfFixedSizeForShortPrimitiveInput() {
+        List<Short> testData = Arrays.asList((short) 1, (short) 2, (short) 3);
+        Serde<List<Short>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Short());
+        assertEquals(15, listSerde.serializer().serialize(topic, 
testData).length,
+            "Should get length of 15 bytes after serialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripFloatPrimitiveInput() {
+        List<Float> testData = Arrays.asList((float) 1, (float) 2, (float) 3);
+        Serde<List<Float>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Float());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of float primitives after 
serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void 
listSerdeSerializerShouldReturnByteArrayOfFixedSizeForFloatPrimitiveInput() {
+        List<Float> testData = Arrays.asList((float) 1, (float) 2, (float) 3);
+        Serde<List<Float>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Float());
+        assertEquals(21, listSerde.serializer().serialize(topic, 
testData).length,
+            "Should get length of 21 bytes after serialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripLongPrimitiveInput() {
+        List<Long> testData = Arrays.asList((long) 1, (long) 2, (long) 3);
+        Serde<List<Long>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Long());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of long primitives after 
serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void 
listSerdeSerializerShouldReturnByteArrayOfFixedSizeForLongPrimitiveInput() {
+        List<Long> testData = Arrays.asList((long) 1, (long) 2, (long) 3);
+        Serde<List<Long>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Long());
+        assertEquals(33, listSerde.serializer().serialize(topic, 
testData).length,
+            "Should get length of 33 bytes after serialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripDoublePrimitiveInput() {
+        List<Double> testData = Arrays.asList((double) 1, (double) 2, (double) 
3);
+        Serde<List<Double>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Double());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of double primitives after 
serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void 
listSerdeSerializerShouldReturnByteArrayOfFixedSizeForDoublePrimitiveInput() {
+        List<Double> testData = Arrays.asList((double) 1, (double) 2, (double) 
3);
+        Serde<List<Double>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Double());
+        assertEquals(33, listSerde.serializer().serialize(topic, 
testData).length,
+            "Should get length of 33 bytes after serialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripUUIDInput() {
+        List<UUID> testData = Arrays.asList(UUID.randomUUID(), 
UUID.randomUUID(), UUID.randomUUID());
+        Serde<List<UUID>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.UUID());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of UUID after serialization 
and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void 
listSerdeSerializerShouldReturnByteArrayOfFixedSizeForUUIDInput() {
+        List<UUID> testData = Arrays.asList(UUID.randomUUID(), 
UUID.randomUUID(), UUID.randomUUID());
+        Serde<List<UUID>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.UUID());
+        assertEquals(117, listSerde.serializer().serialize(topic, 
testData).length,
+            "Should get length of 117 bytes after serialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripNonPrimitiveInput() {
+        List<String> testData = Arrays.asList("A", "B", "C");
+        Serde<List<String>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.String());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of strings list after 
serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripPrimitiveInputWithNullEntries() {
+        List<Integer> testData = Arrays.asList(1, null, 3);
+        Serde<List<Integer>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.Integer());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of integer primitives with 
null entries "
+                + "after serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldRoundtripNonPrimitiveInputWithNullEntries() {
+        List<String> testData = Arrays.asList("A", null, "C");
+        Serde<List<String>> listSerde = Serdes.ListSerde(ArrayList.class, 
Serdes.String());
+        assertEquals(testData,
+            listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData)),
+            "Should get the original collection of strings list with null 
entries "
+                + "after serialization and deserialization");
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void listSerdeShouldReturnLinkedList() {
+        List<Integer> testData = new LinkedList<>();
+        Serde<List<Integer>> listSerde = Serdes.ListSerde(LinkedList.class, 
Serdes.Integer());
+        assertTrue(listSerde.deserializer().deserialize(topic, 
listSerde.serializer().serialize(topic, testData))
+            instanceof LinkedList, "Should return List instance of type 
LinkedList");
+    }
+
+    @SuppressWarnings("unchecked")

Review comment:
       Thanks for the context, it is what it is (and I agree with your decision 
to prioritize ease of use). I was more wondering whether we might be missing a 
`SuppressWarnings("unchecked")` on one of the methods in the implementation, so 
that the user isn't forced to do the suppression themselves. But I can't quite 
tell where the warning is coming from, since it seems like we do already 
suppress unchecked warnings in `ListDeserializer#createListInstance`  where the 
casting occurs? Is it possible this was just left over from an earlier version, 
and we no longer need all the suppressions on these tests?

##########
File path: 
clients/src/main/java/org/apache/kafka/common/serialization/ListSerializer.java
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.serialization;
+
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.utils.Utils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static 
org.apache.kafka.common.serialization.Serdes.ListSerde.SerializationStrategy;
+
+public class ListSerializer<Inner> implements Serializer<List<Inner>> {
+
+    private static final List<Class<? extends Serializer<?>>> 
FIXED_LENGTH_SERIALIZERS = Arrays.asList(
+        ShortSerializer.class,
+        IntegerSerializer.class,
+        FloatSerializer.class,
+        LongSerializer.class,
+        DoubleSerializer.class,
+        UUIDSerializer.class);
+
+    private Serializer<Inner> inner;
+    private SerializationStrategy serStrategy;
+    private boolean isFixedLength;
+
+    public ListSerializer() {}
+
+    public ListSerializer(Serializer<Inner> inner) {
+        if (inner == null) {
+            throw new IllegalArgumentException("ListSerializer requires 
\"serializer\" parameter to be provided during initialization");
+        }
+        this.inner = inner;
+        this.isFixedLength = 
FIXED_LENGTH_SERIALIZERS.contains(inner.getClass());
+        this.serStrategy = this.isFixedLength ? 
SerializationStrategy.CONSTANT_SIZE : SerializationStrategy.VARIABLE_SIZE;
+    }
+
+    public Serializer<Inner> getInnerSerializer() {
+        return inner;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public void configure(Map<String, ?> configs, boolean isKey) {
+        if (inner != null) {
+            throw new ConfigException("List serializer was already initialized 
using a non-default constructor");
+        }
+        final String innerSerdePropertyName = isKey ? 
CommonClientConfigs.DEFAULT_LIST_KEY_SERDE_INNER_CLASS : 
CommonClientConfigs.DEFAULT_LIST_VALUE_SERDE_INNER_CLASS;
+        final Object innerSerdeClassOrName = 
configs.get(innerSerdePropertyName);
+        if (innerSerdeClassOrName == null) {
+            throw new ConfigException("Not able to determine the serializer 
class because it was neither passed via the constructor nor set in the 
config.");
+        }
+        try {
+            if (innerSerdeClassOrName instanceof String) {
+                inner = Utils.newInstance((String) innerSerdeClassOrName, 
Serde.class).serializer();
+            } else if (innerSerdeClassOrName instanceof Class) {
+                inner = (Serializer<Inner>) ((Serde) Utils.newInstance((Class) 
innerSerdeClassOrName)).serializer();
+            } else {
+                throw new KafkaException("Could not create a serializer class 
instance using \"" + innerSerdePropertyName + "\" property.");
+            }
+            inner.configure(configs, isKey);
+            isFixedLength = 
FIXED_LENGTH_SERIALIZERS.contains(inner.getClass());
+            serStrategy = this.isFixedLength ? 
SerializationStrategy.CONSTANT_SIZE : SerializationStrategy.VARIABLE_SIZE;
+        } catch (final ClassNotFoundException e) {
+            throw new ConfigException(innerSerdePropertyName, 
innerSerdeClassOrName, "Serializer class " + innerSerdeClassOrName + " could 
not be found.");
+        }
+    }
+
+    private void serializeNullIndexList(final DataOutputStream out, 
List<Inner> data) throws IOException {
+        List<Integer> nullIndexList = IntStream.range(0, data.size())
+                .filter(i -> data.get(i) == null)

Review comment:
       Yep, exactly (not sure why I said to use `for int i`, obviously that 
suffers from the same problem -- the iterator is what I had in mind)

##########
File path: 
clients/src/main/java/org/apache/kafka/common/serialization/Serdes.java
##########
@@ -125,6 +126,27 @@ public UUIDSerde() {
         }
     }
 
+    static public final class ListSerde<Inner> extends 
WrapperSerde<List<Inner>> {

Review comment:
       Ah, I didn't notice...tbh we should probably just fix all of them, but 
it's fine with me to leave that out of this PR and just conform to this for 
now. I'll leave it up to you




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to