diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 686da62c7..d6620065b 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -21,6 +21,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version - Enhance query at Template for Inheritance (Document, Column, and Graph) - Enhance query at Repository for Inheritance (Document and Column) +- Fix MapReader when reads to an interaction of elements or a list of maps == [1.0.4] - 2023-12-19 diff --git a/jnosql-communication/jnosql-communication-core/src/main/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReader.java b/jnosql-communication/jnosql-communication-core/src/main/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReader.java index c788b6600..a5fd34979 100644 --- a/jnosql-communication/jnosql-communication-core/src/main/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReader.java +++ b/jnosql-communication/jnosql-communication-core/src/main/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReader.java @@ -26,6 +26,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -50,8 +51,8 @@ public boolean test(TypeSupplier typeReference) { if (type instanceof ParameterizedType parameterizedType) { return Map.class.equals(parameterizedType.getRawType()) && - parameterizedType.getActualTypeArguments()[0] instanceof Class && - parameterizedType.getActualTypeArguments()[1] instanceof Class; + parameterizedType.getActualTypeArguments()[0] instanceof Class && + parameterizedType.getActualTypeArguments()[1] instanceof Class; } return false; } @@ -72,37 +73,34 @@ private Map getMap(Class keyClass, Class valueClass, Object v if (Map.class.isInstance(value)) { return convertToMap(keyClass, valueClass, value); } - if (value instanceof Iterable iterable) { + if (value instanceof Iterable iterable) { List collection = new ArrayList<>(); iterable.forEach(collection::add); if (collection.isEmpty()) { return Collections.emptyMap(); - } - if (collection.size() == 1) { - Object object = collection.get(0); - if (object instanceof Map) { - return convertToMap(keyClass, valueClass, object); - } - if (object instanceof Entry) { - Map map = new HashMap<>(); - convertEntryToMap(object, map); - return convertToMap(keyClass, valueClass, map); - } + } else if (collection.stream().allMatch(Map.class::isInstance)) { + return collection.stream().map(m -> convertToMap(keyClass, valueClass, m)).map(Map::entrySet) + .flatMap(Collection::stream) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } else if (collection.stream().allMatch(Entry.class::isInstance)) { + Map map = new HashMap<>(); + collection.stream().forEach(e -> convertEntryToMap(e, map)); + return map; } } throw new UnsupportedOperationException("There is not supported convert" + value + " a not Map type."); } - private void convertEntryToMap(Object value, Map map) { + private void convertEntryToMap(Object value, Map map) { Entry entry = Entry.class.cast(value); Object entryValue = entry.value().get(); if (entryValue instanceof Entry) { Map subMap = new HashMap<>(); Entry subEntry = Entry.class.cast(entryValue); convertEntryToMap(subEntry, subMap); - map.put(entry.name(), subMap); + map.put((K) entry.name(), (V) subMap); } else { - map.put(entry.name(), entryValue); + map.put((K) entry.name(), (V) entryValue); } } diff --git a/jnosql-communication/jnosql-communication-core/src/test/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReaderTest.java b/jnosql-communication/jnosql-communication-core/src/test/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReaderTest.java index bf3742eae..3a785e80c 100644 --- a/jnosql-communication/jnosql-communication-core/src/test/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReaderTest.java +++ b/jnosql-communication/jnosql-communication-core/src/test/java/org/eclipse/jnosql/communication/reader/MapTypeReferenceReaderTest.java @@ -95,6 +95,22 @@ void shouldMutableMap2() { assertThat(map).hasSize(3).containsOnly(entry("1", "234"), entry("2", "2345"), entry("23", "123")); } + @Test + @DisplayName("Should create list of Maps") + void shouldMergeListMap() { + List> maps = new ArrayList<>(); + maps.add(Map.of(1, 234L, 2, 2345L)); + maps.add(Map.of(3, 234564L, 4, 3452L)); + + Map map = referenceReader.convert(new TypeReference<>() { + }, maps); + + + assertThat(map).hasSize(4).containsOnly(entry("1", "234"), + entry("2", "2345"), entry("3", "234564"), entry("4", "3452")); + } + + @Test @DisplayName("Should convert Entry to Map") void shouldConvertEntryToMap() { @@ -106,6 +122,17 @@ void shouldConvertEntryToMap() { assertThat(map).hasSize(1).contains(entry("key", "value")); } + @Test + @DisplayName("Should convert list of entry to Map") + void shouldConvertEntriesToMap() { + List entries = List.of(new EntryTest("key", Value.of("value")), + new EntryTest("key2", Value.of("value"))); + Map map = referenceReader.convert(new TypeReference<>() { + }, entries); + + assertThat(map).hasSize(2).contains(entry("key", "value"), entry("key2", "value")); + } + @Test @DisplayName("Should convert SubEntry to Map") void shouldConvertSubEntryToMap() {