go-toml
go-toml copied to clipboard
toml: cannot decode TOML array table into struct field
Describe the bug When using version 2.0.0, the following structure can be parsed as a map. When using version 2.1.0, it will be judged as an array structure, resulting in a parsing error.
To Reproduce Steps to reproduce the behavior. Including TOML files.
package main
import (
"fmt"
tomlv2 "github.com/pelletier/go-toml/v2"
)
type Config struct {
A A `toml:"A" yaml:"A"`
}
type A map[string]struct {
C int `toml:"C"`
D int `toml:"D"` // 走老接口
Body1 Body `toml:"E"`
Body2 Body `toml:"G"`
}
// WhiteList 白名单配置
type Body struct {
F string `toml:"F"`
}
var data2 = `
[A]
[[A.B]]
C = 100
[[A.B]]
D = 0
[[A.B.E]]
F = "123215320,10286292"
[[A.B.G]]
F = ""
`
func main() {
newConf := Config{}
err := tomlv2.Unmarshal([]byte(data2), &newConf)
fmt.Println(newConf, err)
}
Expected behavior A clear and concise description of what you expected to happen, if other than "should work".
Versions
- go-toml: 2.1.0 (git sha)
- go: go1.21
- operating system: Linux
Additional context Add any other context about the problem here that you think may help to diagnose.
Bisected. This behavior changed in https://github.com/pelletier/go-toml/commit/7baa23f493a82b3f24f4dada5d0b02d17fbde175.
Unless I've missed something, this error is normal (and what was corrected in the fix @moorereason highlighted). The notation [[...]]
in TOML indicates an array of table. It defines a new table in a list of table. As a result, the containing key must be a slice or an array.
The version before https://github.com/pelletier/go-toml/commit/7baa23f493a82b3f24f4dada5d0b02d17fbde175 was erroneously accepting input that didn't match that structure.
I've modified the A
type in the example to match the provided data: https://go.dev/play/p/KywHmXIjURW
Closing this as it's been over a month and the issue seems understood. Feel free to re-open if there is still a problem!