Skip to content

Commit

Permalink
Allow merging lists within dicts merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazoyer committed Jan 14, 2025
1 parent 6e82bba commit a8d105b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion infrahub_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ def deep_merge_dict(dicta: dict, dictb: dict, path: list | None = None) -> dict:
if key in dicta:
if isinstance(dicta[key], dict) and isinstance(dictb[key], dict):
deep_merge_dict(dicta[key], dictb[key], path + [str(key)])
elif isinstance(dicta[key], list) and isinstance(dictb[key], list):
# Merge lists
# Cannot use compare_list because list of dicts won't work (dict not hashable)
dicta[key] = [i for i in dicta[key] if i not in dictb[key]] + dictb[key]
elif dicta[key] == dictb[key]:
pass
continue

Check warning on line 144 in infrahub_sdk/utils.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/utils.py#L144

Added line #L144 was not covered by tests
else:
raise ValueError("Conflict at %s" % ".".join(path + [str(key)]))
else:
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/sdk/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ def test_deep_merge_dict():
a = {"keyA": 1}
b = {"keyB": {"sub1": 10}}
c = {"keyB": {"sub2": 20}}
d = {"keyA": [10, 20]}
e = {"keyA": [20, 30]}
assert deep_merge_dict(a, b) == {"keyA": 1, "keyB": {"sub1": 10}}
assert deep_merge_dict(c, b) == {"keyB": {"sub1": 10, "sub2": 20}}
assert deep_merge_dict(d, e) == {"keyA": [10, 20, 30]}


def test_str_to_bool():
Expand Down

0 comments on commit a8d105b

Please sign in to comment.