Skip to content

Commit

Permalink
Issue #5 add simple_merge method to MultiDictGetter
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenVerstraelen authored and soxofaan committed Sep 16, 2022
1 parent 9960083 commit bea49b7
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/openeo_aggregator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ def union(self, key: str, skip_duplicates=False) -> list:
result.append(item)
return result

def simple_merge(self) -> dict:
"""
All dictionaries are merged following simple rules:
For list or sets: all elements are merged into a single list, without duplicates.
For dictionaries: all keys are added to a single dict, duplicate keys are merged recursively.
For all other types: the first value is returned.
It assumes that all duplicate keys in a dictionary have items of the same type.
"""
if len(self.dictionaries) == 0:
return {}
if len(self.dictionaries) == 1:
return self.dictionaries[0]

result = {}
for dictionary in self.dictionaries:
for key, item in dictionary.items():
if key in result:
if isinstance(item, list) or isinstance(item, set):
result[key] = self.merge_arrays(key, skip_duplicates=True)
elif isinstance(item, dict):
result[key] = self.select(key).simple_merge()
else:
result[key] = item
return result

def first(self, key, default=None):
return next(self.get(key), default)

Expand Down

0 comments on commit bea49b7

Please sign in to comment.