unmarshal: empty document results in map (#640)

Fixes #602
This commit is contained in:
Thomas Pelletier
2021-10-25 15:55:54 -04:00
committed by GitHub
parent ed02a1f192
commit 64941b99e2
2 changed files with 17 additions and 1 deletions
+7 -1
View File
@@ -174,7 +174,13 @@ func (d *decoder) FromParser(v interface{}) error {
return fmt.Errorf("toml: decoding pointer target cannot be nil")
}
err := d.fromParser(r.Elem())
r = r.Elem()
if r.Kind() == reflect.Interface && r.IsNil() {
newMap := map[string]interface{}{}
r.Set(reflect.ValueOf(newMap))
}
err := d.fromParser(r)
if err == nil {
return d.strict.Error(d.p.data)
}
+10
View File
@@ -1800,6 +1800,16 @@ func TestIssue596(t *testing.T) {
require.Error(t, err)
}
func TestIssue602(t *testing.T) {
var v interface{}
err := toml.Unmarshal([]byte(""), &v)
require.NoError(t, err)
var expected interface{} = map[string]interface{}{}
require.Equal(t, expected, v)
}
//nolint:funlen
func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {