pelletier / go-toml

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

go-toml/v2 silently ignores array type mismatches during unmarshal #799

Closed Arnie97 closed 2 years ago

Arnie97 commented 2 years ago

To Reproduce

package main

import (
    "fmt"
    "github.com/pelletier/go-toml/v2"
)

const testTOML = `
# notice the double brackets
[[test]]
answer = 42
`

func main() {
    var s struct {
        // should be []map[string]int
        Test map[string]int `toml:"test"`
    }
    fmt.Printf("err: %+v\n\nret: %+v\n\n",
        toml.Unmarshal([]byte(testTOML), &s), &s)
}

Expected behavior

github.com/pelletier/go-toml v1.9.5 (remove the /v2 suffix from the imports in the example snippet above):

err: (3, 1): Can't convert [answer = 42
]([]*toml.Tree) to trees

ret: &{Test:map[]}

Actual behavior

github.com/pelletier/go-toml/v2 v2.0.2:

err: <nil>

ret: &{Test:map[answer:42]}

Versions

pelletier commented 2 years ago

Definitely looks like a bug. Thanks! I'll take a look.

pelletier commented 2 years ago

This issue should be fixed as of https://github.com/pelletier/go-toml/commit/7baa23f493a82b3f24f4dada5d0b02d17fbde175. With this example you should see an error like:

toml: cannot decode array table into a map[string]int

Feel free to reopen if you're still facing the issue!