Skip to content

Commit 5c9b6bd

Browse files
committed
chore(refact): assembled bson-dependent methods and tests
This code layout rehashing is a preparatory work to remove the hard dependency on the mongo driver. Signed-off-by: Frederic BIDON <[email protected]>
1 parent 5e4146b commit 5c9b6bd

16 files changed

+1078
-782
lines changed

bson.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import (
1818
"database/sql/driver"
1919
"fmt"
2020

21-
"go.mongodb.org/mongo-driver/bson"
22-
23-
"go.mongodb.org/mongo-driver/bson/bsontype"
2421
bsonprim "go.mongodb.org/mongo-driver/bson/primitive"
2522
)
2623

@@ -113,42 +110,6 @@ func (id *ObjectId) UnmarshalJSON(data []byte) error {
113110
return nil
114111
}
115112

116-
// MarshalBSON renders the object id as a BSON document
117-
func (id ObjectId) MarshalBSON() ([]byte, error) {
118-
return bson.Marshal(bson.M{"data": bsonprim.ObjectID(id)})
119-
}
120-
121-
// UnmarshalBSON reads the objectId from a BSON document
122-
func (id *ObjectId) UnmarshalBSON(data []byte) error {
123-
var obj struct {
124-
Data bsonprim.ObjectID
125-
}
126-
if err := bson.Unmarshal(data, &obj); err != nil {
127-
return err
128-
}
129-
*id = ObjectId(obj.Data)
130-
return nil
131-
}
132-
133-
// MarshalBSONValue is an interface implemented by types that can marshal themselves
134-
// into a BSON document represented as bytes. The bytes returned must be a valid
135-
// BSON document if the error is nil.
136-
func (id ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error) {
137-
oid := bsonprim.ObjectID(id)
138-
return bson.TypeObjectID, oid[:], nil
139-
}
140-
141-
// UnmarshalBSONValue is an interface implemented by types that can unmarshal a
142-
// BSON value representation of themselves. The BSON bytes and type can be
143-
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
144-
// wishes to retain the data after returning.
145-
func (id *ObjectId) UnmarshalBSONValue(_ bsontype.Type, data []byte) error {
146-
var oid bsonprim.ObjectID
147-
copy(oid[:], data)
148-
*id = ObjectId(oid)
149-
return nil
150-
}
151-
152113
// DeepCopyInto copies the receiver and writes its value into out.
153114
func (id *ObjectId) DeepCopyInto(out *ObjectId) {
154115
*out = *id

bson_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ import (
2323
)
2424

2525
func TestBSONObjectId_fullCycle(t *testing.T) {
26+
const str = "507f1f77bcf86cd799439011"
27+
2628
id := NewObjectId("507f1f77bcf86cd799439011")
2729
bytes, err := id.MarshalText()
2830
require.NoError(t, err)
2931

32+
require.True(t, IsBSONObjectID(str))
33+
3034
var idCopy ObjectId
3135

3236
err = idCopy.Scan(bytes)
@@ -37,6 +41,8 @@ func TestBSONObjectId_fullCycle(t *testing.T) {
3741
require.NoError(t, err)
3842
assert.Equal(t, id, idCopy)
3943

44+
require.Equal(t, str, idCopy.String())
45+
4046
jsonBytes, err := id.MarshalJSON()
4147
require.NoError(t, err)
4248

@@ -50,6 +56,7 @@ func TestBSONObjectId_fullCycle(t *testing.T) {
5056
err = bson.Unmarshal(bsonBytes, &idCopy)
5157
require.NoError(t, err)
5258
assert.Equal(t, id, idCopy)
59+
5360
}
5461

5562
func TestDeepCopyObjectId(t *testing.T) {

date.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"time"
22-
23-
"go.mongodb.org/mongo-driver/bson"
2422
)
2523

2624
func init() {
@@ -114,28 +112,6 @@ func (d *Date) UnmarshalJSON(data []byte) error {
114112
return nil
115113
}
116114

117-
func (d Date) MarshalBSON() ([]byte, error) {
118-
return bson.Marshal(bson.M{"data": d.String()})
119-
}
120-
121-
func (d *Date) UnmarshalBSON(data []byte) error {
122-
var m bson.M
123-
if err := bson.Unmarshal(data, &m); err != nil {
124-
return err
125-
}
126-
127-
if data, ok := m["data"].(string); ok {
128-
rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
129-
if err != nil {
130-
return err
131-
}
132-
*d = Date(rd)
133-
return nil
134-
}
135-
136-
return fmt.Errorf("couldn't unmarshal bson bytes value as Date: %w", ErrFormat)
137-
}
138-
139115
// DeepCopyInto copies the receiver and writes its value into out.
140116
func (d *Date) DeepCopyInto(out *Date) {
141117
*out = *d

date_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/stretchr/testify/assert"
2626
"github.com/stretchr/testify/require"
27-
"go.mongodb.org/mongo-driver/bson"
2827
)
2928

3029
var _ sql.Scanner = &Date{}
@@ -57,16 +56,6 @@ func TestDate(t *testing.T) {
5756
require.NoError(t, err)
5857
assert.Equal(t, bj, b)
5958

60-
dateOriginal := Date(time.Date(2014, 10, 10, 0, 0, 0, 0, time.UTC))
61-
62-
bsonData, err := bson.Marshal(&dateOriginal)
63-
require.NoError(t, err)
64-
65-
var dateCopy Date
66-
err = bson.Unmarshal(bsonData, &dateCopy)
67-
require.NoError(t, err)
68-
assert.Equal(t, dateOriginal, dateCopy)
69-
7059
var dateZero Date
7160
err = dateZero.UnmarshalJSON([]byte(jsonNull))
7261
require.NoError(t, err)

0 commit comments

Comments
 (0)