feat: decode arrays into [N]T and add MarshalAppend
Test / test (push) Successful in 2m31s

Assisted-by: GLM 5.3 Flash
This commit is contained in:
2026-09-21 23:58:23 +02:00
parent 10d49fbe60
commit 2e5dfc54c9
6 changed files with 140 additions and 18 deletions
+43
View File
@@ -1273,3 +1273,46 @@ func TestSourceLineEdgePositions(t *testing.T) {
t.Errorf("SourceLine(offset past end) =\n%q\nwant:\n%q", got, want)
}
}
func TestDecodeFixedArray(t *testing.T) {
t.Run("value array", func(t *testing.T) {
var cfg struct {
Ports [2]int `toml:"ports"`
Label [2]string `toml:"label"`
Grid [2][2]int64 `toml:"grid"`
}
in := []byte("ports = [8080, 9090]\nlabel = [\"a\", \"b\"]\ngrid = [[1, 2], [3, 4]]\n")
if err := Unmarshal(in, &cfg); err != nil {
t.Fatal(err)
}
if cfg.Ports != [2]int{8080, 9090} || cfg.Label != [2]string{"a", "b"} || cfg.Grid != [2][2]int64{{1, 2}, {3, 4}} {
t.Errorf("decoded %+v", cfg)
}
})
t.Run("array of tables", func(t *testing.T) {
type Item struct {
Name string `toml:"name"`
Qty int `toml:"qty"`
}
var cfg struct {
Items [2]Item `toml:"items"`
}
in := []byte("[[items]]\nname = \"a\"\nqty = 1\n[[items]]\nname = \"b\"\nqty = 2\n")
if err := Unmarshal(in, &cfg); err != nil {
t.Fatal(err)
}
if cfg.Items != [2]Item{{"a", 1}, {"b", 2}} {
t.Errorf("decoded %+v", cfg)
}
})
t.Run("a length mismatch is an error", func(t *testing.T) {
var cfg struct {
Ports [3]int `toml:"ports"`
}
err := Unmarshal([]byte("ports = [8080, 9090]\n"), &cfg)
want := "ports: interpres: cannot assign 2 elements to [3]int"
if err == nil || err.Error() != want {
t.Errorf("err = %v, want %q", err, want)
}
})
}