unmarshal: don't panic when storing table in slice (#641)

New error message:

```
toml: cannot store a table in a slice
1| [things]
 |  ~~~~~~ cannot store a table in a slice
2| foo = "bar"
```

Fixes #623
This commit is contained in:
Thomas Pelletier
2021-10-25 16:47:10 -04:00
committed by GitHub
parent 64941b99e2
commit d0d001625c
2 changed files with 15 additions and 0 deletions
+3
View File
@@ -488,6 +488,9 @@ func (d *decoder) handleArrayTablePart(key ast.Iterator, v reflect.Value) (refle
// cannot handle it.
func (d *decoder) handleTable(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
if v.Kind() == reflect.Slice {
if v.Len() == 0 {
return reflect.Value{}, newDecodeError(key.Node().Data, "cannot store a table in a slice")
}
elem := v.Index(v.Len() - 1)
x, err := d.handleTable(key, elem)
if err != nil {