Skip to content

Commit

Permalink
GODRIVER-3205 Use bson options when decoding distinct results
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Jun 5, 2024
1 parent 6f19aef commit db1c0e3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
18 changes: 18 additions & 0 deletions internal/integration/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,24 @@ func TestCollection(t *testing.T) {
assert.EqualValues(mt, tc.want, got, "expected result %v, got %v", tc.want, got)
})
}

collOpts := options.Collection().SetBSONOptions(&options.BSONOptions{AllowTruncatingDoubles: true})
opts := mtest.NewOptions().CollectionOptions(collOpts)

mt.RunOpts("distinct with bson options", opts, func(mt *mtest.T) {
_, err := mt.Coll.InsertOne(context.Background(), bson.D{{"y", 1.7}})
assert.NoError(mt, err, "failed to insert double")

filter := bson.D{{"y", bson.D{{"$gt", 1}}}}

res := mt.Coll.Distinct(context.Background(), "y", filter)
assert.Nil(mt, res.Err(), "Distinct error: %v", res.Err())

var got []int32
assert.NoError(t, res.Decode(&got))

assert.EqualValues(mt, []int32{1}, got)
})
})
mt.RunOpts("find", noClientOpts, func(mt *mtest.T) {
mt.Run("found", func(mt *mtest.T) {
Expand Down
9 changes: 6 additions & 3 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func mergeCollectionOptions(opts ...*options.CollectionOptions) *options.Collect
if opt.Registry != nil {
c.Registry = opt.Registry
}
if opt.BSONOptions != nil {
c.BSONOptions = opt.BSONOptions
}
}

return c
Expand Down Expand Up @@ -1304,7 +1307,6 @@ func (coll *Collection) Distinct(
filter interface{},
opts ...*options.DistinctOptions,
) *DistinctResult {

if ctx == nil {
ctx = context.Background()
}
Expand Down Expand Up @@ -1384,8 +1386,9 @@ func (coll *Collection) Distinct(
}

return &DistinctResult{
reg: coll.registry,
arr: bson.RawArray(arr),
reg: coll.registry,
arr: bson.RawArray(arr),
bsonOpts: coll.bsonOpts,
}
}

Expand Down
22 changes: 14 additions & 8 deletions mongo/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package mongo

import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver/operation"
)

Expand Down Expand Up @@ -179,9 +181,10 @@ type CollectionSpecification struct {
// that error. If the operation did not return any data, all DistinctResult
// methods will return ErrNoDocuments.
type DistinctResult struct {
err error
arr bson.RawArray
reg *bson.Registry
err error
arr bson.RawArray
reg *bson.Registry
bsonOpts *options.BSONOptions
}

// Decode will unmarshal the array represented by this DistinctResult into v. If
Expand All @@ -193,12 +196,15 @@ type DistinctResult struct {
// errors from the unmarshalling process without any modification. If v is nil
// or is a typed nil, an error will be returned.
func (dr *DistinctResult) Decode(v any) error {
val := bson.RawValue{
Value: dr.arr,
Type: bson.TypeArray,
}
doc := bsoncore.NewDocumentBuilder().
AppendValue("arr", bsoncore.Value{
Type: bsoncore.TypeArray,
Data: dr.arr,
}).Build()

dec := getDecoder(doc, dr.bsonOpts, dr.reg)

return val.UnmarshalWithRegistry(dr.reg, v)
return dec.Decode(&struct{ Arr any }{Arr: v})
}

// Err provides a way to check for query errors without calling Decode. Err
Expand Down

0 comments on commit db1c0e3

Please sign in to comment.