Skip to content

Commit

Permalink
unmarshal: empty document results in map
Browse files Browse the repository at this point in the history
Fixes #602
  • Loading branch information
pelletier committed Oct 25, 2021
1 parent ed02a1f commit 22848b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 22848b4

Please sign in to comment.