Skip to content

Add a "merge" option for box_merge_lists (#263) - #323

Open
ChrisJr404 wants to merge 1 commit into
cdgriffith:developfrom
ChrisJr404:merge-list-option
Open

Add a "merge" option for box_merge_lists (#263)#323
ChrisJr404 wants to merge 1 commit into
cdgriffith:developfrom
ChrisJr404:merge-list-option

Conversation

@ChrisJr404

Copy link
Copy Markdown

This adds a "merge" value for the existing box_merge_lists keyword so merge_update can combine lists of dictionaries element by element instead of replacing the whole list, which is what #263 asked for.

Right now box_merge_lists handles the two list-level strategies, extend and unique, but nested dictionaries buried inside a list still get dropped on merge. With this option merge_update walks both lists by index: when the same position holds a dict on each side it recurses (reusing merge_update so it keeps working at any depth), otherwise the incoming value wins, and any extra incoming elements are appended. The default behavior is unchanged since this only kicks in when you explicitly pass box_merge_lists="merge".

Using the example from the issue:

>>> a = Box({"data": [{"foo": 1, "foobar": 20}, {"bar": 2}]})
>>> a.merge_update({"data": [{"foo": 1, "baz": 10}]}, box_merge_lists="merge")
>>> a
Box({"data": [{"foo": 1, "foobar": 20, "baz": 10}, {"bar": 2}]})

I added a docstring to merge_update covering all the list modes, tests for the happy and sad paths, and CHANGES/AUTHORS entries.

merge_update can already combine lists with extend and unique, but there
was no way to merge lists of dictionaries the way nested dicts merge. Add
a "merge" option that walks both lists by index, recursing into dicts at
the same position so their keys combine and appending any extra incoming
items. Closes cdgriffith#263.
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

Successfully merging this pull request may close these issues.

1 participant