Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
"database/sql/driver"
"fmt"

"go.mongodb.org/mongo-driver/bson"

"go.mongodb.org/mongo-driver/bson/bsontype"
bsonprim "go.mongodb.org/mongo-driver/bson/primitive"
)

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

// MarshalBSON renders the object id as a BSON document
func (id ObjectId) MarshalBSON() ([]byte, error) {
return bson.Marshal(bson.M{"data": bsonprim.ObjectID(id)})
}

// UnmarshalBSON reads the objectId from a BSON document
func (id *ObjectId) UnmarshalBSON(data []byte) error {
var obj struct {
Data bsonprim.ObjectID
}
if err := bson.Unmarshal(data, &obj); err != nil {
return err
}
*id = ObjectId(obj.Data)
return nil
}

// MarshalBSONValue is an interface implemented by types that can marshal themselves
// into a BSON document represented as bytes. The bytes returned must be a valid
// BSON document if the error is nil.
func (id ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error) {
oid := bsonprim.ObjectID(id)
return bson.TypeObjectID, oid[:], nil
}

// UnmarshalBSONValue is an interface implemented by types that can unmarshal a
// BSON value representation of themselves. The BSON bytes and type can be
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
// wishes to retain the data after returning.
func (id *ObjectId) UnmarshalBSONValue(_ bsontype.Type, data []byte) error {
var oid bsonprim.ObjectID
copy(oid[:], data)
*id = ObjectId(oid)
return nil
}

// DeepCopyInto copies the receiver and writes its value into out.
func (id *ObjectId) DeepCopyInto(out *ObjectId) {
*out = *id
Expand Down
7 changes: 7 additions & 0 deletions bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import (
)

func TestBSONObjectId_fullCycle(t *testing.T) {
const str = "507f1f77bcf86cd799439011"

id := NewObjectId("507f1f77bcf86cd799439011")
bytes, err := id.MarshalText()
require.NoError(t, err)

require.True(t, IsBSONObjectID(str))

var idCopy ObjectId

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

require.Equal(t, str, idCopy.String())

jsonBytes, err := id.MarshalJSON()
require.NoError(t, err)

Expand All @@ -50,6 +56,7 @@ func TestBSONObjectId_fullCycle(t *testing.T) {
err = bson.Unmarshal(bsonBytes, &idCopy)
require.NoError(t, err)
assert.Equal(t, id, idCopy)

}

func TestDeepCopyObjectId(t *testing.T) {
Expand Down
24 changes: 0 additions & 24 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"encoding/json"
"fmt"
"time"

"go.mongodb.org/mongo-driver/bson"
)

func init() {
Expand Down Expand Up @@ -114,28 +112,6 @@ func (d *Date) UnmarshalJSON(data []byte) error {
return nil
}

func (d Date) MarshalBSON() ([]byte, error) {
return bson.Marshal(bson.M{"data": d.String()})
}

func (d *Date) UnmarshalBSON(data []byte) error {
var m bson.M
if err := bson.Unmarshal(data, &m); err != nil {
return err
}

if data, ok := m["data"].(string); ok {
rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
if err != nil {
return err
}
*d = Date(rd)
return nil
}

return fmt.Errorf("couldn't unmarshal bson bytes value as Date: %w", ErrFormat)
}

// DeepCopyInto copies the receiver and writes its value into out.
func (d *Date) DeepCopyInto(out *Date) {
*out = *d
Expand Down
11 changes: 0 additions & 11 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
)

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

dateOriginal := Date(time.Date(2014, 10, 10, 0, 0, 0, 0, time.UTC))

bsonData, err := bson.Marshal(&dateOriginal)
require.NoError(t, err)

var dateCopy Date
err = bson.Unmarshal(bsonData, &dateCopy)
require.NoError(t, err)
assert.Equal(t, dateOriginal, dateCopy)

var dateZero Date
err = dateZero.UnmarshalJSON([]byte(jsonNull))
require.NoError(t, err)
Expand Down
Loading
Loading