Skip to content

Additive merge for dict with list values? #11

@oerp-odoo

Description

@oerp-odoo

I have these dictionaries as a sample:

Y1 = {'migration': {'options': {'install_command': 'odoo2'},
  'versions': [{'version': 'setup',
    'operations': {'pre': ["echo 'pre-operation'"]},
    'addons': {'install': ['crm'], 'upgrade': ['note']}},
   {'version': '0.1.0',
    'operations': {'post': ["echo 'post-operation'"]},
    'addons': {'install': ['web', 'contacts']}}]}}
Y2 = {'migration': {'options': {'install_command': 'odoo'},
  'versions': [{'version': 'setup',
    'operations': {'pre': ["echo 'pre-operation-2'", "echo 'pre-operation'"]},
    'addons': {'install': ['account', 'crm'], 'upgrade': ['note', 'hr']}},
   {'version': '0.1.0',
    'operations': {'post': ["echo 'post-operation'",
      "echo 'post-operation-2'"]},
    'addons': {'install': ['purchase', 'sale']}}]}}
Y3 = {'migration': {'versions': [{'version': '0.2.0',
    'addons': {'install': ['mrp', 'stock']}}]}}

I merge it like this:

from mergedeep import merge, Strategy

d = merge({}, Y1, Y2, Y2, strategy=Strategy.ADDITIVE)
print(d)

And get this output:

{'migration': {'options': {'install_command': 'odoo'}, 'versions': [{'version': 'setup', 'operations': {'pre': ["echo 'pre-operation'"]}, 'addons': {'install': ['crm'], 'upgrade': ['note']}}, {'version': '0.1.0', 'operations': {'post': ["echo 'post-operation'"]}, 'addons': {'install': ['web', 'contacts']}}, {'version': 'setup', 'operations': {'pre': ["echo 'pre-operation-2'", "echo 'pre-operation'"]}, 'addons': {'install': ['account', 'crm'], 'upgrade': ['note', 'hr']}}, {'version': '0.1.0', 'operations': {'post': ["echo 'post-operation'", "echo 'post-operation-2'"]}, 'addons': {'install': ['purchase', 'sale']}}, {'version': 'setup', 'operations': {'pre': ["echo 'pre-operation-2'", "echo 'pre-operation'"]}, 'addons': {'install': ['account', 'crm'], 'upgrade': ['note', 'hr']}}, {'version': '0.1.0', 'operations': {'post': ["echo 'post-operation'", "echo 'post-operation-2'"]}, 'addons': {'install': ['purchase', 'sale']}}]}}

Yet I expect lists to be appended. For example from dicts Y1, Y2, install key in setup version, is ['crm'] and ['account', 'crm'], yet first one is kept values from Y2 is ignored. I expect it to become ['crm, 'account', 'crm']

Is my assumption is incorrect and mergedeep works as intended?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions