Skip to content

Commit 8bbca55

Browse files
Yves Baldusarp242
Yves Baldus
authored andcommitted
add a check for uncomparable empty structs
1 parent 17ef72d commit 8bbca55

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

encode.go

+6
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,12 @@ func (enc *Encoder) isEmpty(rv reflect.Value) bool {
657657
if rv.Type().Comparable() {
658658
return reflect.Zero(rv.Type()).Interface() == rv.Interface()
659659
}
660+
for i := 0; i < rv.NumField(); i++ {
661+
if !enc.isEmpty(rv.Field(i)) {
662+
return false
663+
}
664+
}
665+
return true
660666
case reflect.Bool:
661667
return !rv.Bool()
662668
}

encode_test.go

+43-24
Original file line numberDiff line numberDiff line change
@@ -182,33 +182,43 @@ func TestEncodeOmitEmptyStruct(t *testing.T) {
182182
}
183183

184184
func TestEncodeWithOmitEmpty(t *testing.T) {
185+
type compareable struct {
186+
Bool bool `toml:"bool,omitempty"`
187+
}
185188
type uncomparable struct {
186-
Field []string `toml:"Field,omitempty"`
189+
Field []string `toml:"field,omitempty"`
190+
}
191+
type nestedUncomparable struct {
192+
Field uncomparable `toml:"uncomparable,omitempty"`
193+
Bool bool `toml:"bool,omitempty"`
187194
}
188195
type simple struct {
189-
Bool bool `toml:"bool,omitempty"`
190-
String string `toml:"string,omitempty"`
191-
Array [0]byte `toml:"array,omitempty"`
192-
Slice []int `toml:"slice,omitempty"`
193-
Map map[string]string `toml:"map,omitempty"`
194-
Time time.Time `toml:"time,omitempty"`
195-
Uncomparable1 uncomparable `toml:"uncomparable1,omitempty"`
196-
Uncomparable2 uncomparable `toml:"uncomparable2,omitempty"`
196+
Bool bool `toml:"bool,omitempty"`
197+
String string `toml:"string,omitempty"`
198+
Array [0]byte `toml:"array,omitempty"`
199+
Slice []int `toml:"slice,omitempty"`
200+
Map map[string]string `toml:"map,omitempty"`
201+
Time time.Time `toml:"time,omitempty"`
202+
Compareable1 compareable `toml:"compareable1,omitempty"`
203+
Compareable2 compareable `toml:"compareable2,omitempty"`
204+
Uncomparable1 uncomparable `toml:"uncomparable1,omitempty"`
205+
Uncomparable2 uncomparable `toml:"uncomparable2,omitempty"`
206+
NestedUncomparable1 nestedUncomparable `toml:"nesteduncomparable1,omitempty"`
207+
NestedUncomparable2 nestedUncomparable `toml:"nesteduncomparable2,omitempty"`
197208
}
198209

199210
var v simple
200-
encodeExpected(t, "fields with omitempty are omitted when empty", v, `
201-
[uncomparable1]
202-
203-
[uncomparable2]
204-
`, nil)
211+
encodeExpected(t, "fields with omitempty are omitted when empty", v, "", nil)
205212
v = simple{
206-
Bool: true,
207-
String: " ",
208-
Slice: []int{2, 3, 4},
209-
Map: map[string]string{"foo": "bar"},
210-
Time: time.Date(1985, 6, 18, 15, 16, 17, 0, time.UTC),
211-
Uncomparable2: uncomparable{[]string{"XXX"}},
213+
Bool: true,
214+
String: " ",
215+
Slice: []int{2, 3, 4},
216+
Map: map[string]string{"foo": "bar"},
217+
Time: time.Date(1985, 6, 18, 15, 16, 17, 0, time.UTC),
218+
Compareable2: compareable{true},
219+
Uncomparable2: uncomparable{[]string{"XXX"}},
220+
NestedUncomparable1: nestedUncomparable{uncomparable{[]string{"XXX"}}, false},
221+
NestedUncomparable2: nestedUncomparable{uncomparable{}, true},
212222
}
213223
expected := `bool = true
214224
string = " "
@@ -218,10 +228,18 @@ time = 1985-06-18T15:16:17Z
218228
[map]
219229
foo = "bar"
220230
221-
[uncomparable1]
231+
[compareable2]
232+
bool = true
222233
223234
[uncomparable2]
224-
Field = ["XXX"]
235+
field = ["XXX"]
236+
237+
[nesteduncomparable1]
238+
[nesteduncomparable1.uncomparable]
239+
field = ["XXX"]
240+
241+
[nesteduncomparable2]
242+
bool = true
225243
`
226244
encodeExpected(t, "fields with omitempty are not omitted when non-empty",
227245
v, expected, nil)
@@ -643,8 +661,9 @@ func TestEncodeEmpty(t *testing.T) {
643661
}
644662

645663
// Would previously fail on 32bit architectures; can test with:
646-
// GOARCH=386 go test -c && ./toml.test
647-
// GOARCH=arm GOARM=7 go test -c && qemu-arm ./toml.test
664+
//
665+
// GOARCH=386 go test -c && ./toml.test
666+
// GOARCH=arm GOARM=7 go test -c && qemu-arm ./toml.test
648667
func TestEncode32bit(t *testing.T) {
649668
type Inner struct {
650669
A, B, C string

0 commit comments

Comments
 (0)