Skip to content

Write nested lists correctly (#5) - #12

Merged
JosiahParry merged 1 commit into
mainfrom
fix/nested-list-write
Jul 31, 2026
Merged

Write nested lists correctly (#5)#12
JosiahParry merged 1 commit into
mainfrom
fix/nested-list-write

Conversation

@CGMossa

@CGMossa CGMossa commented Jul 31, 2026

Copy link
Copy Markdown
Member

TODO: replace this line with your own framing, in your own voice.

AI-written details

Summary

  • as_item() chose between an array and a table from whether the key was named rather than from the value list's own names, so any unnamed list passed as a value took the table branch. as_kv_pairs() then inserted every element under the literal key NA, keeping only the last one: as_toml(list(x = list(1, 2, 3))) produced [x] / NA = 3.0. It is now derived from the value's names, mirroring what as_value() already does.
  • A list of named lists is written as an array of tables, so an item read with get_item() can be inserted back with insert_items(). This addresses the second half of handling nested list types #5 ("there isn't a clear way to massage this output as something that can be easily inserted back in as an array of tables").
  • as_kv_pairs() errors on a non-empty unnamed list instead of emitting NA keys. Unreachable through the public API after the change above, kept as a guard.
  • Dropped a set_prefix("\n") decor call on the array branch that was effectively unreachable before and rendered x = followed by a broken multi-line array.
  • New tests/testthat/test-nested-lists.R, plus a NEWS entry. Reading these documents already worked after 190531f; the tests cover the read shape too so handling nested list types #5 stays covered end to end.

Test plan

  • R CMD check: clean (the only NOTE is a .git entry from building inside a git worktree)
  • 20 assertions pass, including the pre-existing formatting snapshots
  • The rv.lock v2 snippet from handling nested list types #5 round-trips: get_item() then insert_items() then reparse returns the same data, with the generated-file comments and untouched keys preserved
  • The gsm.app example from the issue thread round-trips identical()-equal
  • Unchanged: named lists to tables, data.frames to arrays of tables, df_as_array = FALSE, partially-named lists still error

Notes

  • Behaviour change: a document holding an inline array of tables (tbls = [{ a = 1 }, { a = 2 }]) is rewritten as [[tbls]] blocks when that value is re-inserted. The data is equal and on main the same operation produced [tbls] / NA = { a = 2 }, so this is strictly better, but it is a formatting change in a package that advertises formatting preservation. Making the array-of-tables branch unconditional was chosen over gating it on df_as_array, whose documented meaning does not extend to lists.
  • Two round-trip gaps are left alone, both pre-existing and both read-side representation questions rather than bugs in this path. dependencies = [] reads as NULL, so the key disappears when written back. dependencies = ["lattice"] reads as "lattice" and writes back as a scalar string rather than a one-element array, which is a type change a consumer like rv would reject. Fixing either means changing what get_item() and from_toml() return for length-0 and length-1 arrays for every user. Worth deciding separately, and until then editing a real rv.lock and writing it back is still not safe.

Drafted by Claude (claude-opus-5). Reviewed by the author.

`as_item()` decided between an array and a table based on whether the
*key* was named, not whether the value list itself was named. Any
unnamed list passed as a value therefore took the table branch, where
`as_kv_pairs()` inserted every element under the literal key `NA`,
keeping only the last one.

Derive it from the value's own names instead, mirroring `as_value()`,
and turn a list of named lists into an array of tables so an item read
with `get_item()` can be written back with `insert_items()`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@JosiahParry
JosiahParry merged commit 08e8283 into main Jul 31, 2026
7 checks passed
@CGMossa
CGMossa deleted the fix/nested-list-write branch July 31, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants