@@ -72,70 +72,9 @@ func (me MarshalError) Error() string {
72
72
//
73
73
type Pipeline []bson.D
74
74
75
- // transformAndEnsureID is a hack that makes it easy to get a RawValue as the _id value. This will
76
- // be removed when we switch from using bsonx to bsoncore for the driver package.
77
- func transformAndEnsureID (registry * bsoncodec.Registry , val interface {}) (bsonx.Doc , interface {}, error ) {
78
- // TODO: performance is going to be pretty bad for bsonx.Doc here since we turn it into a []byte
79
- // only to turn it back into a bsonx.Doc. We can fix this post beta1 when we refactor the driver
80
- // package to use bsoncore.Document instead of bsonx.Doc.
81
- if registry == nil {
82
- registry = bson .NewRegistryBuilder ().Build ()
83
- }
84
- switch tt := val .(type ) {
85
- case nil :
86
- return nil , nil , ErrNilDocument
87
- case bsonx.Doc :
88
- val = tt .Copy ()
89
- case []byte :
90
- // Slight optimization so we'll just use MarshalBSON and not go through the codec machinery.
91
- val = bson .Raw (tt )
92
- }
93
-
94
- // TODO(skriptble): Use a pool of these instead.
95
- buf := make ([]byte , 0 , 256 )
96
- b , err := bson .MarshalAppendWithRegistry (registry , buf , val )
97
- if err != nil {
98
- return nil , nil , MarshalError {Value : val , Err : err }
99
- }
100
-
101
- d , err := bsonx .ReadDoc (b )
102
- if err != nil {
103
- return nil , nil , err
104
- }
105
-
106
- var id interface {}
107
-
108
- idx := d .IndexOf ("_id" )
109
- var idElem bsonx.Elem
110
- switch idx {
111
- case - 1 :
112
- idElem = bsonx.Elem {"_id" , bsonx .ObjectID (primitive .NewObjectID ())}
113
- d = append (d , bsonx.Elem {})
114
- copy (d [1 :], d )
115
- d [0 ] = idElem
116
- default :
117
- idElem = d [idx ]
118
- copy (d [1 :idx + 1 ], d [0 :idx ])
119
- d [0 ] = idElem
120
- }
121
-
122
- idBuf := make ([]byte , 0 , 256 )
123
- t , data , err := idElem .Value .MarshalAppendBSONValue (idBuf [:0 ])
124
- if err != nil {
125
- return nil , nil , err
126
- }
127
-
128
- err = bson.RawValue {Type : t , Value : data }.UnmarshalWithRegistry (registry , & id )
129
- if err != nil {
130
- return nil , nil , err
131
- }
132
-
133
- return d , id , nil
134
- }
135
-
136
- // transformAndEnsureIDv2 is a hack that makes it easy to get a RawValue as the _id value. This will
137
- // be removed when we switch from using bsonx to bsoncore for the driver package.
138
- func transformAndEnsureIDv2 (registry * bsoncodec.Registry , val interface {}) (bsoncore.Document , interface {}, error ) {
75
+ // transformAndEnsureID is a hack that makes it easy to get a RawValue as the _id value.
76
+ // It will also add an ObjectID _id as the first key if it not already present in the passed-in val.
77
+ func transformAndEnsureID (registry * bsoncodec.Registry , val interface {}) (bsoncore.Document , interface {}, error ) {
139
78
if registry == nil {
140
79
registry = bson .NewRegistryBuilder ().Build ()
141
80
}
@@ -237,17 +176,7 @@ func ensureID(d bsonx.Doc) (bsonx.Doc, interface{}) {
237
176
return d , id
238
177
}
239
178
240
- func ensureDollarKey (doc bsonx.Doc ) error {
241
- if len (doc ) == 0 {
242
- return errors .New ("update document must have at least one element" )
243
- }
244
- if ! strings .HasPrefix (doc [0 ].Key , "$" ) {
245
- return errors .New ("update document must contain key beginning with '$'" )
246
- }
247
- return nil
248
- }
249
-
250
- func ensureDollarKeyv2 (doc bsoncore.Document ) error {
179
+ func ensureDollarKey (doc bsoncore.Document ) error {
251
180
firstElem , err := doc .IndexErr (0 )
252
181
if err != nil {
253
182
return errors .New ("update document must have at least one element" )
@@ -267,39 +196,7 @@ func ensureNoDollarKey(doc bsoncore.Document) error {
267
196
return nil
268
197
}
269
198
270
- func transformAggregatePipeline (registry * bsoncodec.Registry , pipeline interface {}) (bsonx.Arr , error ) {
271
- pipelineArr := bsonx.Arr {}
272
- switch t := pipeline .(type ) {
273
- case bsoncodec.ValueMarshaler :
274
- btype , val , err := t .MarshalBSONValue ()
275
- if err != nil {
276
- return nil , err
277
- }
278
- if btype != bsontype .Array {
279
- return nil , fmt .Errorf ("ValueMarshaler returned a %v, but was expecting %v" , btype , bsontype .Array )
280
- }
281
- err = pipelineArr .UnmarshalBSONValue (btype , val )
282
- if err != nil {
283
- return nil , err
284
- }
285
- default :
286
- val := reflect .ValueOf (t )
287
- if ! val .IsValid () || (val .Kind () != reflect .Slice && val .Kind () != reflect .Array ) {
288
- return nil , fmt .Errorf ("can only transform slices and arrays into aggregation pipelines, but got %v" , val .Kind ())
289
- }
290
- for idx := 0 ; idx < val .Len (); idx ++ {
291
- elem , err := transformDocument (registry , val .Index (idx ).Interface ())
292
- if err != nil {
293
- return nil , err
294
- }
295
- pipelineArr = append (pipelineArr , bsonx .Document (elem ))
296
- }
297
- }
298
-
299
- return pipelineArr , nil
300
- }
301
-
302
- func transformAggregatePipelinev2 (registry * bsoncodec.Registry , pipeline interface {}) (bsoncore.Document , bool , error ) {
199
+ func transformAggregatePipeline (registry * bsoncodec.Registry , pipeline interface {}) (bsoncore.Document , bool , error ) {
303
200
switch t := pipeline .(type ) {
304
201
case bsoncodec.ValueMarshaler :
305
202
btype , val , err := t .MarshalBSONValue ()
@@ -350,7 +247,7 @@ func transformAggregatePipelinev2(registry *bsoncodec.Registry, pipeline interfa
350
247
}
351
248
352
249
func transformUpdateValue (registry * bsoncodec.Registry , update interface {}, dollarKeysAllowed bool ) (bsoncore.Value , error ) {
353
- documentCheckerFunc := ensureDollarKeyv2
250
+ documentCheckerFunc := ensureDollarKey
354
251
if ! dollarKeysAllowed {
355
252
documentCheckerFunc = ensureNoDollarKey
356
253
}
0 commit comments