Skip to content

Commit 34765b4

Browse files
authored
Fix unmarshaling of nested non-exported struct (#917)
Fixes #915
1 parent 358c8d2 commit 34765b4

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Diff for: unmarshaler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,9 @@ func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node
10971097

10981098
f := fieldByIndex(v, path)
10991099

1100-
if !f.CanSet() {
1101-
// If the field is not settable, need to take a slower path and make a copy of
1102-
// the struct itself to a new location.
1100+
if !f.CanAddr() {
1101+
// If the field is not addressable, need to take a slower path and
1102+
// make a copy of the struct itself to a new location.
11031103
nvp := reflect.New(v.Type())
11041104
nvp.Elem().Set(v)
11051105
v = nvp.Elem()

Diff for: unmarshaler_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,28 @@ res = [
28012801
}
28022802
}
28032803

2804+
func TestIssue915(t *testing.T) {
2805+
type blah struct {
2806+
A string `toml:"a"`
2807+
}
2808+
2809+
type config struct {
2810+
Fizz string `toml:"fizz"`
2811+
blah `toml:"blah"`
2812+
}
2813+
2814+
b := []byte(`
2815+
fizz = "abc"
2816+
blah.a = "def"`)
2817+
var cfg config
2818+
err := toml.Unmarshal(b, &cfg)
2819+
require.NoError(t, err)
2820+
2821+
require.Equal(t, "abc", cfg.Fizz)
2822+
require.Equal(t, "def", cfg.blah.A)
2823+
require.Equal(t, "def", cfg.A)
2824+
}
2825+
28042826
func TestUnmarshalDecodeErrors(t *testing.T) {
28052827
examples := []struct {
28062828
desc string

0 commit comments

Comments
 (0)