fix(encode): keep a []any of tables in the value-array form

Assisted-by: GLM 5.3
This commit is contained in:
2026-09-17 23:10:35 +02:00
parent c485aab227
commit d5327568fb
5 changed files with 48 additions and 0 deletions
+30
View File
@@ -597,6 +597,36 @@ func TestMarshalMixedArrayWithInlineTable(t *testing.T) {
}
}
// A []any of tables is what Parse produces for a value array of inline
// tables; it must stay in the value-array form, or the output would re-parse
// as []map[string]any and the round-trip would change the value's type.
func TestMarshalValueArrayOfTablesStaysInline(t *testing.T) {
for _, doc := range []string{
"0=[{}]",
"a = [{x = 1}, {x = 2}]\n",
"b = [{x = 1}, 2, \"three\"]\n",
} {
tree, err := Parse([]byte(doc))
if err != nil {
t.Fatalf("%s: parse: %v", doc, err)
}
out, err := Marshal(tree)
if err != nil {
t.Fatalf("%s: marshal: %v", doc, err)
}
if bytes.HasPrefix(out, []byte("[[")) {
t.Errorf("%s: emitted the [[header]] form for a value array:\n%s", doc, out)
}
re, err := Parse(out)
if err != nil {
t.Fatalf("%s: re-parse: %v\ndoc:\n%s", doc, err, out)
}
if !tomlEqual(tree, re) {
t.Errorf("%s: round-trip changed the tree:\nwas: %#v\nnow: %#v\ndoc:\n%s", doc, tree, re, out)
}
}
}
func TestMarshalNestedInlineTables(t *testing.T) {
tree := map[string]any{
"mix": []any{