Skip to content

Commit

Permalink
Add conversions to and from MongoDB driver (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Dec 25, 2024
1 parent d102839 commit f782d49
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 2 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ toolchain go1.23.4
require (
github.com/stretchr/testify v1.10.0
github.com/xdg-go/scram v1.1.2
go.mongodb.org/mongo-driver/v2 v2.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
Expand All @@ -11,6 +13,8 @@ github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3k
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver/v2 v2.0.0 h1:Jfd7XpdZa9yk3eY774bO7SWVb30noLSirL9nKTpavhI=
go.mongodb.org/mongo-driver/v2 v2.0.0/go.mod h1:nSjmNq4JUstE8IRZKTktLgMHM4F1fccL6HGX1yh+8RA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -29,8 +33,9 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
152 changes: 152 additions & 0 deletions wirebson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ package wirebson
import (
"time"

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

"github.com/FerretDB/wire/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -111,3 +113,153 @@ func validBSONType(v any) error {

return nil
}

// fromDriver converts MongoDB driver value ([bson.D], [bson.A], etc) to wirebson value.
func fromDriver(v any) (any, error) {
switch v := v.(type) {
// composite types

case bson.D:
doc := MakeDocument(len(v))
for _, e := range v {
val, err := fromDriver(e.Value)
if err != nil {
return nil, lazyerrors.Error(err)
}

if err = doc.Add(e.Key, val); err != nil {
return nil, lazyerrors.Error(err)
}
}

return doc, nil

case bson.A:
arr := MakeArray(len(v))
for _, e := range v {
val, err := fromDriver(e)
if err != nil {
return nil, lazyerrors.Error(err)
}

if err = arr.Add(val); err != nil {
return nil, lazyerrors.Error(err)
}
}

return arr, nil

// scalar types (in the same order as in bson package)

case float64:
return v, nil
case string:
return v, nil
case bson.Binary:
return Binary{
Subtype: BinarySubtype(v.Subtype),
B: v.Data,
}, nil
case bson.ObjectID:
return ObjectID(v), nil
case bool:
return v, nil
case bson.DateTime:
return v.Time().UTC(), nil
case bson.Null, nil:
return Null, nil
case bson.Regex:
return Regex{
Pattern: v.Pattern,
Options: v.Options,
}, nil
case int32:
return v, nil
case bson.Timestamp:
return Timestamp(uint64(v.T)<<32 | uint64(v.I)), nil
case int64:
return v, nil
case bson.Decimal128:
h, l := v.GetBytes()

return Decimal128{
L: l,
H: h,
}, nil

default:
return nil, lazyerrors.Errorf("invalid BSON type %T", v)
}
}

// toDriver converts wirebson value to MongoDB driver value (bson.D, bson.A, etc).
func toDriver(v any) (any, error) {
switch v := v.(type) {
// composite types

case *Document:
doc := make(bson.D, 0, v.Len())
for k, v := range v.All() {
val, err := toDriver(v)
if err != nil {
return nil, lazyerrors.Error(err)
}

doc = append(doc, bson.E{Key: k, Value: val})
}

return doc, nil

case *Array:
arr := make(bson.A, v.Len())
for i, v := range v.All() {
val, err := toDriver(v)
if err != nil {
return nil, lazyerrors.Error(err)
}

arr[i] = val
}

return arr, nil

// scalar types (in the same order as in bson package)

case float64:
return v, nil
case string:
return v, nil
case Binary:
return bson.Binary{
Subtype: byte(v.Subtype),
Data: v.B,
}, nil
case ObjectID:
return bson.ObjectID(v), nil
case bool:
return v, nil
case time.Time:
return bson.NewDateTimeFromTime(v), nil
case NullType:
return nil, nil
case Regex:
return bson.Regex{
Pattern: v.Pattern,
Options: v.Options,
}, nil
case int32:
return v, nil
case Timestamp:
return bson.Timestamp{
T: uint32(v >> 32),
I: uint32(v),
}, nil
case int64:
return v, nil
case Decimal128:
return bson.NewDecimal128(v.H, v.L), nil

default:
return nil, lazyerrors.Errorf("invalid BSON type %T", v)
}
}
23 changes: 23 additions & 0 deletions wirebson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,15 @@ func TestNormal(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, tc.raw, raw, "actual:\n"+hex.Dump(raw))
})

t.Run("ToDriverFromDrive", func(t *testing.T) {
d, err := toDriver(tc.doc)
require.NoError(t, err)

doc, err := fromDriver(d)
require.NoError(t, err)
assert.Equal(t, tc.doc, doc)
})
})
}
}
Expand Down Expand Up @@ -1111,6 +1120,20 @@ func testRawDocument(t *testing.T, rawDoc RawDocument) {
require.NoError(t, err)
assert.Equal(t, rawDoc, raw)
})

t.Run("ToDriverFromDriver", func(t *testing.T) {
doc, err := rawDoc.DecodeDeep()
if err != nil {
return
}

d, err := toDriver(doc)
require.NoError(t, err)

doc2, err := fromDriver(d)
require.NoError(t, err)
assert.Equal(t, doc, doc2)
})
}

func FuzzDocument(f *testing.F) {
Expand Down

0 comments on commit f782d49

Please sign in to comment.