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 encoding anonymous struct with multiple fields on 32-bit systems #374

Merged
merged 1 commit into from
Jan 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {

frv := eindirect(rv.Field(i))

if is32Bit {
// Copy so it works correct on 32bit archs; not clear why this
// is needed. See #314, and https://www.reddit.com/r/golang/comments/pnx8v4
// This also works fine on 64bit, but 32bit archs are somewhat
// rare and this is a wee bit faster.
copyStart := make([]int, len(start))
copy(copyStart, start)
start = copyStart
}

// Treat anonymous struct fields with tag names as though they are
// not anonymous, like encoding/json does.
//
Expand All @@ -471,17 +481,7 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
if typeIsTable(tomlTypeOfGo(frv)) {
fieldsSub = append(fieldsSub, append(start, f.Index...))
} else {
// Copy so it works correct on 32bit archs; not clear why this
// is needed. See #314, and https://www.reddit.com/r/golang/comments/pnx8v4
// This also works fine on 64bit, but 32bit archs are somewhat
// rare and this is a wee bit faster.
if is32Bit {
copyStart := make([]int, len(start))
copy(copyStart, start)
fieldsDirect = append(fieldsDirect, append(copyStart, f.Index...))
} else {
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
}
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ func TestEncodeOmitemptyEmptyName(t *testing.T) {
func TestEncodeAnonymousStruct(t *testing.T) {
type Inner struct{ N int }
type inner struct{ B int }
type Embedded struct {
Inner1 Inner
Inner2 Inner
}
type Outer0 struct {
Inner
inner
Expand All @@ -352,6 +356,9 @@ func TestEncodeAnonymousStruct(t *testing.T) {
Inner `toml:"inner"`
inner `toml:"innerb"`
}
type Outer3 struct {
Embedded
}

v0 := Outer0{Inner{3}, inner{4}}
expected := "N = 3\nB = 4\n"
Expand All @@ -360,6 +367,10 @@ func TestEncodeAnonymousStruct(t *testing.T) {
v1 := Outer1{Inner{3}, inner{4}}
expected = "[inner]\n N = 3\n\n[innerb]\n B = 4\n"
encodeExpected(t, "embedded anonymous tagged struct", v1, expected, nil)

v3 := Outer3{Embedded: Embedded{Inner{3}, Inner{4}}}
expected = "[Inner1]\n N = 3\n\n[Inner2]\n N = 4\n"
encodeExpected(t, "embedded anonymous multiple fields", v3, expected, nil)
}

func TestEncodeAnonymousStructPointerField(t *testing.T) {
Expand Down