TOML encoder: prefer readable table sections over inline tables#2649
TOML encoder: prefer readable table sections over inline tables#2649
Conversation
When converting from YAML/JSON to TOML, the encoder now always uses
readable TOML table section syntax ([section]) instead of compact inline
hash table syntax (key = { ... }), which better matches TOML's goal as
a human-focused configuration format.
Changes:
- decoder_toml.go: Mark inline TOML tables with FlowStyle so round-trips
can be distinguished from YAML flow mappings
- encoder_toml.go:
- encodeTopLevelEntry: use FlowStyle check instead of EncodeSeparate to
decide inline vs table section (all block mappings now become tables)
- encodeSeparateMapping: count FlowStyle children as attributes; use
recursive encodeSeparateMapping for nested non-flow mappings
- encodeMappingBodyWithPath: emit non-flow child mappings as sub-table
sections instead of inline tables
- toml_test.go: add encode (YAML→TOML) test scenarios, update roundtrip
expectations for inline tables (now expanded to table sections)
Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/4824a219-6d5e-42e7-bca1-a8a277bf8c6a
Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
pkg/yqlib/toml_test.go
Outdated
| input: rtInlineTableAttr, | ||
| expression: ".", | ||
| expected: rtInlineTableAttr, | ||
| expected: rtInlineTableAttrEncoded, |
There was a problem hiding this comment.
@copilot the point of the roundtrip tests is to maintain the format as much as possible...this no longer does that.
There was a problem hiding this comment.
Fixed in d1ffec1. The root cause was that FlowStyle (used in the previous commit to mark inline tables) also affects the YAML encoder, which was changing the TOML→YAML decode output.
The fix introduces a new TomlInline bool field on CandidateNode — TOML-specific and invisible to the YAML encoder. The TOML decoder sets it for inline tables, UpdateAttributesFrom propagates it through DeeplyAssign merges, and the TOML encoder checks it to decide inline vs table-section output.
TOML roundtrip tests are restored to their original expected values: inline tables stay inline, table sections stay as table sections.
…rve inline tables FlowStyle affected YAML decode output (causing inline tables to appear as YAML flow mappings). Replace it with a new TOML-specific TomlInline bool on CandidateNode that: - Is set by the TOML decoder for inline tables (not FlowStyle) - Is copied by UpdateAttributesFrom so it survives DeeplyAssign merges - Is checked by the TOML encoder alongside FlowStyle (for YAML flow maps) - Has no effect on the YAML encoder, preserving existing TOML→YAML output TOML roundtrip tests are restored to their original expected values (inline tables stay inline, table sections stay as sections). Agent-Logs-Url: https://github.com/mikefarah/yq/sessions/f59bdf62-6d16-4664-991b-38eb87c9d81c Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
When encoding to TOML from YAML/JSON, nested mappings were collapsed into compact inline table syntax (
key = { ... }) instead of idiomatic TOML table sections ([key]). This affected both direct YAML→TOML conversion and round-trips through YAML.Changes
Encoder (
encoder_toml.go)encodeTopLevelEntry: CheckTomlInline/FlowStyle— only nodes marked as inline stay inline; all block mappings become table sectionsencodeSeparateMapping: CountTomlInline/FlowStylemapping children ashasAttrs(they render askey = { ... }); use recursiveencodeSeparateMappingin the "no attrs" loopencodeMappingBodyWithPath: Emit non-inline child mappings as sub-table sections ([parent.child]) instead of inline tablesDecoder (
decoder_toml.go)TomlInline: trueon decoded TOML inline tables, distinguishing them from block mappings so they survive re-encoding as inlineCore (
candidate_node.go)TomlInline boolfield toCandidateNode— a TOML-specific marker invisible to the YAML encoder, copied byUpdateAttributesFromso it survivesDeeplyAssigndeep-mergesTests (
toml_test.go)encodescenario type (YAML→TOML) and coverage for table section outputBefore / After
YAML flow mappings (
arg: {hello: foo}) and TOML inline tables are preserved as TOML inline tables, respecting the author's explicit style intent. TOML table sections and YAML block mappings both produce readable[section]headers.