pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.67k stars 208 forks source link

Slices of structs are not properly reset when unmarshalling #931

Closed kkoreilly closed 4 months ago

kkoreilly commented 5 months ago

Describe the bug When unmarshalling a slice of structs (array of tables in TOML terminology) into a Go slice, any elements already present in the slice are not reset, leading to compounding slices. This did not happen in v1.

To Reproduce

main.go:

package main

import (
    "fmt"
    "os"

    "github.com/pelletier/go-toml/v2"
)

type item struct {
    Name string
}

type items struct {
    Slice []item
}

func main() {
    its := items{[]item{{"a"}, {"b"}}}
    b, _ := os.ReadFile("slice.toml")
    toml.Unmarshal(b, &its)
    fmt.Println(its) // should be {[{c} {d}]}, but get {[{a} {b} {c} {d}]}
}

slice.toml:

[[Slice]]
  Name = 'c'

[[Slice]]
  Name = 'd'

Expected behavior As stated in the code, I expected {[{c} {d}]} but got {[{a} {b} {c} {d}]}.

Versions

Additional context As stated above, I have confirmed that this issue did not happen in v1.

pelletier commented 4 months ago

Good catch, thank you for the bug report!

The intent is to match encoding/json's behavior when unmarshaling. I've confirmed (https://go.dev/play/p/WQ8VPf5C-L1) it resets the slice and the resulting value is {[{c} {d}]}.

pelletier commented 4 months ago

@kkoreilly I've merged #934, which should address your issue. Let me know if it worked, feel free to re-open this issue if not!

kkoreilly commented 4 months ago

It worked, thank you!