Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unmarshaling of nested non-exported struct #917

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,9 @@ func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node

f := fieldByIndex(v, path)

if !f.CanSet() {
// If the field is not settable, need to take a slower path and make a copy of
// the struct itself to a new location.
if !f.CanAddr() {
// If the field is not addressable, need to take a slower path and
// make a copy of the struct itself to a new location.
nvp := reflect.New(v.Type())
nvp.Elem().Set(v)
v = nvp.Elem()
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