Skip to content

Commit

Permalink
Fixed ability to merge a list of dicts in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Dec 16, 2022
1 parent d61010b commit f3698ef
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cookie_composer/merge_files/yaml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pathlib import Path

from frozendict import frozendict

from cookie_composer import data_merge
from cookie_composer.composition import (
COMPREHENSIVE,
Expand All @@ -24,9 +26,10 @@ def merge_yaml_files(new_file: Path, existing_file: Path, merge_strategy: str):
Raises:
MergeError: If something goes wrong
"""
from ruamel.yaml import YAML, YAMLError
from ruamel.yaml import YAML, SafeRepresenter, YAMLError

yaml = YAML(typ="safe")
yaml.Representer.add_representer(frozendict, SafeRepresenter.represent_dict)

if merge_strategy == DO_NOT_MERGE:
raise MergeError(
Expand Down
24 changes: 22 additions & 2 deletions tests/fixtures/existing.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
{
"number": 1,
"string": "abc",
"list": ["a", 1, "c"],
"dictionary": {"a": 1, "b": [1, 2]}
"list": [
"a",
1,
"c"
],
"dictionary": {
"a": 1,
"b": [
1,
2
]
},
"list_of_dicts": [
{
"a": 1,
"b": 2
},
{
"c": 3,
"d": 4
}
]
}
1 change: 1 addition & 0 deletions tests/fixtures/existing.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ string = "abc"
list = ["a", "1", "c"]
dictionary.a = 1
dictionary.b = [1, 2]
list_of_dicts = [{a = 1, b = 2}, {c = 3, d = 4}]
5 changes: 5 additions & 0 deletions tests/fixtures/existing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ dictionary:
"b":
- 1
- 2
list_of_dicts:
- a: 1
b: 2
- c: 3
d: 4
20 changes: 18 additions & 2 deletions tests/fixtures/new.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{
"number": 2,
"string": "def",
"list": ["a", 2],
"dictionary": {"b": [3, 2]}
"list": [
"a",
2
],
"dictionary": {
"b": [
3,
2
]
},
"list_of_dicts": [
{
"e": 1
},
{
"f": 3
}
]
}
1 change: 1 addition & 0 deletions tests/fixtures/new.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ number = 2
string = "def"
list = ["a", "2"]
dictionary.b = [3, 2]
list_of_dicts = [{e = 1}, {f = 3}]
3 changes: 3 additions & 0 deletions tests/fixtures/new.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ dictionary:
"b":
- 3
- 2
list_of_dicts:
- e: 1
- f: 3
2 changes: 2 additions & 0 deletions tests/test_merge_files_json_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_overwrite_merge(tmp_path, fixtures_path):
"string": "def",
"list": ["a", 2],
"dictionary": {"b": [3, 2]},
"list_of_dicts": [{"e": 1}, {"f": 3}],
}


Expand All @@ -52,6 +53,7 @@ def test_overwrite_nested_merge(tmp_path, fixtures_path):
"string": "def",
"list": ["a", 2],
"dictionary": {"a": 1, "b": [3, 2]},
"list_of_dicts": [{"e": 1}, {"f": 3}],
}


Expand Down
2 changes: 2 additions & 0 deletions tests/test_merge_files_toml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_overwrite_merge(tmp_path, fixtures_path):
"string": "def",
"list": ["a", "2"],
"dictionary": {"b": [3, 2]},
"list_of_dicts": [{"e": 1}, {"f": 3}],
}
}

Expand All @@ -57,6 +58,7 @@ def test_overwrite_nested_merge(tmp_path, fixtures_path):
"string": "def",
"list": ["a", "2"],
"dictionary": {"a": 1, "b": [3, 2]},
"list_of_dicts": [{"e": 1}, {"f": 3}],
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/test_merge_files_yaml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_overwrite_merge(tmp_path, fixtures_path):
"string": "def",
"list": ["a", 2],
"dictionary": {"b": [3, 2]},
"list_of_dicts": [{"e": 1}, {"f": 3}],
}


Expand All @@ -56,6 +57,7 @@ def test_overwrite_nested_merge(tmp_path, fixtures_path):
"string": "def",
"list": ["a", 2],
"dictionary": {"a": 1, "b": [3, 2]},
"list_of_dicts": [{"e": 1}, {"f": 3}],
}


Expand All @@ -73,6 +75,7 @@ def test_comprehensive_merge(tmp_path, fixtures_path):
assert set(rendered["list"]) == {"a", 1, 2, "c"}
assert rendered["dictionary"]["a"] == 1
assert set(rendered["dictionary"]["b"]) == {1, 2, 3}
assert len(rendered["list_of_dicts"]) == 4


def test_bad_files(tmp_path, fixtures_path):
Expand Down

0 comments on commit f3698ef

Please sign in to comment.