Skip to content

Commit 18280a5

Browse files
committed
Move the Doc type family outside of stable
Change-Id: I6ec8f06f91db1d8450d20a8cfe1d08c77aa019f1
1 parent bc03cee commit 18280a5

File tree

149 files changed

+2418
-2260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+2418
-2260
lines changed

benchmark/bson.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313

1414
"github.com/mongodb/mongo-go-driver/bson"
15+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1516
)
1617

1718
const (
@@ -24,12 +25,12 @@ const (
2425

2526
// utility functions for the bson benchmarks
2627

27-
func loadSourceDocument(pathParts ...string) (bson.Doc, error) {
28+
func loadSourceDocument(pathParts ...string) (bsonx.Doc, error) {
2829
data, err := ioutil.ReadFile(filepath.Join(pathParts...))
2930
if err != nil {
3031
return nil, err
3132
}
32-
doc := bson.Doc{}
33+
doc := bsonx.Doc{}
3334
err = bson.UnmarshalExtJSON(data, true, &doc)
3435
if err != nil {
3536
return nil, err

benchmark/bson_document.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"context"
1111
"errors"
1212

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

1616
func bsonDocumentEncoding(ctx context.Context, tm TimerManager, iters int, source string) error {
@@ -48,7 +48,7 @@ func bsonDocumentDecodingLazy(ctx context.Context, tm TimerManager, iters int, s
4848
tm.ResetTimer()
4949

5050
for i := 0; i < iters; i++ {
51-
out, err := bson.ReadDoc(raw)
51+
out, err := bsonx.ReadDoc(raw)
5252
if err != nil {
5353
return err
5454
}
@@ -73,7 +73,7 @@ func bsonDocumentDecoding(ctx context.Context, tm TimerManager, iters, numKeys i
7373
tm.ResetTimer()
7474

7575
for i := 0; i < iters; i++ {
76-
out, err := bson.ReadDoc(raw)
76+
out, err := bsonx.ReadDoc(raw)
7777
if err != nil {
7878
return err
7979
}

benchmark/multi.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212

1313
"github.com/mongodb/mongo-go-driver/bson"
14+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1415
)
1516

1617
func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
@@ -46,7 +47,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {
4647

4748
tm.ResetTimer()
4849

49-
cursor, err := coll.Find(ctx, bson.Doc{})
50+
cursor, err := coll.Find(ctx, bsonx.Doc{})
5051
if err != nil {
5152
return err
5253
}
@@ -108,7 +109,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin
108109
return err
109110
}
110111

111-
_, err = db.RunCommand(ctx, bson.Doc{{"create", bson.String("corpus")}})
112+
_, err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}})
112113
if err != nil {
113114
return err
114115
}

benchmark/single.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"context"
1111
"errors"
1212

13-
"github.com/mongodb/mongo-go-driver/bson"
1413
"github.com/mongodb/mongo-go-driver/internal/testutil"
1514
"github.com/mongodb/mongo-go-driver/mongo"
15+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1616
)
1717

1818
const (
@@ -49,7 +49,7 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
4949
}
5050
defer db.Client().Disconnect(ctx)
5151

52-
cmd := bson.Doc{{"ismaster", bson.Boolean(true)}}
52+
cmd := bsonx.Doc{{"ismaster", bsonx.Boolean(true)}}
5353

5454
tm.ResetTimer()
5555
for i := 0; i < iters; i++ {
@@ -88,7 +88,7 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
8888
coll := db.Collection("corpus")
8989
for i := 0; i < iters; i++ {
9090
id := int32(i)
91-
res, err := coll.InsertOne(ctx, doc.Set("_id", bson.Int32(id)))
91+
res, err := coll.InsertOne(ctx, doc.Set("_id", bsonx.Int32(id)))
9292
if err != nil {
9393
return err
9494
}
@@ -100,7 +100,7 @@ func SingleFindOneByID(ctx context.Context, tm TimerManager, iters int) error {
100100
tm.ResetTimer()
101101

102102
for i := 0; i < iters; i++ {
103-
res := coll.FindOne(ctx, bson.Doc{{"_id", bson.Int32(int32(i))}})
103+
res := coll.FindOne(ctx, bsonx.Doc{{"_id", bsonx.Int32(int32(i))}})
104104
if res == nil {
105105
return errors.New("find one query produced nil result")
106106
}
@@ -135,7 +135,7 @@ func singleInsertCase(ctx context.Context, tm TimerManager, iters int, data stri
135135
return err
136136
}
137137

138-
_, err = db.RunCommand(ctx, bson.Doc{{"create", bson.String("corpus")}})
138+
_, err = db.RunCommand(ctx, bsonx.Doc{{"create", bsonx.String("corpus")}})
139139
if err != nil {
140140
return err
141141
}

bson/bson_corpus_spec_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/google/go-cmp/cmp"
2323
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
2424
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
25+
"github.com/mongodb/mongo-go-driver/x/bsonx"
2526
"github.com/stretchr/testify/require"
2627
"github.com/tidwall/pretty"
2728
)
@@ -161,20 +162,20 @@ func normalizeRelaxedDouble(t *testing.T, key string, rEJ string) string {
161162
}
162163

163164
// bsonToNative decodes the BSON bytes (b) into a native Document
164-
func bsonToNative(t *testing.T, b []byte, bType, testDesc string) Doc {
165-
var doc Doc
166-
err := pc.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), &doc)
165+
func bsonToNative(t *testing.T, b []byte, bType, testDesc string) bsonx.Doc {
166+
var doc bsonx.Doc
167+
err := pc.x.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), &doc)
167168
expectNoError(t, err, fmt.Sprintf("%s: decoding %s BSON", testDesc, bType))
168169
return doc
169170
}
170171

171172
// nativeToBSON encodes the native Document (doc) into canonical BSON and compares it to the expected
172173
// canonical BSON (cB)
173-
func nativeToBSON(t *testing.T, cB []byte, doc Doc, testDesc, bType, docSrcDesc string) {
174+
func nativeToBSON(t *testing.T, cB []byte, doc bsonx.Doc, testDesc, bType, docSrcDesc string) {
174175
actualB := new(bytes.Buffer)
175176
vw, err := bsonrw.NewBSONValueWriter(actualB)
176177
expectNoError(t, err, fmt.Sprintf("%s: creating ValueWriter", testDesc))
177-
err = pc.DocumentEncodeValue(ec, vw, doc)
178+
err = pc.x.DocumentEncodeValue(ec, vw, doc)
178179
expectNoError(t, err, fmt.Sprintf("%s: encoding %s BSON", testDesc, bType))
179180

180181
if diff := cmp.Diff(cB, actualB.Bytes()); diff != "" {
@@ -185,15 +186,15 @@ func nativeToBSON(t *testing.T, cB []byte, doc Doc, testDesc, bType, docSrcDesc
185186
}
186187

187188
// jsonToNative decodes the extended JSON string (ej) into a native Document
188-
func jsonToNative(t *testing.T, ej, ejType, testDesc string) Doc {
189-
var doc Doc
189+
func jsonToNative(t *testing.T, ej, ejType, testDesc string) bsonx.Doc {
190+
var doc bsonx.Doc
190191
err := UnmarshalExtJSON([]byte(ej), ejType != "relaxed", &doc)
191192
expectNoError(t, err, fmt.Sprintf("%s: decoding %s extended JSON", testDesc, ejType))
192193
return doc
193194
}
194195

195196
// nativeToJSON encodes the native Document (doc) into an extended JSON string
196-
func nativeToJSON(t *testing.T, ej string, doc Doc, testDesc, ejType, ejShortName, docSrcDesc string) {
197+
func nativeToJSON(t *testing.T, ej string, doc bsonx.Doc, testDesc, ejType, ejShortName, docSrcDesc string) {
197198
actualEJ, err := MarshalExtJSON(doc, ejType != "relaxed", true)
198199
expectNoError(t, err, fmt.Sprintf("%s: encoding %s extended JSON", testDesc, ejType))
199200

@@ -298,8 +299,8 @@ func runTest(t *testing.T, file string) {
298299
b, err := hex.DecodeString(d.Bson)
299300
expectNoError(t, err, d.Description)
300301

301-
var doc Doc
302-
err = pc.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), &doc)
302+
var doc bsonx.Doc
303+
err = pc.x.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), &doc)
303304
expectError(t, err, fmt.Sprintf("%s: expected decode error", d.Description))
304305
}
305306

@@ -316,7 +317,7 @@ func runTest(t *testing.T, file string) {
316317

317318
switch test.BsonType {
318319
case "0x00":
319-
var doc Doc
320+
var doc bsonx.Doc
320321
err := UnmarshalExtJSON([]byte(s), true, &doc)
321322
expectError(t, err, fmt.Sprintf("%s: expected parse error", p.Description))
322323
case "0x13":

bson/bson_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/google/go-cmp/cmp"
1515
"github.com/mongodb/mongo-go-driver/bson/bsoncore"
16+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1617
"github.com/stretchr/testify/require"
1718
)
1819

@@ -26,7 +27,7 @@ func noerr(t *testing.T, err error) {
2627

2728
func requireErrEqual(t *testing.T, err1 error, err2 error) { require.True(t, compareErrors(err1, err2)) }
2829

29-
func elementSliceEqual(t *testing.T, e1 []Elem, e2 []Elem) {
30+
func elementSliceEqual(t *testing.T, e1 []bsonx.Elem, e2 []bsonx.Elem) {
3031
require.Equal(t, len(e1), len(e2))
3132

3233
for i := range e1 {

bson/encoder_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
1616
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
1717
"github.com/mongodb/mongo-go-driver/bson/bsonrw/bsonrwtest"
18+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1819
)
1920

2021
func TestBasicEncode(t *testing.T) {
@@ -128,15 +129,15 @@ type testMarshaler struct {
128129

129130
func (tm testMarshaler) MarshalBSON() ([]byte, error) { return tm.buf, tm.err }
130131

131-
func docToBytes(d Doc) []byte {
132+
func docToBytes(d bsonx.Doc) []byte {
132133
b, err := d.MarshalBSON()
133134
if err != nil {
134135
panic(err)
135136
}
136137
return b
137138
}
138139

139-
func arrToBytes(a Arr) []byte {
140+
func arrToBytes(a bsonx.Arr) []byte {
140141
_, b, err := a.MarshalBSONValue()
141142
if err != nil {
142143
panic(err)

bson/marshal_test.go

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

1313
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
14+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1415
"github.com/stretchr/testify/require"
1516
)
1617

@@ -105,7 +106,7 @@ func TestMarshal_roundtripFromBytes(t *testing.T) {
105106
0x0,
106107
}
107108

108-
var doc Doc
109+
var doc bsonx.Doc
109110
require.NoError(t, Unmarshal(before, &doc))
110111

111112
after, err := Marshal(doc)
@@ -115,16 +116,16 @@ func TestMarshal_roundtripFromBytes(t *testing.T) {
115116
}
116117

117118
func TestMarshal_roundtripFromDoc(t *testing.T) {
118-
before := Doc{
119-
{"foo", String("bar")},
120-
{"baz", Int32(-27)},
121-
{"bing", Array(Arr{Null(), Regex("word", "i")})},
119+
before := bsonx.Doc{
120+
{"foo", bsonx.String("bar")},
121+
{"baz", bsonx.Int32(-27)},
122+
{"bing", bsonx.Array(bsonx.Arr{bsonx.Null(), bsonx.Regex("word", "i")})},
122123
}
123124

124125
b, err := Marshal(before)
125126
require.NoError(t, err)
126127

127-
var after Doc
128+
var after bsonx.Doc
128129
require.NoError(t, Unmarshal(b, &after))
129130

130131
require.True(t, before.Equal(after))

bson/marshaling_cases_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package bson
88

99
import (
1010
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
11+
"github.com/mongodb/mongo-go-driver/x/bsonx"
1112
)
1213

1314
type marshalingTestCase struct {
@@ -24,6 +25,6 @@ var marshalingTestCases = []marshalingTestCase{
2425
struct {
2526
Foo bool
2627
}{Foo: true},
27-
docToBytes(Doc{{"foo", Boolean(true)}}),
28+
docToBytes(bsonx.Doc{{"foo", bsonx.Boolean(true)}}),
2829
},
2930
}

0 commit comments

Comments
 (0)