Skip to content

Commit

Permalink
Fix unmarshaling of nested non-exported struct
Browse files Browse the repository at this point in the history
Fixes #915
  • Loading branch information
pelletier committed Dec 11, 2023
1 parent 358c8d2 commit fca11a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node

f := fieldByIndex(v, path)

if !f.CanSet() {
if !f.CanAddr() {
// If the field is not settable, need to take a slower path and make a copy of
// the struct itself to a new location.
nvp := reflect.New(v.Type())
Expand Down
22 changes: 22 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,28 @@ res = [
}
}

func TestIssue915(t *testing.T) {
type blah struct {
A string `toml:"a"`
}

type config struct {
Fizz string `toml:"fizz"`
blah `toml:"blah"`
}

b := []byte(`
fizz = "abc"
blah.a = "def"`)
var cfg config
err := toml.Unmarshal(b, &cfg)
require.NoError(t, err)

require.Equal(t, "abc", cfg.Fizz)
require.Equal(t, "def", cfg.blah.A)
require.Equal(t, "def", cfg.A)
}

func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {
desc string
Expand Down

0 comments on commit fca11a6

Please sign in to comment.