Skip to content

Commit

Permalink
Fix yaml.merge_seq not preserving order
Browse files Browse the repository at this point in the history
Order in sequence should now always be preserved, though metadata of the sequence items, such as comments, are going to be lost.
  • Loading branch information
user committed Dec 10, 2024
1 parent 5934047 commit 49ce2a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
17 changes: 9 additions & 8 deletions avtdl/core/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def make_yaml_parser() -> YAML:


def merge_data(base: CommentedData, data: PlainData) -> CommentedData:
if isinstance(base, CommentedSeq) and isinstance(data, list):
if str(base) == str(data):
return base
elif isinstance(base, CommentedSeq) and isinstance(data, list):
merge_seq(base, data)
return base
elif isinstance(base, CommentedMap) and isinstance(data, dict):
Expand All @@ -47,13 +49,12 @@ def merge_map(base: CommentedMap, data: dict) -> None:


def merge_seq(base: CommentedSeq, data: list) -> None:
to_delete = [v for v in base if v not in data]
for v in to_delete:
old_base = {str(v): v for v in base}
for v in old_base.values():
base.remove(v)
idx = 0
for v in data:
if v in base:
idx = base.index(v)
base[idx] = merge_data(base[idx], v)
if str(v) in old_base:
value = old_base[str(v)]
else:
base.insert(idx+1, v)
value = v
base.append(value)
18 changes: 11 additions & 7 deletions tests/unit/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ def merge_yaml(base: str, data_json: str) -> CommentedData:

testcases: Dict[str, Tuple[str, str, str]] = {
'comments preserved': (
"""top:
"""
# top comment
- 0
- 1 # nested comment
key:
# mapping comment
- "0"
- "1" # list comment
- 2""",
'{"top": [0, 1, 3]}',
'{"key": ["0", "1", 3]}',

"""top:
"""
# top comment
- 0
- 1 # nested comment
key:
# mapping comment
- "0"
- "1"
- 3""",
),
'parenthesses preserved': (
Expand Down

0 comments on commit 49ce2a1

Please sign in to comment.