Skip to content

Commit 8051092

Browse files
committed
Remove exposure of bsonx package in stable API
GODRIVER-708 Change-Id: I9781f38eaf72077ad3cef5281d18968164f888e0
1 parent ae01efc commit 8051092

29 files changed

+317
-213
lines changed

event/monitoring.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ package event
99
import (
1010
"context"
1111

12-
"github.com/mongodb/mongo-go-driver/x/bsonx"
12+
"github.com/mongodb/mongo-go-driver/bson"
1313
)
1414

1515
// CommandStartedEvent represents an event generated when a command is sent to a server.
1616
type CommandStartedEvent struct {
17-
Command bsonx.Doc
17+
Command bson.Raw
1818
DatabaseName string
1919
CommandName string
2020
RequestID int64
@@ -32,7 +32,7 @@ type CommandFinishedEvent struct {
3232
// CommandSucceededEvent represents an event generated when a command's execution succeeds.
3333
type CommandSucceededEvent struct {
3434
CommandFinishedEvent
35-
Reply bsonx.Doc
35+
Reply bson.Raw
3636
}
3737

3838
// CommandFailedEvent represents an event generated when a command's execution fails.

mongo/causal_consistency_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"testing"
1313

14+
"github.com/mongodb/mongo-go-driver/bson"
1415
"github.com/mongodb/mongo-go-driver/bson/primitive"
1516
"github.com/mongodb/mongo-go-driver/event"
1617
"github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
@@ -44,23 +45,23 @@ func compareOperationTimes(t *testing.T, expected *primitive.Timestamp, actual *
4445
}
4546
}
4647

47-
func checkOperationTime(t *testing.T, cmd bsonx.Doc, shouldInclude bool) {
48+
func checkOperationTime(t *testing.T, cmd bson.Raw, shouldInclude bool) {
4849
rc, err := cmd.LookupErr("readConcern")
4950
testhelpers.RequireNil(t, err, "key read concern not found")
5051

51-
_, err = rc.Document().LookupErr("afterClusterTime")
52+
_, err = bson.Raw(rc.Value).LookupErr("afterClusterTime")
5253
if shouldInclude {
5354
testhelpers.RequireNil(t, err, "afterClusterTime not found")
5455
} else {
5556
testhelpers.RequireNotNil(t, err, "afterClusterTime found")
5657
}
5758
}
5859

59-
func getOperationTime(t *testing.T, cmd bsonx.Doc) *primitive.Timestamp {
60+
func getOperationTime(t *testing.T, cmd bson.Raw) *primitive.Timestamp {
6061
rc, err := cmd.LookupErr("readConcern")
6162
testhelpers.RequireNil(t, err, "key read concern not found")
6263

63-
ct, err := rc.Document().LookupErr("afterClusterTime")
64+
ct, err := bson.Raw(rc.Value).LookupErr("afterClusterTime")
6465
testhelpers.RequireNil(t, err, "key afterClusterTime not found")
6566

6667
timeT, timeI := ct.Timestamp()
@@ -93,11 +94,11 @@ func createReadFuncMap(t *testing.T, dbName string, collName string) (*Client, *
9394
return client, db, coll, functions
9495
}
9596

96-
func checkReadConcern(t *testing.T, cmd bsonx.Doc, levelIncluded bool, expectedLevel string, optimeIncluded bool, expectedTime *primitive.Timestamp) {
97+
func checkReadConcern(t *testing.T, cmd bson.Raw, levelIncluded bool, expectedLevel string, optimeIncluded bool, expectedTime *primitive.Timestamp) {
9798
rc, err := cmd.LookupErr("readConcern")
9899
testhelpers.RequireNil(t, err, "key readConcern not found")
99100

100-
rcDoc := rc.Document()
101+
rcDoc := bson.Raw(rc.Value)
101102
levelVal, err := rcDoc.LookupErr("level")
102103
if levelIncluded {
103104
testhelpers.RequireNil(t, err, "key level not found")

mongo/change_stream_spec_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ func changeStreamCompareErrors(t *testing.T, expected map[string]interface{}, ac
9595
}
9696
}
9797

98-
func compareCommands(t *testing.T, expected bsonx.Doc, actual bsonx.Doc) {
98+
func compareCommands(t *testing.T, expectedraw, actualraw bson.Raw) {
99+
expected, err := bsonx.ReadDoc(expectedraw)
100+
if err != nil {
101+
t.Fatalf("could not parse document: %v", err)
102+
}
103+
actual, err := bsonx.ReadDoc(actualraw)
104+
if err != nil {
105+
t.Fatalf("could not parse document: %v", err)
106+
}
99107
for _, expectedElem := range expected {
100108

101109
aVal, err := actual.LookupErr(expectedElem.Key)
@@ -140,7 +148,7 @@ func compareCsStartedEvent(t *testing.T, expected json.RawMessage) {
140148
t.Fatalf("db name mismatch. expected %s got %s", expectedDbName, actual.DatabaseName)
141149
}
142150

143-
expectedCmd := expectedDoc.Lookup("command").Document()
151+
expectedCmd, _ := expectedDoc.Lookup("command").Document().MarshalBSON()
144152
compareCommands(t, expectedCmd, actual.Command)
145153
}
146154

mongo/change_stream_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/mongodb/mongo-go-driver/bson"
16+
"github.com/mongodb/mongo-go-driver/bson/bsontype"
1617
"github.com/mongodb/mongo-go-driver/bson/primitive"
1718
"github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
1819
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
@@ -132,7 +133,17 @@ func compareOptions(t *testing.T, expected bsonx.Doc, actual bsonx.Doc) {
132133
}
133134
}
134135

135-
func comparePipelines(t *testing.T, expected bsonx.Arr, actual bsonx.Arr) {
136+
func comparePipelines(t *testing.T, expectedraw, actualraw bson.Raw) {
137+
var expected bsonx.Arr
138+
var actual bsonx.Arr
139+
err := expected.UnmarshalBSONValue(bsontype.Array, expectedraw)
140+
if err != nil {
141+
t.Fatalf("could not unmarshal expected: %v", err)
142+
}
143+
err = actual.UnmarshalBSONValue(bsontype.Array, actualraw)
144+
if err != nil {
145+
t.Fatalf("could not unmarshal actual: %v", err)
146+
}
136147
if len(expected) != len(actual) {
137148
t.Fatalf("pipeline length mismatch. expected %d got %d", len(expected), len(actual))
138149
}
@@ -375,10 +386,6 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
375386

376387
pipeline := started.Command.Lookup("pipeline").Array()
377388

378-
if len(startPipeline) != len(pipeline) {
379-
t.Fatalf("pipeline len mismatch")
380-
}
381-
382389
comparePipelines(t, startPipeline, pipeline)
383390
})
384391

@@ -547,10 +554,10 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
547554
if len(pipeline) == 0 {
548555
t.Fatalf("empty pipeline")
549556
}
550-
csVal := pipeline[0] // doc with nested options document (key $changeStream)
557+
csVal := pipeline.Index(0) // doc with nested options document (key $changeStream)
551558
testhelpers.RequireNil(t, err, "pipeline is empty")
552559

553-
optsVal, err := csVal.Document().LookupErr("$changeStream")
560+
optsVal, err := csVal.Value().Document().LookupErr("$changeStream")
554561
testhelpers.RequireNil(t, err, "key $changeStream not found")
555562

556563
if _, err := optsVal.Document().LookupErr("startAtOperationTime"); err != nil {

mongo/client_test.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/google/go-cmp/cmp"
15+
"github.com/mongodb/mongo-go-driver/bson"
1516
"github.com/mongodb/mongo-go-driver/mongo/options"
1617
"github.com/mongodb/mongo-go-driver/mongo/readconcern"
1718
"github.com/mongodb/mongo-go-driver/mongo/readpref"
@@ -80,10 +81,11 @@ func TestClient(t *testing.T) {
8081
rc := readconcern.Majority()
8182
client, err := newClient(cs)
8283
noerr(t, err)
83-
want, err := rc.MarshalBSONElement()
84+
wantT, wantData, err := rc.MarshalBSONValue()
8485
noerr(t, err)
85-
got, err := client.readConcern.MarshalBSONElement()
86+
gotT, gotData, err := client.readConcern.MarshalBSONValue()
8687
noerr(t, err)
88+
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
8789
if !cmp.Equal(got, want) {
8890
t.Errorf("ReadConcern mode not set correctly. got %v; want %v", got, want)
8991
}
@@ -94,10 +96,11 @@ func TestClient(t *testing.T) {
9496
rc := readconcern.Majority()
9597
client, err := newClient(cs, options.Client().SetReadConcern(rc))
9698
noerr(t, err)
97-
want, err := rc.MarshalBSONElement()
99+
wantT, wantData, err := rc.MarshalBSONValue()
98100
noerr(t, err)
99-
got, err := client.readConcern.MarshalBSONElement()
101+
gotT, gotData, err := client.readConcern.MarshalBSONValue()
100102
noerr(t, err)
103+
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
101104
if !cmp.Equal(got, want) {
102105
t.Errorf("ReadConcern mode not set correctly. got %v; want %v", got, want)
103106
}
@@ -108,10 +111,11 @@ func TestClient(t *testing.T) {
108111
rc := readconcern.Linearizable()
109112
client, err := newClient(cs, options.Client().SetReadConcern(rc))
110113
noerr(t, err)
111-
want, err := rc.MarshalBSONElement()
114+
wantT, wantData, err := rc.MarshalBSONValue()
112115
noerr(t, err)
113-
got, err := client.readConcern.MarshalBSONElement()
116+
gotT, gotData, err := client.readConcern.MarshalBSONValue()
114117
noerr(t, err)
118+
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
115119
if !cmp.Equal(got, want) {
116120
t.Errorf("ReadConcern mode not set correctly. got %v; want %v", got, want)
117121
}
@@ -124,10 +128,11 @@ func TestClient(t *testing.T) {
124128
wc := writeconcern.New(writeconcern.WMajority())
125129
client, err := newClient(cs)
126130
noerr(t, err)
127-
want, err := wc.MarshalBSONElement()
131+
wantT, wantData, err := wc.MarshalBSONValue()
128132
noerr(t, err)
129-
got, err := client.writeConcern.MarshalBSONElement()
133+
gotT, gotData, err := client.writeConcern.MarshalBSONValue()
130134
noerr(t, err)
135+
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
131136
if !cmp.Equal(got, want) {
132137
t.Errorf("WriteConcern mode not set correctly. got %v; want %v", got, want)
133138
}
@@ -138,10 +143,11 @@ func TestClient(t *testing.T) {
138143
wc := writeconcern.New(writeconcern.WMajority())
139144
client, err := newClient(cs, options.Client().SetWriteConcern(wc))
140145
noerr(t, err)
141-
want, err := wc.MarshalBSONElement()
146+
wantT, wantData, err := wc.MarshalBSONValue()
142147
noerr(t, err)
143-
got, err := client.writeConcern.MarshalBSONElement()
148+
gotT, gotData, err := client.writeConcern.MarshalBSONValue()
144149
noerr(t, err)
150+
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
145151
if !cmp.Equal(got, want) {
146152
t.Errorf("WriteConcern mode not set correctly. got %v; want %v", got, want)
147153
}
@@ -152,10 +158,11 @@ func TestClient(t *testing.T) {
152158
wc := writeconcern.New(writeconcern.WMajority())
153159
client, err := newClient(cs, options.Client().SetWriteConcern(wc))
154160
noerr(t, err)
155-
want, err := wc.MarshalBSONElement()
161+
wantT, wantData, err := wc.MarshalBSONValue()
156162
noerr(t, err)
157-
got, err := client.writeConcern.MarshalBSONElement()
163+
gotT, gotData, err := client.writeConcern.MarshalBSONValue()
158164
noerr(t, err)
165+
want, got := bson.RawValue{Type: wantT, Value: wantData}, bson.RawValue{Type: gotT, Value: gotData}
159166
if !cmp.Equal(got, want) {
160167
t.Errorf("WriteConcern mode not set correctly. got %v; want %v", got, want)
161168
}

mongo/command_monitoring_test.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ func compareUpserted(t *testing.T, expected bsonx.Arr, actual bsonx.Arr) {
331331
}
332332

333333
func compareReply(t *testing.T, succeeded *event.CommandSucceededEvent, reply bsonx.Doc) {
334+
eventReply, err := bsonx.ReadDoc(succeeded.Reply)
335+
if err != nil {
336+
t.Fatalf("could not read reply doc: %v", err)
337+
}
334338
for _, elem := range reply {
335339
switch elem.Key {
336340
case "ok":
@@ -339,7 +343,7 @@ func compareReply(t *testing.T, succeeded *event.CommandSucceededEvent, reply bs
339343
actualOkVal, err := succeeded.Reply.LookupErr("ok")
340344
testhelpers.RequireNil(t, err, "could not find key ok in reply")
341345

342-
switch actualOkVal.Type() {
346+
switch actualOkVal.Type {
343347
case bson.TypeInt32:
344348
actualOk = actualOkVal.Int32()
345349
case bson.TypeInt64:
@@ -353,24 +357,24 @@ func compareReply(t *testing.T, succeeded *event.CommandSucceededEvent, reply bs
353357
t.FailNow()
354358
}
355359
case "n":
356-
actualNVal, err := succeeded.Reply.LookupErr("n")
360+
actualNVal, err := eventReply.LookupErr("n")
357361
testhelpers.RequireNil(t, err, "could not find key n in reply")
358362

359363
if !compareValues(elem.Value, actualNVal) {
360364
t.Errorf("n values do not match")
361365
}
362366
case "writeErrors":
363-
actualArr, err := succeeded.Reply.LookupErr("writeErrors")
367+
actualArr, err := eventReply.LookupErr("writeErrors")
364368
testhelpers.RequireNil(t, err, "could not find key writeErrors in reply")
365369

366370
compareWriteErrors(t, elem.Value.Array(), actualArr.Array())
367371
case "cursor":
368-
actualDoc, err := succeeded.Reply.LookupErr("cursor")
372+
actualDoc, err := eventReply.LookupErr("cursor")
369373
testhelpers.RequireNil(t, err, "could not find key cursor in reply")
370374

371375
compareCursors(t, elem.Value.Document(), actualDoc.Document())
372376
case "upserted":
373-
actualDoc, err := succeeded.Reply.LookupErr("upserted")
377+
actualDoc, err := eventReply.LookupErr("upserted")
374378
testhelpers.RequireNil(t, err, "could not find key upserted in reply")
375379

376380
compareUpserted(t, elem.Value.Array(), actualDoc.Array())
@@ -393,6 +397,18 @@ func getInt64(val bsonx.Val) int64 {
393397
return 0
394398
}
395399

400+
func compareRawValues(expected, actual bson.RawValue) bool {
401+
var e bsonx.Val
402+
var a bsonx.Val
403+
if err := e.UnmarshalBSONValue(expected.Type, expected.Value); err != nil {
404+
return false
405+
}
406+
if err := a.UnmarshalBSONValue(actual.Type, actual.Value); err != nil {
407+
return false
408+
}
409+
return compareValues(e, a)
410+
}
411+
396412
func compareValues(expected bsonx.Val, actual bsonx.Val) bool {
397413
if expected.IsNumber() {
398414
if !actual.IsNumber() {
@@ -515,7 +531,7 @@ func compareStartedEvent(t *testing.T, expected bsonx.Doc) {
515531
continue
516532
}
517533

518-
actualVal, err := started.Command.LookupErr(key)
534+
actualRawVal, err := started.Command.LookupErr(key)
519535
testhelpers.RequireNil(t, err, "key %s not found in started event %s", key, started.CommandName)
520536

521537
// TODO: tests in find.json expect different batch sizes for find/getMore commands based on an optional limit
@@ -525,6 +541,11 @@ func compareStartedEvent(t *testing.T, expected bsonx.Doc) {
525541
continue
526542
}
527543

544+
var actualVal bsonx.Val
545+
err = actualVal.UnmarshalBSONValue(actualRawVal.Type, actualRawVal.Value)
546+
if err != nil {
547+
t.Errorf("could not unmarshal actual value: %v", err)
548+
}
528549
if !compareValues(val, actualVal) {
529550
t.Errorf("values for key %s do not match. cmd: %s", key, started.CommandName)
530551
t.FailNow()
@@ -538,7 +559,8 @@ func compareStartedEvent(t *testing.T, expected bsonx.Doc) {
538559

539560
actualArrayVal, err := started.Command.LookupErr(identifier)
540561
testhelpers.RequireNil(t, err, "array with id %s not found in started command", identifier)
541-
actualPayload := actualArrayVal.Array()
562+
var actualPayload bsonx.Arr
563+
err = actualPayload.UnmarshalBSONValue(actualArrayVal.Type, actualArrayVal.Value)
542564

543565
if len(expectedPayload) != len(actualPayload) {
544566
t.Errorf("payload length mismatch. expected %d, got %d", len(expectedPayload), len(actualPayload))
@@ -552,7 +574,11 @@ func compareStartedEvent(t *testing.T, expected bsonx.Doc) {
552574
compareDocs(t, expectedDoc, actualDoc)
553575
}
554576

555-
checkActualFields(t, expectedCmd, started.Command)
577+
startedCommand, err := bsonx.ReadDoc(started.Command)
578+
if err != nil {
579+
t.Fatalf("cannot read command: %v", err)
580+
}
581+
checkActualFields(t, expectedCmd, startedCommand)
556582
}
557583

558584
func compareSuccessEvent(t *testing.T, expected bsonx.Doc) {

0 commit comments

Comments
 (0)