From 69be7b72f54ace5a9dfae553e786dc47223fd554 Mon Sep 17 00:00:00 2001 From: Uttam Kumar Date: Mon, 11 Mar 2024 09:47:43 -0700 Subject: [PATCH] Map transformer used only if value is charseq --- .../src/main/avro/vs18/TestCollections.avsc | 10 ++++++++ .../avroutil1/builder/SpecificRecordTest.java | 7 +++++- .../CollectionTransformerUtil.java | 25 +++++++++++++++---- .../collectiontransformer/MapTransformer.java | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc b/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc index 42fa056f..3fbbd583 100644 --- a/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc +++ b/avro-builder/tests/codegen-18/src/main/avro/vs18/TestCollections.avsc @@ -79,6 +79,16 @@ "type": "array", "items": "int" } + }, + { + "name": "unionOfIntMap", + "type": [ + "null", + { + "type": "map", + "values": "int" + } + ] } ], "type": "record" diff --git a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java index 302c7b40..ff8825b3 100644 --- a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java +++ b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java @@ -1851,7 +1851,7 @@ public void testNewBuilder() throws Exception { public void modifiablePrimitiveCollectionTest() { String tba = "NewElement"; RandomRecordGenerator generator = new RandomRecordGenerator(); - TestCollections instance = generator.randomSpecific(TestCollections.class, RecordGenerationConfig.newConfig().withAvoidNulls(true)); + vs18.TestCollections instance = generator.randomSpecific(vs18.TestCollections.class, RecordGenerationConfig.newConfig().withAvoidNulls(true)); // array of string instance.getStrAr().add(tba); @@ -1877,6 +1877,11 @@ public void modifiablePrimitiveCollectionTest() { instance.getIntAr().add(Integer.MAX_VALUE); Assert.assertEquals((int) instance.getIntAr().get(instance.getIntAr().size() - 1), Integer.MAX_VALUE); Assert.assertEquals((int) instance.intAr.get(instance.getIntAr().size() - 1), Integer.MAX_VALUE); + + // Union (null, Map) + instance.getUnionOfIntMap().put("key1", Integer.MAX_VALUE); + Assert.assertEquals(Integer.MAX_VALUE, (int) instance.getUnionOfIntMap().get("key1")); + Assert.assertEquals(Integer.MAX_VALUE, (int) instance.getUnionOfIntMap().get("key1")); } @Test diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java index 39b02247..b567bad9 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/CollectionTransformerUtil.java @@ -7,6 +7,7 @@ package com.linkedin.avroutil1.compatibility.collectiontransformer; import com.linkedin.avroutil1.compatibility.StringUtils; +import java.util.Collections; import java.util.List; import java.util.Map; import org.apache.avro.util.Utf8; @@ -53,11 +54,18 @@ public static List createUtf8ListView(List utf8List) { * @param utf8Map map of {@link Utf8} objects * @return a {@link StringMapView} for the given map of {@link Utf8} objects */ - public static Map createStringMapView(Map utf8Map) { + public static Map createStringMapView(Map utf8Map) { if (utf8Map == null) { return null; } - return new StringMapView(utf8Map); + if (utf8Map.isEmpty()) { + return Collections.emptyMap(); + } + Object val = utf8Map.values().iterator().next(); + if (val instanceof CharSequence) { + return new StringMapView(utf8Map); + } + return utf8Map; } /** @@ -65,7 +73,7 @@ public static Map createStringMapView(Map utf8Map) { * @param utf8Map map of {@link Utf8} objects * @return a {@link CharSequenceMapView} for the given map of {@link Utf8} objects */ - public static Map createUtf8MapView(Map utf8Map) { + public static Map createUtf8MapView(Map utf8Map) { return utf8Map; } @@ -74,10 +82,17 @@ public static Map createUtf8MapView(Map utf8Map) { * @param utf8Map map of {@link Utf8} objects * @return a {@link CharSequenceMapView} for the given map of {@link Utf8} objects */ - public static Map createCharSequenceMapView(Map utf8Map) { + public static Map createCharSequenceMapView(Map utf8Map) { if (utf8Map == null) { return null; } - return new CharSequenceMapView(utf8Map); + if (utf8Map.isEmpty()) { + return Collections.emptyMap(); + } + Object val = utf8Map.values().iterator().next(); + if (val instanceof CharSequence) { + return new CharSequenceMapView(utf8Map); + } + return utf8Map; } } diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java index ad732d1b..78dc56de 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/MapTransformer.java @@ -62,7 +62,7 @@ public static Map getUtf8Map(Object mapObj, boolean isPrimitiveCollection) { public static Map getStringMap(Object mapObj, boolean isPrimitiveCollection) { if(isPrimitiveCollection) { - return CollectionTransformerUtil.createStringMapView((Map) mapObj); + return CollectionTransformerUtil.createStringMapView((Map) mapObj); } if (mapObj == null) { return null;