-
Notifications
You must be signed in to change notification settings - Fork 0
34 support upsert into empty documents without sentinel workaround #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nathanjmcdougall
merged 10 commits into
main
from
34-support-upsert-into-empty-documents-without-sentinel-workaround
May 21, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
309414a
feat: expose serialize_value() in _core module
nathanjmcdougall 2b3e222
feat: upsert bootstraps root mapping on empty documents
nathanjmcdougall 6f8fa19
feat: add() bootstraps root mapping on empty documents
nathanjmcdougall 45598ca
test: verify sync works on empty documents
nathanjmcdougall ebcaf4e
test: confirm error behaviour on empty documents is preserved
nathanjmcdougall 98b98c3
fix: add serialize_value to _core.pyi __all__
nathanjmcdougall d4ddd43
docs: add spec and plan for empty document mutations
nathanjmcdougall aaeede0
fix: raise clear error for root upsert on empty document
nathanjmcdougall df3818c
docs: update __contains__ docstring for empty document semantics
nathanjmcdougall 23ea425
fix: align serialize_value error message with codebase convention
nathanjmcdougall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Support Mutations on Empty Documents | ||
|
|
||
| **Date:** 2026-05-22 | ||
| **Issue:** #34 | ||
|
|
||
| ## Problem | ||
|
|
||
| Mutation methods that create structure (`upsert`, `add`, `sync`) raise `PatchError` when called on a document with no root data node: | ||
|
|
||
| ```python | ||
| doc = yamltrip.loads("") | ||
| doc.upsert("x", value=1) | ||
| # PatchError: YAML query error: syntax node 'stream' is missing named child 'document' | ||
| ``` | ||
|
|
||
| The underlying yamlpatch `Add` operation requires an existing mapping node at the target route. This forces callers to use a sentinel workaround. | ||
|
|
||
| ## Scope | ||
|
|
||
| "Empty document" means any document with no root data node: | ||
| - Zero-length source (`""`) | ||
| - Whitespace-only source (`" \n"`) | ||
| - Comment-only source (`"# header\n"`) | ||
|
|
||
| Integer keys on empty documents still error — consistent with existing behaviour. Only string keys can bootstrap a root mapping. | ||
|
|
||
| ## Design decisions | ||
|
|
||
| ### Detection | ||
|
|
||
| Check whether the root route resolves to a data node. If it doesn't, the document is "empty" for our purposes. This covers all three cases above uniformly. | ||
|
|
||
| ### New Rust capability: `_core.serialize_value()` | ||
|
|
||
| Expose a function that takes a Python value and returns YAML text via `serde_yaml`. This gives the Python layer a direct serialization path without constructing throwaway documents. Uses the existing `py_to_yaml_value` conversion. | ||
|
|
||
| ### Bootstrap strategy | ||
|
|
||
| When a mutation targets an empty document and needs to create structure: | ||
| 1. Build the nested Python dict representing the full key path + value | ||
| 2. Serialize it to YAML text via `serialize_value` | ||
| 3. Concatenate with existing source content (preserving comments) | ||
| 4. Re-parse into a new Document | ||
|
|
||
| This is the same cost as any other mutation (every patch application ends with a re-parse). | ||
|
|
||
| ### Comment preservation | ||
|
|
||
| When the document is comment-only, the existing source is preserved as a prefix above the new content. | ||
|
|
||
| ### Per-method behaviour on empty documents | ||
|
|
||
| | Method | Behaviour on empty doc | | ||
| |--------|----------------------| | ||
| | `upsert` | Creates root mapping (bootstrap) | | ||
| | `sync` | Delegates to `upsert` (existing path) | | ||
| | `add` | Creates root mapping (bootstrap) | | ||
| | `replace` | `KeyMissingError` (correct — nothing to replace) | | ||
| | `append` / `insert` / `extend_list` | `PatchError` (correct — no sequence) | | ||
| | `remove` | `PatchError` (correct — nothing to remove) | | ||
|
|
||
| ## Expected behaviour | ||
|
|
||
| ```python | ||
| # Basic | ||
| Document("").upsert("x", value=1)["x"] == 1 | ||
|
|
||
| # Nested keys | ||
| Document("").upsert("a", "b", value="hello")["a", "b"] == "hello" | ||
|
|
||
| # Comment preservation | ||
| doc = Document("# header\n").upsert("x", value=1) | ||
| doc.source.startswith("# header\n") # True | ||
|
|
||
| # add() works too | ||
| Document("").add(key="name", value="foo")["name"] == "foo" | ||
|
|
||
| # Integer keys still error | ||
| Document("").upsert(0, value="x") # raises PatchError | ||
|
|
||
| # Complex values | ||
| Document("").upsert("items", value=["a", "b", "c"])["items"] == ["a", "b", "c"] | ||
| ``` | ||
|
|
||
| ## Non-goals | ||
|
|
||
| - Creating root sequences via integer keys | ||
| - Modifying yamlpatch to handle empty documents internally | ||
| - Changing the Rust `apply_patches_impl` flow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.