Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 15571ea

Browse files
committed
Fixed omitempty for non-comparable structs.
Problem reported by Andrew Bonventre.
1 parent 3ed4873 commit 15571ea

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

bson/bson_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ type condTime struct {
957957
V time.Time ",omitempty"
958958
}
959959
type condStruct struct {
960-
V struct { A int } ",omitempty"
960+
V struct { A []int } ",omitempty"
961961
}
962962

963963
type shortInt struct {
@@ -1157,8 +1157,8 @@ var twoWayCrossItems = []crossTypeItem{
11571157
{&condTime{time.Unix(123456789, 123e6)}, map[string]time.Time{"v": time.Unix(123456789, 123e6)}},
11581158
{&condTime{}, map[string]string{}},
11591159

1160-
{&condStruct{struct{A int}{1}}, bson.M{"v": bson.M{"a": 1}}},
1161-
{&condStruct{struct{A int}{}}, bson.M{}},
1160+
{&condStruct{struct{A []int}{[]int{1}}}, bson.M{"v": bson.M{"a": []interface{}{1}}}},
1161+
{&condStruct{struct{A []int}{}}, bson.M{}},
11621162

11631163
{&namedCondStr{"yo"}, map[string]string{"myv": "yo"}},
11641164
{&namedCondStr{}, map[string]string{}},

bson/encode.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,12 @@ func isZero(v reflect.Value) bool {
179179
if v.Type() == typeTime {
180180
return v.Interface().(time.Time).IsZero()
181181
}
182-
info, err := getStructInfo(v.Type())
183-
if err != nil {
184-
panic("should never happen here: " + err.Error())
182+
for i := v.NumField()-1; i >= 0; i-- {
183+
if !isZero(v.Field(i)) {
184+
return false
185+
}
185186
}
186-
return info.Zero.Interface() == v.Interface()
187+
return true
187188
}
188189
return false
189190
}

0 commit comments

Comments
 (0)