diff --git a/avtdl/core/yaml.py b/avtdl/core/yaml.py index 1c25de4..f1e6262 100644 --- a/avtdl/core/yaml.py +++ b/avtdl/core/yaml.py @@ -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): @@ -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) diff --git a/tests/unit/test_yaml.py b/tests/unit/test_yaml.py index e7405b7..7e6de70 100644 --- a/tests/unit/test_yaml.py +++ b/tests/unit/test_yaml.py @@ -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': (