pelletier / go-toml

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

Regression with string type as key in v2 #740

Closed oschwald closed 2 years ago

oschwald commented 2 years ago

Describe the bug

A panic using a new string type in v2.

To Reproduce

The following program works as expected with v1 but causes a panic in v2:

package main

import (
    "fmt"
    "log"

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

type (
    Section string
)

func main() {
    var m map[Section]map[string]string

    b := []byte(`
        [Test]
        test = "string"
    `)

    err := toml.Unmarshal(b, &m)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%#v\n", m)
}

Panic:

panic: reflect.Value.MapIndex: value of type string is not assignable to type main.Section

goroutine 1 [running]:
reflect.Value.assignTo({0x4c57c0, 0xc00010c260, 0x458405}, {0x4db97e, 0x16}, 0x4c5340, 0x0)
    /usr/local/go/src/reflect/value.go:2797 +0x2ad
reflect.Value.MapIndex({0x4c8ae0, 0xc00010e180, 0x4}, {0x4c57c0, 0xc00010c260, 0x98})
    /usr/local/go/src/reflect/value.go:1522 +0x7e
github.com/pelletier/go-toml/v2.(*decoder).handleKeyPart(0xc00012e000, {0x1, 0xc00012c0a8}, {0x4c8ae0, 0xc000124018, 0x60}, 0xc000108ce0, 0x4e2318)
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:425 +0x21d
github.com/pelletier/go-toml/v2.(*decoder).handleTablePart(0x4, {0x19, 0xc00012c0a8}, {0x4c8ae0, 0xc000124018, 0xc00012e018})
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:600 +0x4e
github.com/pelletier/go-toml/v2.(*decoder).handleTable(0xc00012e018, {0x1, 0xc00012c0a8}, {0x4c8ae0, 0xc000124018, 0x0})
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:547 +0x93
github.com/pelletier/go-toml/v2.(*decoder).handleRootExpression(0xc00012e000, 0xc00012c070, {0x4c8ae0, 0xc000124018, 0x0})
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:261 +0x134
github.com/pelletier/go-toml/v2.(*decoder).fromParser(0xc00012e000, {0x4c8ae0, 0xc000124018, 0x100010000000009})
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:223 +0xa9
github.com/pelletier/go-toml/v2.(*decoder).FromParser(0xc00012e000, {0x4c2840, 0xc000124018})
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:208 +0x1e7
github.com/pelletier/go-toml/v2.Unmarshal({0xc00012a000, 0x1d, 0x1d}, {0x4c2840, 0xc000124018})
    /home/greg/MaxMind/go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.6/unmarshaler.go:28 +0x137
main.main()
    /tmp/c/main.go:22 +0x86
exit status 2

Expected behavior

The following should be printed:

map[main.Section]map[string]string{"Test":map[string]string{"test":"string"}}

Versions

pelletier commented 2 years ago

Thank you for catching and fixing this!