Skip to content

Commit 635eb59

Browse files
authored
Merge branch 'master' into godriver3175-oidc-k8s
2 parents 4778463 + b225485 commit 635eb59

File tree

10 files changed

+112
-16
lines changed

10 files changed

+112
-16
lines changed

.github/workflows/merge-up.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Merge up
2+
3+
on:
4+
push:
5+
branches:
6+
- release/*.*
7+
- v*
8+
9+
permissions:
10+
id-token: write
11+
contents: write
12+
pull-requests: write
13+
14+
jobs:
15+
merge-up:
16+
name: Create merge up pull request
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- uses: mongodb-labs/drivers-github-tools/secure-checkout@v2
21+
with:
22+
app_id: ${{ vars.PR_APP_ID }}
23+
private_key: ${{ secrets.PR_APP_PRIVATE_KEY }}
24+
# Make sure to include fetch-depth 0 so all branches are fetched, not
25+
# just the current one
26+
fetch-depth: 0
27+
28+
- name: Create pull request
29+
id: create-pull-request
30+
uses: alcaeus/automatic-merge-up-action@main
31+
with:
32+
ref: ${{ github.ref_name }}
33+
branchNamePattern: 'release/<major>.<minor>'
34+
devBranchNamePattern: 'v<major>'
35+
fallbackBranch: 'master'
36+
enableAutoMerge: true

bson/bsoncodec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type EncodeContext struct {
8888
nilSliceAsEmpty bool
8989
nilByteSliceAsEmpty bool
9090
omitZeroStruct bool
91+
omitEmpty bool
9192
useJSONStructTags bool
9293
}
9394

bson/encoder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,21 @@ func (e *Encoder) NilByteSliceAsEmpty() {
108108
// TODO struct fields once the logic is updated to also inspect private struct fields.
109109

110110
// OmitZeroStruct causes the Encoder to consider the zero value for a struct (e.g. MyStruct{})
111-
// as empty and omit it from the marshaled BSON when the "omitempty" struct tag option is set.
111+
// as empty and omit it from the marshaled BSON when the "omitempty" struct tag option is set
112+
// or the OmitEmpty() method is called.
112113
//
113114
// Note that the Encoder only examines exported struct fields when determining if a struct is the
114115
// zero value. It considers pointers to a zero struct value (e.g. &MyStruct{}) not empty.
115116
func (e *Encoder) OmitZeroStruct() {
116117
e.ec.omitZeroStruct = true
117118
}
118119

120+
// OmitEmpty causes the Encoder to omit empty values from the marshaled BSON as the "omitempty"
121+
// struct tag option is set.
122+
func (e *Encoder) OmitEmpty() {
123+
e.ec.omitEmpty = true
124+
}
125+
119126
// UseJSONStructTags causes the Encoder to fall back to using the "json" struct tag if a "bson"
120127
// struct tag is not specified.
121128
func (e *Encoder) UseJSONStructTags() {

bson/encoder_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff 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"
@@ -248,6 +249,42 @@ func TestEncoderConfiguration(t *testing.T) {
248249
}{},
249250
want: bsoncore.NewDocumentBuilder().Build(),
250251
},
252+
// Test that OmitZeroStruct omits empty structs from the marshaled document if
253+
// OmitEmpty is also set.
254+
{
255+
description: "OmitEmpty with non-zeroer struct",
256+
configure: func(enc *Encoder) {
257+
enc.OmitZeroStruct()
258+
enc.OmitEmpty()
259+
},
260+
input: struct {
261+
Zero zeroStruct
262+
}{},
263+
want: bsoncore.NewDocumentBuilder().Build(),
264+
},
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+
},
251288
// Test that UseJSONStructTags causes the Encoder to fall back to "json" struct tags if
252289
// "bson" struct tags are not available.
253290
{

bson/primitive_codecs_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,8 @@ type zeroTest struct {
10941094

10951095
func (z zeroTest) IsZero() bool { return z.reportZero }
10961096

1097+
var _ Zeroer = zeroTest{}
1098+
10971099
func compareZeroTest(_, _ zeroTest) bool { return true }
10981100

10991101
func compareDecimal128(d1, d2 Decimal128) bool {

bson/struct_codec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func (sc *structCodec) EncodeValue(ec EncodeContext, vw ValueWriter, val reflect
118118
}
119119
}
120120

121+
if ec.omitEmpty {
122+
desc.omitEmpty = true
123+
}
124+
121125
desc.encoder, rv, err = lookupElementEncoder(ec, desc.encoder, rv)
122126

123127
if err != nil && !errors.Is(err, errInvalidValue) {

bson/struct_codec_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff 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-
2717
func 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
{

internal/integration/client_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,19 @@ func TestClient_BSONOptions(t *testing.T) {
956956
want: &bson.D{},
957957
wantRaw: bson.Raw(bsoncore.NewDocumentBuilder().Build()),
958958
},
959+
{
960+
name: "OmitEmpty with non-zeroer struct",
961+
bsonOpts: &options.BSONOptions{
962+
OmitZeroStruct: true,
963+
OmitEmpty: true,
964+
},
965+
doc: struct {
966+
X jsonTagsTest `bson:"x"`
967+
}{},
968+
decodeInto: func() interface{} { return &bson.D{} },
969+
want: &bson.D{},
970+
wantRaw: bson.Raw(bsoncore.NewDocumentBuilder().Build()),
971+
},
959972
{
960973
name: "StringifyMapKeysWithFmt",
961974
bsonOpts: &options.BSONOptions{

mongo/mongo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func getEncoder(
8383
if opts.OmitZeroStruct {
8484
enc.OmitZeroStruct()
8585
}
86+
if opts.OmitEmpty {
87+
enc.OmitEmpty()
88+
}
8689
if opts.StringifyMapKeysWithFmt {
8790
enc.StringifyMapKeysWithFmt()
8891
}

mongo/options/clientoptions.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,12 @@ type BSONOptions struct {
182182

183183
// OmitZeroStruct causes the driver to consider the zero value for a struct
184184
// (e.g. MyStruct{}) as empty and omit it from the marshaled BSON when the
185-
// "omitempty" struct tag option is set.
185+
// "omitempty" struct tag option or the "OmitEmpty" field is set.
186186
OmitZeroStruct bool
187187

188+
// OmitEmpty causes the driver to omit empty values from the marshaled BSON.
189+
OmitEmpty bool
190+
188191
// StringifyMapKeysWithFmt causes the driver to convert Go map keys to BSON
189192
// document field name strings using fmt.Sprint instead of the default
190193
// string conversion logic.

0 commit comments

Comments
 (0)