Skip to content

Commit 4760305

Browse files
authored
GODRIVER-1180 Remove legacy transform functions from mongo (#583)
1 parent 899e5cd commit 4760305

File tree

5 files changed

+160
-176
lines changed

5 files changed

+160
-176
lines changed

mongo/bulk_write.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (bw *bulkWrite) runInsert(ctx context.Context, batch bulkWriteBatch) (opera
168168
var i int
169169
for _, model := range batch.models {
170170
converted := model.(*InsertOneModel)
171-
doc, _, err := transformAndEnsureIDv2(bw.collection.registry, converted.Document)
171+
doc, _, err := transformAndEnsureID(bw.collection.registry, converted.Document)
172172
if err != nil {
173173
return operation.InsertResult{}, err
174174
}

mongo/collection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func (coll *Collection) insert(ctx context.Context, documents []interface{},
244244

245245
for i, doc := range documents {
246246
var err error
247-
docs[i], result[i], err = transformAndEnsureIDv2(coll.registry, doc)
247+
docs[i], result[i], err = transformAndEnsureID(coll.registry, doc)
248248
if err != nil {
249249
return nil, err
250250
}
@@ -746,7 +746,7 @@ func aggregate(a aggregateParams) (*Cursor, error) {
746746
a.ctx = context.Background()
747747
}
748748

749-
pipelineArr, hasOutputStage, err := transformAggregatePipelinev2(a.registry, a.pipeline)
749+
pipelineArr, hasOutputStage, err := transformAggregatePipeline(a.registry, a.pipeline)
750750
if err != nil {
751751
return nil, err
752752
}

mongo/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func (db *Database) CreateCollection(ctx context.Context, name string, opts ...*
572572
func (db *Database) CreateView(ctx context.Context, viewName, viewOn string, pipeline interface{},
573573
opts ...*options.CreateViewOptions) error {
574574

575-
pipelineArray, _, err := transformAggregatePipelinev2(db.registry, pipeline)
575+
pipelineArray, _, err := transformAggregatePipeline(db.registry, pipeline)
576576
if err != nil {
577577
return err
578578
}

mongo/mongo.go

Lines changed: 6 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -72,70 +72,9 @@ func (me MarshalError) Error() string {
7272
//
7373
type Pipeline []bson.D
7474

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) {
13978
if registry == nil {
14079
registry = bson.NewRegistryBuilder().Build()
14180
}
@@ -237,17 +176,7 @@ func ensureID(d bsonx.Doc) (bsonx.Doc, interface{}) {
237176
return d, id
238177
}
239178

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 {
251180
firstElem, err := doc.IndexErr(0)
252181
if err != nil {
253182
return errors.New("update document must have at least one element")
@@ -267,39 +196,7 @@ func ensureNoDollarKey(doc bsoncore.Document) error {
267196
return nil
268197
}
269198

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) {
303200
switch t := pipeline.(type) {
304201
case bsoncodec.ValueMarshaler:
305202
btype, val, err := t.MarshalBSONValue()
@@ -350,7 +247,7 @@ func transformAggregatePipelinev2(registry *bsoncodec.Registry, pipeline interfa
350247
}
351248

352249
func transformUpdateValue(registry *bsoncodec.Registry, update interface{}, dollarKeysAllowed bool) (bsoncore.Value, error) {
353-
documentCheckerFunc := ensureDollarKeyv2
250+
documentCheckerFunc := ensureDollarKey
354251
if !dollarKeysAllowed {
355252
documentCheckerFunc = ensureNoDollarKey
356253
}

0 commit comments

Comments
 (0)