Skip to content

Commit 7d9b764

Browse files
feat: add Editor.merge() delegate
1 parent 52c3afb commit 7d9b764

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/yamltrip/editor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ def sync(self, *keys: KeyPart, value: Any) -> None:
151151
"""Sync the value at path to match the desired value."""
152152
self._document = self.document.sync(*keys, value=value)
153153

154+
def merge(self, *keys: KeyPart, value: Any) -> None:
155+
"""Merge a value into the mapping at path without removing extra keys."""
156+
self._document = self.document.merge(*keys, value=value)
157+
154158
def find_index(self, *keys: KeyPart, where: dict[str, Any]) -> int | None:
155159
"""Return the index of the first list item matching all key/value pairs."""
156160
return self.document.find_index(*keys, where=where)

tests/test_merge.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from yamltrip import edit
12
from yamltrip.document import Document
23

34

@@ -74,3 +75,15 @@ def test_returns_self_when_subset(self):
7475
doc = Document("a: 1\nb: 2\nc: 3\n")
7576
doc2 = doc.merge(value={"a": 1, "b": 2})
7677
assert doc2 is doc
78+
79+
80+
class TestEditorMerge:
81+
def test_editor_merge(self, tmp_path):
82+
p = tmp_path / "test.yaml"
83+
p.write_text("a: 1\nb: 2\n", encoding="utf-8")
84+
with edit(p) as ed:
85+
ed.merge(value={"a": 99, "c": 3})
86+
result = p.read_text(encoding="utf-8")
87+
assert "a: 99" in result
88+
assert "b: 2" in result
89+
assert "c: 3" in result

0 commit comments

Comments
 (0)