Repository navigation
Deliver the whole document to a root Unmarshaler (#994) - #1083
Merged
Merged
Conversation
When the decode target itself implements unstable.Unmarshaler (with the interface enabled), top-level key-values were handed to UnmarshalTOML one value at a time, and a document split into pieces (top-level keys followed by tables) was delivered as disconnected fragments rather than as one document. That made the root unmarshaler unusable for its main purpose: re-parsing the whole document (e.g. to preserve key ordering), which is what issue #994 asked for. PR #996 only delegated unmatched top-level keys individually and, as noted at the time, never handled piece-wise tables. The rewritten decoder already accumulates a whole table (including split and array tables) into a single raw "capture" delivered to UnmarshalTOML once. When the root target implements the interface, open such a capture spanning the entire document up front: top-level key-values then flow through the existing capture branch and any tables attach to it via resumeCapture, so UnmarshalTOML is called exactly once with a self-contained TOML document that re-parses to the original structure. Gated on the opt-in interface, this adds no work to the default decode path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #994.
Problem
When the decode target itself implements
unstable.Unmarshaler(withEnableUnmarshalerInterface()),UnmarshalTOMLwas not receiving the document. Top-level key-values were delegated one value at a time, and a document split into pieces (top-level keys followed by tables) arrived as disconnected fragments instead of a single document:foo = "bar""bar"(just the value)foo = "bar"\ntitle=…then[owner]…This made a root
Unmarshalerunusable for its main purpose — re-parsing the full document, e.g. to preserve key ordering, which is exactly what the issue asked for. PR #996 only delegated unmatched top-level keys individually and, as noted when it was merged, never handled piece-wise tables.Fix
The rewritten decoder already accumulates a whole table (including split and array tables) into a single raw "capture" that is delivered to
UnmarshalTOMLonce. This routes the root through the same machinery: when the root target implements the interface, a capture spanning the entire document is opened up front. Top-level key-values then flow through the existing capture branch, and any tables attach to it viaresumeCapture, soUnmarshalTOMLis called exactly once with a self-contained TOML document that re-parses to the original structure.The change is gated on the opt-in interface, so it adds no work to the default decode path.
Example: preserving key order (the issue's use-case)
Before this change,
cfg.Keyswould have been empty (the unmarshaler was handed only the value1, twice).Tests
TestIssue994_OK, which had baked in the old partial-value result.TestIssue994_WholeDocument(single/multiple keys, keys-then-table, keys-then-array-table, only-table, dotted keys, empty doc): assertsUnmarshalTOMLis called exactly once and that the bytes round-trip back to the same structure as the original input.TestIssue994_ErrorPropagationfor the multi-expression capture path.Full suite passes (
go test ./...).Performance
No regression. Interleaved A/B benchstat on linux/amd64 (go1.26.4, n=12), all benchmarks not statistically significant —
allocs/opbyte-identical:🤖 Generated with Claude Code