go-toml icon indicating copy to clipboard operation
go-toml copied to clipboard

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

Open Arnie97 opened this issue 3 years ago • 1 comments

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

  • go-toml v2.0.2 (i.e. 216628222f5716163bb96d3651a68feef31b090d)
  • go version go1.18.4 darwin/amd64 (i.e. macOS)

Arnie97 avatar Aug 01 '22 19:08 Arnie97

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

pelletier avatar Aug 11 '22 07:08 pelletier

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!

pelletier avatar Aug 15 '22 20:08 pelletier