Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Arrays #4

Open
ravensorb opened this issue Jan 4, 2021 · 1 comment
Open

Support for Arrays #4

ravensorb opened this issue Jan 4, 2021 · 1 comment

Comments

@ravensorb
Copy link

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

@Metatronwings
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants