-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathresult.go
72 lines (57 loc) · 1.39 KB
/
result.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package lungo
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"github.com/256dpi/lungo/bsonkit"
)
// ErrNoDocuments is returned by SingleResult if not document has been found.
// The value is the same as mongo.ErrNoDocuments and can be used interchangeably.
var ErrNoDocuments = mongo.ErrNoDocuments
var _ ISingleResult = &SingleResult{}
// SingleResult wraps a result to be mongo compatible.
type SingleResult struct {
doc bsonkit.Doc
err error
}
// Decode implements the ISingleResult.Decode method.
func (r *SingleResult) Decode(out interface{}) error {
// check error
if r.err != nil {
return r.err
}
// check document
if r.doc == nil {
return ErrNoDocuments
}
// decode document
return bsonkit.Decode(r.doc, out)
}
// DecodeBytes implements the ISingleResult.DecodeBytes method.
func (r *SingleResult) DecodeBytes() (bson.Raw, error) {
// check error
if r.err != nil {
return nil, r.err
}
// check document
if r.doc == nil {
return nil, ErrNoDocuments
}
// marshal document
return bson.Marshal(r.doc)
}
// Err implements the ISingleResult.Err method.
func (r *SingleResult) Err() error {
// check error
if r.err != nil {
return r.err
}
// check document
if r.doc == nil {
return ErrNoDocuments
}
return nil
}
// Raw implements the ISingleResult.Raw method.
func (r *SingleResult) Raw() (bson.Raw, error) {
return r.DecodeBytes()
}