Skip to content

Commit 75a4f17

Browse files
committed
Correct encoding of several layers of embedded structs
I'm not entirely sure if I understand what's going on here, but this seems to fix it. Fixes #430
1 parent 702f5a6 commit 75a4f17

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

Diff for: encode.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,6 @@ func (enc *Encoder) eMap(key Key, rv reflect.Value, inline bool) {
440440
}
441441
}
442442

443-
const is32Bit = (32 << (^uint(0) >> 63)) == 32
444-
445443
func pointerTo(t reflect.Type) reflect.Type {
446444
if t.Kind() == reflect.Ptr {
447445
return pointerTo(t.Elem())
@@ -476,15 +474,14 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value, inline bool) {
476474

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

479-
if is32Bit {
480-
// Copy so it works correct on 32bit archs; not clear why this
481-
// is needed. See #314, and https://www.reddit.com/r/golang/comments/pnx8v4
482-
// This also works fine on 64bit, but 32bit archs are somewhat
483-
// rare and this is a wee bit faster.
484-
copyStart := make([]int, len(start))
485-
copy(copyStart, start)
486-
start = copyStart
487-
}
477+
// Need to make a copy because ... ehm, I don't know why... I guess
478+
// allocating a new array can cause it to fail(?)
479+
//
480+
// Done for: https://github.com/BurntSushi/toml/issues/430
481+
// Previously only on 32bit for: https://github.com/BurntSushi/toml/issues/314
482+
copyStart := make([]int, len(start))
483+
copy(copyStart, start)
484+
start = copyStart
488485

489486
// Treat anonymous struct fields with tag names as though they are
490487
// not anonymous, like encoding/json does.

Diff for: encode_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,17 @@ func TestEncodeJSONNumber(t *testing.T) {
836836
}
837837
}
838838

839+
type (
840+
StructA struct{ StructB }
841+
StructB struct{ StructC }
842+
StructC struct{ StructD }
843+
StructD struct {
844+
FieldD1 string
845+
FieldD2 string
846+
FieldD3 string
847+
}
848+
)
849+
839850
func TestEncode(t *testing.T) {
840851
type (
841852
Embedded struct {
@@ -1114,6 +1125,20 @@ ArrayOfMixedSlices = [[1, 2], ["a", "b"]]
11141125
}{struct{ Embedded }{Embedded{1}}},
11151126
wantOutput: "[_struct]\n _int = 1\n",
11161127
},
1128+
"deeply nested embedded struct": { // #430
1129+
input: StructA{
1130+
StructB: StructB{
1131+
StructC: StructC{
1132+
StructD: StructD{
1133+
FieldD1: "V1",
1134+
FieldD2: "V2",
1135+
FieldD3: "V3",
1136+
},
1137+
},
1138+
},
1139+
},
1140+
wantOutput: "FieldD1 = \"V1\"\nFieldD2 = \"V2\"\nFieldD3 = \"V3\"\n",
1141+
},
11171142
"nested embedded *struct": {
11181143
input: struct {
11191144
Struct struct{ *Embedded } `toml:"_struct"`

0 commit comments

Comments
 (0)