Skip to content

Commit

Permalink
adding merge list case to flatten_merge_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dsillman2000 committed Nov 23, 2023
1 parent b6525b1 commit 599a3fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion metadock/yaml_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import operator
from functools import reduce
from typing import Any


Expand All @@ -18,7 +20,10 @@ def flatten_merge_keys(yaml_dict: Any) -> dict:
for key in yaml_dict.keys():
flat_yaml_value = flatten_merge_keys(yaml_dict[key])
if key == "<<":
flattened_yaml_dict |= flat_yaml_value
if isinstance(flat_yaml_value, list):
flattened_yaml_dict |= reduce(operator.or_, flat_yaml_value, {})
elif isinstance(flat_yaml_value, dict):
flattened_yaml_dict |= flat_yaml_value
else:
flattened_yaml_dict[key] = flat_yaml_value

Expand Down
10 changes: 10 additions & 0 deletions tests/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
{"key": "value", "inner": "item", "first": "level"},
id="simple 2 level to flatten with adjacent key",
),
pytest.param(
{"<<": [{"key_a": "value"}, {"key_b": "value again"}], "first": "level"},
{"key_a": "value", "key_b": "value again", "first": "level"},
id="simple merge list",
),
pytest.param(
{"<<": [{"k": "value"}, {"k": "value again"}], "first": "level"},
{"k": "value again", "first": "level"},
id="simple merge list with collision",
),
],
)
def test_yaml_utils__flatten_merge_keys(yml_dict, flat_dict):
Expand Down

0 comments on commit 599a3fe

Please sign in to comment.