Skip to content

Commit 2ca31cd

Browse files
authored
Allow merging lists within dicts merge (#238)
1 parent 6e82bba commit 2ca31cd

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

infrahub_sdk/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ def deep_merge_dict(dicta: dict, dictb: dict, path: list | None = None) -> dict:
136136
if key in dicta:
137137
if isinstance(dicta[key], dict) and isinstance(dictb[key], dict):
138138
deep_merge_dict(dicta[key], dictb[key], path + [str(key)])
139+
elif isinstance(dicta[key], list) and isinstance(dictb[key], list):
140+
# Merge lists
141+
# Cannot use compare_list because list of dicts won't work (dict not hashable)
142+
dicta[key] = [i for i in dicta[key] if i not in dictb[key]] + dictb[key]
139143
elif dicta[key] == dictb[key]:
140-
pass
144+
continue
141145
else:
142146
raise ValueError("Conflict at %s" % ".".join(path + [str(key)]))
143147
else:

tests/unit/sdk/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ def test_deep_merge_dict():
8787
a = {"keyA": 1}
8888
b = {"keyB": {"sub1": 10}}
8989
c = {"keyB": {"sub2": 20}}
90+
d = {"keyA": [10, 20], "keyB": "foo"}
91+
e = {"keyA": [20, 30], "keyB": "foo"}
9092
assert deep_merge_dict(a, b) == {"keyA": 1, "keyB": {"sub1": 10}}
9193
assert deep_merge_dict(c, b) == {"keyB": {"sub1": 10, "sub2": 20}}
94+
assert deep_merge_dict(d, e) == {"keyA": [10, 20, 30], "keyB": "foo"}
9295

9396

9497
def test_str_to_bool():

0 commit comments

Comments
 (0)