clarketm / mergedeep

A deep merge function for šŸ.

Home Page:https://mergedeep.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Arrays

ravensorb opened this issue Ā· comments

commented

Are there any plans for support for complex arrays (ex: list of dict objects)?

Hi, some time ago I had the same problem. I came up with a way to solve it, maybe not exactly right, but hope it helps.

Here's the code:

# Original code
def _handle_merge_additive(destination, source, key):
    # Values are combined into one long collection.
    if isinstance(destination[key], list) and isinstance(source[key], list):
        # Extend destination if both destination and source are `list` type.
        destination[key].extend(deepcopy(source[key]))
    # and so on ...

# Changed code
def _handle_merge_additive(destination, source, key):
    # Values are combined into one long collection.
    if isinstance(destination[key], list) and isinstance(source[key], list):
        # First, need to consider what type of elements are in `dst`
        # If there is no element in `dst`, or the elements in `dst` are str, int and float type,
        # then replace `dst` with `src`
        if len(destination[key]) == 0 or isinstance(destination[key][0], (int, float, str)):
            destination[key] = deepcopy(source[key])
        # If the number of elements in `dst` is less than `src`,
        # then traverse `dst` according to the number of elements in `dst`
        elif len(destination[key]) <= len(source[key]):
            for i in range(len(destination[key])):
                _deepmerge(destination[key][i], source[key][i], Strategy.ADDITIVE)
        # If the number of elements in `dst` is more than `src`,
        # then append empty dict in `src`, until the number of elements in `src` is the same as `dst`
        else:
            source[key].extend([{} for x in range(len(destination[key]) - len(source[key]))])
            for i in range(len(destination[key])):
                _deepmerge(destination[key][i], source[key][i], Strategy.ADDITIVE)        
    # and so on ...

And it works for me!

Note: this modification will change the way original mergedeep handles list with int, str, float or sth not dict in it. Original mergedeep extends the list content, but changed mergedeep replaces the list content.