Skip to content

Commit

Permalink
Merge pull request #374 from saracen/fix-32bit-anonymous-struct-fields
Browse files Browse the repository at this point in the history
Fix encoding anonymous struct with multiple fields on 32-bit systems
  • Loading branch information
arp242 committed Jan 12, 2023
2 parents 7801cfb + 022bf7e commit 08a2344
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
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

0 comments on commit 08a2344

Please sign in to comment.