Skip to content

Commit 620f598

Browse files
committed
Reverse the dependency between bson and bsonx
GODRIVER-669 Change-Id: I0a25021a7537ec34001cc258fa8909cf18162434
1 parent 733dc8b commit 620f598

16 files changed

+1116
-1057
lines changed

bson/bson_corpus_spec_test.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
package bson
88

99
import (
10-
"bytes"
1110
"encoding/hex"
1211
"encoding/json"
1312
"fmt"
1413
"io/ioutil"
1514
"path"
16-
"reflect"
1715
"strconv"
1816
"strings"
1917
"testing"
@@ -23,7 +21,6 @@ import (
2321
"github.com/google/go-cmp/cmp"
2422
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
2523
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
26-
"github.com/mongodb/mongo-go-driver/x/bsonx"
2724
"github.com/stretchr/testify/require"
2825
"github.com/tidwall/pretty"
2926
)
@@ -62,7 +59,6 @@ type parseErrorTestCase struct {
6259

6360
const dataDir = "../data"
6461

65-
var pc bsonx.PrimitiveCodecs
6662
var dvd bsoncodec.DefaultValueDecoders
6763
var dve bsoncodec.DefaultValueEncoders
6864

@@ -163,39 +159,36 @@ func normalizeRelaxedDouble(t *testing.T, key string, rEJ string) string {
163159
}
164160

165161
// bsonToNative decodes the BSON bytes (b) into a native Document
166-
func bsonToNative(t *testing.T, b []byte, bType, testDesc string) bsonx.Doc {
167-
doc := reflect.New(reflect.TypeOf(bsonx.Doc{})).Elem()
168-
err := pc.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), doc)
162+
func bsonToNative(t *testing.T, b []byte, bType, testDesc string) D {
163+
var doc D
164+
err := Unmarshal(b, &doc)
169165
expectNoError(t, err, fmt.Sprintf("%s: decoding %s BSON", testDesc, bType))
170-
return doc.Interface().(bsonx.Doc)
166+
return doc
171167
}
172168

173169
// nativeToBSON encodes the native Document (doc) into canonical BSON and compares it to the expected
174170
// canonical BSON (cB)
175-
func nativeToBSON(t *testing.T, cB []byte, doc bsonx.Doc, testDesc, bType, docSrcDesc string) {
176-
actualB := new(bytes.Buffer)
177-
vw, err := bsonrw.NewBSONValueWriter(actualB)
178-
expectNoError(t, err, fmt.Sprintf("%s: creating ValueWriter", testDesc))
179-
err = pc.DocumentEncodeValue(ec, vw, reflect.ValueOf(doc))
171+
func nativeToBSON(t *testing.T, cB []byte, doc D, testDesc, bType, docSrcDesc string) {
172+
actual, err := Marshal(doc)
180173
expectNoError(t, err, fmt.Sprintf("%s: encoding %s BSON", testDesc, bType))
181174

182-
if diff := cmp.Diff(cB, actualB.Bytes()); diff != "" {
175+
if diff := cmp.Diff(cB, actual); diff != "" {
183176
t.Errorf("%s: 'native_to_bson(%s) = cB' failed (-want, +got):\n-%v\n+%v\n",
184-
testDesc, docSrcDesc, cB, actualB.Bytes())
177+
testDesc, docSrcDesc, cB, actual)
185178
t.FailNow()
186179
}
187180
}
188181

189182
// jsonToNative decodes the extended JSON string (ej) into a native Document
190-
func jsonToNative(t *testing.T, ej, ejType, testDesc string) bsonx.Doc {
191-
var doc bsonx.Doc
183+
func jsonToNative(t *testing.T, ej, ejType, testDesc string) D {
184+
var doc D
192185
err := UnmarshalExtJSON([]byte(ej), ejType != "relaxed", &doc)
193186
expectNoError(t, err, fmt.Sprintf("%s: decoding %s extended JSON", testDesc, ejType))
194187
return doc
195188
}
196189

197190
// nativeToJSON encodes the native Document (doc) into an extended JSON string
198-
func nativeToJSON(t *testing.T, ej string, doc bsonx.Doc, testDesc, ejType, ejShortName, docSrcDesc string) {
191+
func nativeToJSON(t *testing.T, ej string, doc D, testDesc, ejType, ejShortName, docSrcDesc string) {
199192
actualEJ, err := MarshalExtJSON(doc, ejType != "relaxed", true)
200193
expectNoError(t, err, fmt.Sprintf("%s: encoding %s extended JSON", testDesc, ejType))
201194

@@ -300,8 +293,8 @@ func runTest(t *testing.T, file string) {
300293
b, err := hex.DecodeString(d.Bson)
301294
expectNoError(t, err, d.Description)
302295

303-
doc := reflect.New(reflect.TypeOf(bsonx.Doc{})).Elem()
304-
err = pc.DocumentDecodeValue(dc, bsonrw.NewBSONDocumentReader(b), doc)
296+
var doc D
297+
err = Unmarshal(b, &doc)
305298
expectError(t, err, fmt.Sprintf("%s: expected decode error", d.Description))
306299
}
307300

@@ -318,7 +311,7 @@ func runTest(t *testing.T, file string) {
318311

319312
switch test.BsonType {
320313
case "0x00":
321-
var doc bsonx.Doc
314+
var doc D
322315
err := UnmarshalExtJSON([]byte(s), true, &doc)
323316
expectError(t, err, fmt.Sprintf("%s: expected parse error", p.Description))
324317
case "0x13":

bson/bson_test.go

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

1414
"github.com/google/go-cmp/cmp"
15-
"github.com/mongodb/mongo-go-driver/x/bsonx"
1615
"github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore"
1716
"github.com/stretchr/testify/require"
1817
)
@@ -27,14 +26,6 @@ func noerr(t *testing.T, err error) {
2726

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

30-
func elementSliceEqual(t *testing.T, e1 []bsonx.Elem, e2 []bsonx.Elem) {
31-
require.Equal(t, len(e1), len(e2))
32-
33-
for i := range e1 {
34-
require.True(t, readerElementComparer(e1[i], e2[i]))
35-
}
36-
}
37-
3829
func TestTimeRoundTrip(t *testing.T) {
3930
val := struct {
4031
Value time.Time

bson/bsoncodec/default_value_decoders.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) {
9393
RegisterTypeMapEntry(bsontype.DBPointer, tDBPointer).
9494
RegisterTypeMapEntry(bsontype.JavaScript, tJavaScript).
9595
RegisterTypeMapEntry(bsontype.Symbol, tSymbol).
96+
RegisterTypeMapEntry(bsontype.CodeWithScope, tCodeWithScope).
9697
RegisterTypeMapEntry(bsontype.Int32, tInt32).
9798
RegisterTypeMapEntry(bsontype.Int64, tInt64).
9899
RegisterTypeMapEntry(bsontype.Timestamp, tTimestamp).

bson/decoder_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
1818
"github.com/mongodb/mongo-go-driver/bson/bsonrw/bsonrwtest"
1919
"github.com/mongodb/mongo-go-driver/bson/bsontype"
20-
"github.com/mongodb/mongo-go-driver/x/bsonx"
2120
"github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore"
2221
)
2322

@@ -182,7 +181,7 @@ func TestDecoderv2(t *testing.T) {
182181
var got foo
183182
got.Item = "apple"
184183
got.Bonus = 2
185-
data := docToBytes(bsonx.Doc{{"item", bsonx.String("canvas")}, {"qty", bsonx.Int32(4)}})
184+
data := docToBytes(D{{"item", "canvas"}, {"qty", 4}})
186185
vr := bsonrw.NewBSONDocumentReader(data)
187186
dec, err := NewDecoder(vr)
188187
noerr(t, err)

bson/encoder_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ 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"
1918
)
2019

2120
func TestBasicEncode(t *testing.T) {
@@ -127,16 +126,8 @@ type testMarshaler struct {
127126

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

130-
func docToBytes(d bsonx.Doc) []byte {
131-
b, err := d.MarshalBSON()
132-
if err != nil {
133-
panic(err)
134-
}
135-
return b
136-
}
137-
138-
func arrToBytes(a bsonx.Arr) []byte {
139-
_, b, err := a.MarshalBSONValue()
129+
func docToBytes(d interface{}) []byte {
130+
b, err := Marshal(d)
140131
if err != nil {
141132
panic(err)
142133
}

bson/marshal_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"bytes"
1111
"testing"
1212

13+
"github.com/google/go-cmp/cmp"
1314
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
14-
"github.com/mongodb/mongo-go-driver/x/bsonx"
15+
"github.com/mongodb/mongo-go-driver/bson/primitive"
1516
"github.com/stretchr/testify/require"
1617
)
1718

@@ -180,7 +181,7 @@ func TestMarshal_roundtripFromBytes(t *testing.T) {
180181
0x0,
181182
}
182183

183-
var doc bsonx.Doc
184+
var doc D
184185
require.NoError(t, Unmarshal(before, &doc))
185186

186187
after, err := Marshal(doc)
@@ -190,17 +191,19 @@ func TestMarshal_roundtripFromBytes(t *testing.T) {
190191
}
191192

192193
func TestMarshal_roundtripFromDoc(t *testing.T) {
193-
before := bsonx.Doc{
194-
{"foo", bsonx.String("bar")},
195-
{"baz", bsonx.Int32(-27)},
196-
{"bing", bsonx.Array(bsonx.Arr{bsonx.Null(), bsonx.Regex("word", "i")})},
194+
before := D{
195+
{"foo", "bar"},
196+
{"baz", int64(-27)},
197+
{"bing", A{nil, primitive.Regex{Pattern: "word", Options: "i"}}},
197198
}
198199

199200
b, err := Marshal(before)
200201
require.NoError(t, err)
201202

202-
var after bsonx.Doc
203+
var after D
203204
require.NoError(t, Unmarshal(b, &after))
204205

205-
require.True(t, before.Equal(after))
206+
if !cmp.Equal(after, before) {
207+
t.Errorf("Documents to not match. got %v; want %v", after, before)
208+
}
206209
}

bson/marshaling_cases_test.go

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

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

1413
type marshalingTestCase struct {
@@ -25,6 +24,6 @@ var marshalingTestCases = []marshalingTestCase{
2524
struct {
2625
Foo bool
2726
}{Foo: true},
28-
docToBytes(bsonx.Doc{{"foo", bsonx.Boolean(true)}}),
27+
docToBytes(D{{"foo", true}}),
2928
},
3029
}

bson/primitive_codecs.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
1414
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
15-
"github.com/mongodb/mongo-go-driver/x/bsonx"
1615
)
1716

1817
var primitiveCodecs PrimitiveCodecs
@@ -33,8 +32,6 @@ func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder)
3332
RegisterEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)).
3433
RegisterDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)).
3534
RegisterDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue))
36-
37-
bsonx.PrimitiveCodecs{}.RegisterPrimitiveCodecs(rb)
3835
}
3936

4037
// RawValueEncodeValue is the ValueEncoderFunc for RawValue.

0 commit comments

Comments
 (0)