Skip to content

Commit

Permalink
Merge pull request #476 from eclipse/fix-map-reader
Browse files Browse the repository at this point in the history
Fix map reader
  • Loading branch information
otaviojava authored Feb 1, 2024
2 parents 4e5a8a1 + bdb3a17 commit 1f897f4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -72,37 +73,34 @@ private <K, V> Map<K, V> getMap(Class<K> keyClass, Class<V> valueClass, Object v
if (Map.class.isInstance(value)) {
return convertToMap(keyClass, valueClass, value);
}
if (value instanceof Iterable iterable) {
if (value instanceof Iterable<?> iterable) {
List<Object> 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<String, Object> 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<K, V> 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<String, Object> map) {
private <K, V> void convertEntryToMap(Object value, Map<K, V> map) {
Entry entry = Entry.class.cast(value);
Object entryValue = entry.value().get();
if (entryValue instanceof Entry) {
Map<String, Object> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<Integer, Long>> maps = new ArrayList<>();
maps.add(Map.of(1, 234L, 2, 2345L));
maps.add(Map.of(3, 234564L, 4, 3452L));

Map<String, String> 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() {
Expand All @@ -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<Entry> entries = List.of(new EntryTest("key", Value.of("value")),
new EntryTest("key2", Value.of("value")));
Map<String, String> 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() {
Expand Down

0 comments on commit 1f897f4

Please sign in to comment.