generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cursor.go
93 lines (75 loc) · 2.08 KB
/
cursor.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package mongoifc
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/mongo"
)
// Cursor is an interface for `mongo.Cursor` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Cursor
type Cursor interface {
Current() bson.Raw
All(ctx context.Context, results interface{}) error
Close(ctx context.Context) error
Decode(val interface{}) error
Err() error
ID() int64
Next(ctx context.Context) bool
RemainingBatchLength() int
SetBatchSize(batchSize int32)
SetComment(comment interface{})
SetMaxTime(dur time.Duration)
TryNext(ctx context.Context) bool
}
type cursor struct {
cr *mongo.Cursor
}
func (c *cursor) Current() bson.Raw {
return c.cr.Current
}
func (c *cursor) All(ctx context.Context, results interface{}) error {
return c.cr.All(ctx, results)
}
func (c *cursor) Close(ctx context.Context) error {
return c.cr.Close(ctx)
}
func (c *cursor) Decode(val interface{}) error {
return c.cr.Decode(val)
}
func (c *cursor) Err() error {
return c.cr.Err()
}
func (c *cursor) ID() int64 {
return c.cr.ID()
}
func (c *cursor) Next(ctx context.Context) bool {
return c.cr.Next(ctx)
}
func (c *cursor) RemainingBatchLength() int {
return c.cr.RemainingBatchLength()
}
func (c *cursor) SetComment(comment interface{}) {
c.cr.SetComment(comment)
}
func (c *cursor) SetMaxTime(dur time.Duration) {
c.cr.SetMaxTime(dur)
}
func (c *cursor) TryNext(ctx context.Context) bool {
return c.cr.TryNext(ctx)
}
func (c *cursor) SetBatchSize(batchSize int32) {
c.cr.SetBatchSize(batchSize)
}
func wrapCursor(cr *mongo.Cursor) Cursor {
return &cursor{cr: cr}
}
// NewCursorFromDocuments is a wrapper for NewCursorFromDocuments function of the mongodb to return Cursor
// https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#NewCursorFromDocuments
func NewCursorFromDocuments(documents []interface{}, err error, registry *bsoncodec.Registry) (Cursor, error) {
cr, err := mongo.NewCursorFromDocuments(documents, err, registry)
if err != nil {
return nil, err
}
return wrapCursor(cr), nil
}