@@ -4,13 +4,14 @@ import (
44 "fmt"
55 "strings"
66
7+ "github.com/10gen/migration-verifier/mslices"
78 "github.com/pkg/errors"
8- "github.com/samber/lo"
99 "go.mongodb.org/mongo-driver/bson"
1010 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
1111)
1212
13- // This extracts the document key from a document gets its field names.
13+ // ExtractTrueDocKeyFromDoc extracts the document key from a document
14+ // given its field names.
1415//
1516// NB: This avoids the problem documented in SERVER-109340; as a result,
1617// the returned key may not always match the change stream’s `documentKey`
@@ -21,7 +22,8 @@ func ExtractTrueDocKeyFromDoc(
2122) (bson.Raw , error ) {
2223 assertFieldNameUniqueness (fieldNames )
2324
24- var dk bson.D
25+ docBuilder := bsoncore .NewDocumentBuilder ()
26+
2527 for _ , field := range fieldNames {
2628 var val bson.RawValue
2729
@@ -38,19 +40,20 @@ func ExtractTrueDocKeyFromDoc(
3840 return nil , errors .Wrapf (err , "extracting doc key field %#q from doc %+v" , field , doc )
3941 }
4042
41- dk = append (dk , bson.E {field , val })
42- }
43-
44- docKey , err := bson .Marshal (dk )
45- if err != nil {
46- return nil , errors .Wrapf (err , "marshaling doc key %v from doc %v" , dk , docKey )
43+ docBuilder .AppendValue (
44+ field ,
45+ bsoncore.Value {
46+ Type : val .Type ,
47+ Data : val .Value ,
48+ },
49+ )
4750 }
4851
49- return docKey , nil
52+ return bson . Raw ( docBuilder . Build ()) , nil
5053}
5154
5255func assertFieldNameUniqueness (fieldNames []string ) {
53- if len ( lo . Uniq (fieldNames )) != len ( fieldNames ) {
56+ if mslices . FindFirstDupe (fieldNames ). IsSome ( ) {
5457 panic (fmt .Sprintf ("Duplicate field names: %v" , fieldNames ))
5558 }
5659}
0 commit comments