pelletier / go-toml

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

beta7: Encode: panic: reflect: NumField of non-struct type #752

Closed bep closed 2 years ago

bep commented 2 years ago

Got some tests errors in Hugo. Note that the failures comes from some usage of Encode in some old test code that in itself looks suspicious and needs fixing. But I suspect that the error situation is a real one and needs a well defined behaviour.

Note that config.Provider is an interface (https://godocs.io/github.com/gohugoio/hugo/config#Provider) implemented by a plain struct.

  panic: reflect: NumField of non-struct type config.Provider [recovered]
    panic: reflect: NumField of non-struct type config.Provider

goroutine 50 [running]:
testing.tRunner.func1.2({0x10596b640, 0x1400028e8f0})
    /usr/local/go/src/testing/testing.go:1389 +0x1c8
testing.tRunner.func1()
    /usr/local/go/src/testing/testing.go:1392 +0x384
panic({0x10596b640, 0x1400028e8f0})
    /usr/local/go/src/runtime/panic.go:838 +0x204
reflect.(*rtype).NumField(0x105a8c620?)
    /usr/local/go/src/reflect/type.go:1026 +0x70
github.com/pelletier/go-toml/v2.walkStruct({{0x1400028e8c0, 0x1, 0x1}, {0x0, 0x0}, 0x0, 0x0, 0x1, 0x0, 0x0, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:579 +0xa0
github.com/pelletier/go-toml/v2.walkStruct({{0x1400028e8c0, 0x1, 0x1}, {0x0, 0x0}, 0x0, 0x0, 0x1, 0x0, 0x0, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:603 +0x21c
github.com/pelletier/go-toml/v2.(*Encoder).encodeStruct(0x14000253290?, {0x14000745800, 0x6d3, 0x800}, {{0x1400028e8c0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:631 +0xa8
github.com/pelletier/go-toml/v2.(*Encoder).encode(0x14000768f88?, {0x14000745800, 0x6d3, 0x800}, {{0x1400028e8c0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:243 +0x814
github.com/pelletier/go-toml/v2.(*Encoder).encode(0x105af8780?, {0x14000745800, 0x6d3, 0x800}, {{0x1400028e8c0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:257 +0x6a0
github.com/pelletier/go-toml/v2.(*Encoder).encodeSliceAsArrayTable(0x1058d0000?, {0x14000745800, 0x6bf, 0x800}, {{0x1400028e8c0, 0x1, 0x1}, {0x0, 0x0}, 0x0, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:870 +0x2f0
github.com/pelletier/go-toml/v2.(*Encoder).encodeSlice(0x140004f7fe0?, {0x14000745800, 0x6bf, 0x800}, {{0x0, 0x0, 0x0}, {0x1400053be20, 0xf}, 0x1, ...}, ...)
    /Users/bep/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.7/marshaler.go:838 +0x150

In beta6, this is what:

[[languagessorted]]
Lang = 'en'
LanguageName = ''
LanguageDirection = ''
Title = ''
Weight = 0
Disabled = false
ContentDir = ''
[languagessorted.Cfg]
[languagessorted.LocalCfg]
[languagessorted.Provider]
pelletier commented 2 years ago

Thanks a lot for trying it out! I think this patch should fix the regression: https://github.com/pelletier/go-toml/commit/2377ac4bc04c4d1fff969c02c1ec02bde202166b (incorrectly trying to walk an embedded interface as if it was a struct).

bep commented 2 years ago

Yes, the tests passes with 2377ac4bc04c4d1fff969c02c1ec02bde202166b