forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_and_modify.go
49 lines (42 loc) · 1.41 KB
/
find_and_modify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package command
import (
"errors"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/result"
)
// unmarshalFindAndModifyResult turns the provided bson.Reader into a findAndModify result.
func unmarshalFindAndModifyResult(rdr bson.Reader) (result.FindAndModify, error) {
var res result.FindAndModify
val, err := rdr.Lookup("value")
switch {
case err == bson.ErrElementNotFound:
return result.FindAndModify{}, errors.New("invalid response from server, no value field")
case err != nil:
return result.FindAndModify{}, err
}
switch val.Value().Type() {
case bson.TypeNull:
case bson.TypeEmbeddedDocument:
res.Value = val.Value().ReaderDocument()
default:
return result.FindAndModify{}, errors.New("invalid response from server, 'value' field is not a document")
}
if val, err := rdr.Lookup("lastErrorObject", "updatedExisting"); err == nil {
b, ok := val.Value().BooleanOK()
if ok {
res.LastErrorObject.UpdatedExisting = b
}
}
if val, err := rdr.Lookup("lastErrorObject", "upserted"); err == nil {
oid, ok := val.Value().ObjectIDOK()
if ok {
res.LastErrorObject.Upserted = oid
}
}
return res, nil
}