Skip to content

Commit 61083af

Browse files
refactor: address review feedback
- Short-circuit list diffing in merge (replace entirely per spec) - Add root-exists comment to merge() for consistency with sync() - Add flow sequence tests to test_merge.py
1 parent 4bcb436 commit 61083af

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/yamltrip/document.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ def merge(self, *keys: KeyPart, value: Any) -> Document:
555555
normalized = _normalize_keys(keys) if keys else ()
556556

557557
# If path doesn't exist, delegate to upsert.
558+
# Root (empty keys) always exists, so skip the check.
558559
if normalized:
559560
route = _make_route(normalized)
560561
if not self._core_doc.query_exists(route):

src/yamltrip/sync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def _compute_patches(
3232
new_is_list = isinstance(new_value, list)
3333

3434
if old_is_list and new_is_list:
35+
if not remove_extra:
36+
# merge semantics: lists replace entirely (no element-wise diff)
37+
route = _core.Route(list(path))
38+
return [_core.Patch(route=route, operation=_core.Op.replace(new_value))]
3539
return _diff_lists(old_value, new_value, path)
3640

3741
# Type mismatch or scalar change — replace

tests/test_merge.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def test_nested_list_replaced(self):
4040
assert doc2["cfg", "name"] == "foo"
4141

4242

43+
class TestMergeFlowSequence:
44+
def test_flow_sequence_replaced(self):
45+
doc = Document("cfg:\n tags: [alpha, beta]\n name: foo\n")
46+
doc2 = doc.merge("cfg", value={"tags": ["gamma"], "extra": 1})
47+
assert doc2["cfg", "tags"] == ["gamma"]
48+
assert doc2["cfg", "name"] == "foo"
49+
assert doc2["cfg", "extra"] == 1
50+
51+
def test_flow_sequence_at_path(self):
52+
doc = Document("items: [a, b, c]\n")
53+
doc2 = doc.merge("items", value=["x", "y"])
54+
assert doc2["items"] == ["x", "y"]
55+
56+
4357
class TestMergeTypePromotion:
4458
def test_scalar_to_mapping(self):
4559
doc = Document("settings: defaults\n")

0 commit comments

Comments
 (0)