[unstable] Support Unmarshaler interface for tables and array tables#1027
Merged
[unstable] Support Unmarshaler interface for tables and array tables#1027
Conversation
Owner
Author
|
@TomWright I think you needed this feature for Dasel to perform custom deserialization. If that's still relevant to you (I think it's been years!) would you mind trying it out see if it solves your issue? |
|
Hey @pelletier , That's correct but it was a bit of a blocker for me so I ended up using the I would be interested in cleaning up that code but will have trouble getting around to it in the short term. I appreciate your work here! |
Extend the unstable.Unmarshaler interface support to work with tables and array tables, not just single values. When a type implementing unstable.Unmarshaler is the target of a table (e.g., [table] or [[array]]), the UnmarshalTOML method receives a synthetic InlineTable node containing all the key-value pairs belonging to that table. Key changes: - Add handleKeyValuesUnmarshaler to collect and process table content - Add copyExpressionNodes to deep-copy AST nodes for synthetic tables - Add helper functions in unstable/ast.go for node manipulation - Update documentation for EnableUnmarshalerInterface - Add comprehensive tests for table and array table unmarshaling
This change brings back support for the unstable.Unmarshaler interface for tables and array tables, addressing issue #873. Key changes: - Changed UnmarshalTOML signature from (*Node) to ([]byte) to provide raw TOML bytes instead of AST nodes - Added RawMessage type (similar to json.RawMessage) for capturing raw TOML bytes for later processing - Updated handleKeyValuesUnmarshaler to reconstruct key-value lines from the parsed keys and raw value bytes - Added support for slice types implementing Unmarshaler (e.g., RawMessage) - Removed unused AST helper functions from unstable/ast.go The bytes-based interface allows users to: - Get raw TOML bytes for custom parsing - Delay TOML decoding using RawMessage - Implement custom unmarshaling logic for complex types Tests added for: - Table unmarshaler with various scenarios - Array table unmarshaler - Split tables (same parent defined in multiple places) - RawMessage usage - Nested tables and mixed regular fields
- Apply De Morgan's law in keyNeedsQuoting to satisfy staticcheck QF1001 - Remove unused splitTableUnmarshaler type from test - Fix unused parameter lint warning in errorUnmarshaler873 - Add test for quoted keys that need special handling - Add test for error propagation from UnmarshalTOML - Update customTable873 parser to handle quoted keys properly Coverage improved: - handleKeyValuesUnmarshaler: 80.0% -> 93.3% - keyNeedsQuoting: 66.7% -> 83.3% - Overall main package: 97.2% -> 97.5%
Add TestIssue873_DottedKeys to test dotted key handling (e.g., sub.key = value) in the Unmarshaler interface. This improves coverage for handleKeyValuesUnmarshaler from 93.3% to 96.7%.
Add TestIssue873_DoublePointerUnmarshaler to test pointer-to-pointer to Unmarshaler types. This covers the pointer dereferencing loop in handleKeyValues, bringing its coverage from 88% to 100%. Total coverage: 97.4%
Add two godoc Example tests: - ExampleDecoder_EnableUnmarshalerInterface_dynamicConfig: shows dynamic unmarshaling based on a type field - ExampleDecoder_EnableUnmarshalerInterface_rawMessage: demonstrates RawMessage usage for deferred parsing Fix handleKeyValuesUnmarshaler to handle values where Raw.Length == 0 (like boolean types) by using value.Data as fallback.
Instead of reconstructing key-value lines from parsed components, now uses the original raw bytes from the document. This preserves: - Whitespace around '=' (e.g., "key = value") - String quoting style (basic vs literal) - Number formats (hex, octal, binary) - Inline table formatting Changes: - Add Raw range tracking to KeyValue expressions in parseKeyval - Update handleKeyValuesUnmarshaler to use expr.Raw directly - Remove keyNeedsQuoting helper (no longer needed) - Add TestIssue873_FormattingPreservation test - Update expected output in ExampleParser_comments
4e71477 to
56b531f
Compare
Add fail-fast: false to the test workflow strategy so that all OS/Go version combinations continue running even if one fails. This provides better visibility into which specific combinations have issues.
Owner
Author
|
I'm going to merge this because I think it doesn't break your existing implementation. So it will be available in the next release and you can experiment with it whenever is convenient! |
Maks1mS
pushed a commit
to stplr-dev/stplr
that referenced
this pull request
Mar 25, 2026
…374) This PR contains the following updates: | Package | Type | Update | Change | OpenSSF | |---|---|---|---|---| | [github.com/pelletier/go-toml/v2](https://github.com/pelletier/go-toml) | require | minor | `v2.2.4` → `v2.3.0` | [](https://securityscorecards.dev/viewer/?uri=github.com/pelletier/go-toml) | --- >⚠️ **Warning** > > Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/23) for more information. --- ### Release Notes <details> <summary>pelletier/go-toml (github.com/pelletier/go-toml/v2)</summary> ### [`v2.3.0`](https://github.com/pelletier/go-toml/releases/tag/v2.3.0) [Compare Source](pelletier/go-toml@v2.2.4...v2.3.0) This is the first release built largely with the help of AI coding agents. Highlights include the complete removal of the unsafe package. go-toml is now fully safe Go code, with a geomean overhead of only \~1.4% vs v2.2.4 and zero additional allocations on benchmarks. This release also adds omitzero struct tag support, improves UnmarshalText/Unmarshaler handling for tables and array tables, and fixes several bugs including nil pointer marshaling, leap second handling, and datetime unmarshaling panics. <!-- Release notes generated using configuration in .github/release.yml at v2.3.0 --> #### What's Changed ##### What's new - marshal: don't escape quotes unnecessarily by [@​virtuald](https://github.com/virtuald) in [#​991](pelletier/go-toml#991) - Add `omitzero` tag support by [@​NathanBaulch](https://github.com/NathanBaulch) in [#​998](pelletier/go-toml#998) - Support custom IsZero() methods with omitzero tag by [@​pelletier](https://github.com/pelletier) in [#​1020](pelletier/go-toml#1020) - UnmarshalText fallbacks to struct unmarshaling for tables and arrays by [@​pelletier](https://github.com/pelletier) in [#​1026](pelletier/go-toml#1026) - \[unstable] Support Unmarshaler interface for tables and array tables by [@​pelletier](https://github.com/pelletier) in [#​1027](pelletier/go-toml#1027) ##### Fixed bugs - Add missing UnmarshalTOML call by [@​pelletier](https://github.com/pelletier) in [#​996](pelletier/go-toml#996) - Handle array table into an empty slice by [@​pelletier](https://github.com/pelletier) in [#​997](pelletier/go-toml#997) - Unwrap strict errors by [@​bersace](https://github.com/bersace) in [#​1012](pelletier/go-toml#1012) - Fix leap second handling found by fuzz by [@​pelletier](https://github.com/pelletier) in [#​1019](pelletier/go-toml#1019) - Fix nil pointer map values not being marshaled by [@​pelletier](https://github.com/pelletier) in [#​1025](pelletier/go-toml#1025) - Fix panic when unmarshaling datetime values to incompatible types ([#​1028](pelletier/go-toml#1028)) by [@​pelletier](https://github.com/pelletier) in [#​1029](pelletier/go-toml#1029) - Fix parser error pointing to wrong line at EOF without trailing newline by [@​pelletier](https://github.com/pelletier) in [#​1041](pelletier/go-toml#1041) ##### Documentation - Improve Unmarshaling README by [@​heckelson](https://github.com/heckelson) in [#​1016](pelletier/go-toml#1016) - Create AGENTS.md guidelines file by [@​pelletier](https://github.com/pelletier) in [#​1017](pelletier/go-toml#1017) ##### Other changes - Unsafe package removal by [@​pelletier](https://github.com/pelletier) in [#​1021](pelletier/go-toml#1021) - Bump CI and test scripts to Go 1.26 by [@​pelletier](https://github.com/pelletier) in [#​1030](pelletier/go-toml#1030) #### New Contributors - [@​virtuald](https://github.com/virtuald) made their first contribution in [#​991](pelletier/go-toml#991) - [@​NathanBaulch](https://github.com/NathanBaulch) made their first contribution in [#​999](pelletier/go-toml#999) - [@​bersace](https://github.com/bersace) made their first contribution in [#​1012](pelletier/go-toml#1012) - [@​flyn-org](https://github.com/flyn-org) made their first contribution in [#​1013](pelletier/go-toml#1013) - [@​heckelson](https://github.com/heckelson) made their first contribution in [#​1016](pelletier/go-toml#1016) **Full Changelog**: <pelletier/go-toml@v2.2.4...v2.3.0> </details> --- ### Configuration 📅 **Schedule**: Branch creation - At 12:00 AM through 04:59 AM and 10:00 PM through 11:59 PM, Monday through Friday ( * 0-4,22-23 * * 1-5 ), Only on Sunday and Saturday ( * * * * 0,6 ) (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44Ni4xIiwidXBkYXRlZEluVmVyIjoiNDMuODYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiS2luZC9EZXBlbmRlbmNpZXMiXX0=--> Reviewed-on: https://altlinux.space/stapler/stplr/pulls/374 Co-authored-by: Renovate Bot <stapler-helper-bot@noreply.altlinux.space> Co-committed-by: Renovate Bot <stapler-helper-bot@noreply.altlinux.space>
6 tasks
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.
Extend the unstable.Unmarshaler interface support to work with tables
and array tables, not just single values.
When a type implementing unstable.Unmarshaler is the target of a table
(e.g., [table] or [[array]]), the UnmarshalTOML method receives a
synthetic InlineTable node containing all the key-value pairs belonging
to that table.
Key changes: