Skip to content

Commit

Permalink
Map transformer used only if value is charseq
Browse files Browse the repository at this point in the history
  • Loading branch information
li-ukumar committed Mar 11, 2024
1 parent 2fd93ac commit 69be7b7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@
"type": "array",
"items": "int"
}
},
{
"name": "unionOfIntMap",
"type": [
"null",
{
"type": "map",
"values": "int"
}
]
}
],
"type": "record"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String, Integer>)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,19 +54,26 @@ public static List<Utf8> createUtf8ListView(List<Utf8> utf8List) {
* @param utf8Map map of {@link Utf8} objects
* @return a {@link StringMapView} for the given map of {@link Utf8} objects
*/
public static Map<String, String> createStringMapView(Map<Utf8, Utf8> utf8Map) {
public static Map<String, String> 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;
}

/**
* Returns a {@link CharSequenceMapView} for the given map of {@link Utf8} objects.
* @param utf8Map map of {@link Utf8} objects
* @return a {@link CharSequenceMapView} for the given map of {@link Utf8} objects
*/
public static Map<Utf8, Utf8> createUtf8MapView(Map<Utf8, Utf8> utf8Map) {
public static Map<Utf8, Utf8> createUtf8MapView(Map utf8Map) {
return utf8Map;
}

Expand All @@ -74,10 +82,17 @@ public static Map<Utf8, Utf8> createUtf8MapView(Map<Utf8, Utf8> utf8Map) {
* @param utf8Map map of {@link Utf8} objects
* @return a {@link CharSequenceMapView} for the given map of {@link Utf8} objects
*/
public static Map<CharSequence, CharSequence> createCharSequenceMapView(Map<Utf8, Utf8> utf8Map) {
public static Map<CharSequence, CharSequence> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utf8, Utf8>) mapObj);
return CollectionTransformerUtil.createStringMapView((Map) mapObj);
}
if (mapObj == null) {
return null;
Expand Down

0 comments on commit 69be7b7

Please sign in to comment.