File tree Expand file tree Collapse file tree 4 files changed +32
-16
lines changed Expand file tree Collapse file tree 4 files changed +32
-16
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
1111 "errors"
1212 "reflect"
1313 "testing"
14+ "time"
1415
1516 "go.mongodb.org/mongo-driver/v2/internal/assert"
1617 "go.mongodb.org/mongo-driver/v2/internal/require"
@@ -251,7 +252,7 @@ func TestEncoderConfiguration(t *testing.T) {
251252 // Test that OmitZeroStruct omits empty structs from the marshaled document if
252253 // OmitEmpty is also set.
253254 {
254- description : "OmitEmpty" ,
255+ description : "OmitEmpty with non-zeroer struct " ,
255256 configure : func (enc * Encoder ) {
256257 enc .OmitZeroStruct ()
257258 enc .OmitEmpty ()
@@ -261,6 +262,29 @@ func TestEncoderConfiguration(t *testing.T) {
261262 }{},
262263 want : bsoncore .NewDocumentBuilder ().Build (),
263264 },
265+ // Test that OmitEmpty omits empty values from the marshaled document.
266+ {
267+ description : "OmitEmpty" ,
268+ configure : func (enc * Encoder ) {
269+ enc .OmitEmpty ()
270+ },
271+ input : struct {
272+ Zero zeroTest
273+ I64 int64
274+ F64 float64
275+ String string
276+ Boolean bool
277+ Slice []int
278+ Array [0 ]int
279+ Map map [string ]int
280+ Bytes []byte
281+ Time time.Time
282+ Pointer * int
283+ }{
284+ Zero : zeroTest {true },
285+ },
286+ want : bsoncore .NewDocumentBuilder ().Build (),
287+ },
264288 // Test that UseJSONStructTags causes the Encoder to fall back to "json" struct tags if
265289 // "bson" struct tags are not available.
266290 {
Original file line number Diff line number Diff line change @@ -1094,6 +1094,8 @@ type zeroTest struct {
10941094
10951095func (z zeroTest ) IsZero () bool { return z .reportZero }
10961096
1097+ var _ Zeroer = zeroTest {}
1098+
10971099func compareZeroTest (_ , _ zeroTest ) bool { return true }
10981100
10991101func compareDecimal128 (d1 , d2 Decimal128 ) bool {
Original file line number Diff line number Diff line change @@ -14,16 +14,6 @@ import (
1414 "go.mongodb.org/mongo-driver/v2/internal/assert"
1515)
1616
17- var _ Zeroer = testZeroer {}
18-
19- type testZeroer struct {
20- val int
21- }
22-
23- func (z testZeroer ) IsZero () bool {
24- return z .val != 0
25- }
26-
2717func TestIsZero (t * testing.T ) {
2818 t .Parallel ()
2919 testCases := []struct {
@@ -84,22 +74,22 @@ func TestIsZero(t *testing.T) {
8474 },
8575 {
8676 description : "zero struct that implements Zeroer" ,
87- value : testZeroer {},
77+ value : zeroTest {},
8878 want : false ,
8979 },
9080 {
9181 description : "non-zero struct that implements Zeroer" ,
92- value : & testZeroer { val : 1 },
82+ value : zeroTest { reportZero : true },
9383 want : true ,
9484 },
9585 {
9686 description : "pointer to zero struct that implements Zeroer" ,
97- value : & testZeroer {},
87+ value : & zeroTest {},
9888 want : false ,
9989 },
10090 {
10191 description : "pointer to non-zero struct that implements Zeroer" ,
102- value : testZeroer { val : 1 },
92+ value : & zeroTest { reportZero : true },
10393 want : true ,
10494 },
10595 {
Original file line number Diff line number Diff line change @@ -957,7 +957,7 @@ func TestClient_BSONOptions(t *testing.T) {
957957 wantRaw : bson .Raw (bsoncore .NewDocumentBuilder ().Build ()),
958958 },
959959 {
960- name : "OmitEmpty" ,
960+ name : "OmitEmpty with non-zeroer struct " ,
961961 bsonOpts : & options.BSONOptions {
962962 OmitZeroStruct : true ,
963963 OmitEmpty : true ,
You can’t perform that action at this time.
0 commit comments