forked from a8m/documentdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
55 lines (48 loc) · 1.54 KB
/
iterator.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
package documentdb
// Iterator allows easily fetch multiple result sets when response max item limit is reacheds
type Iterator struct {
continuationToken string
err error
response *Response
next bool
source IteratorFunc
db *DocumentDB
}
// NewIterator creates iterator instance
func NewIterator(db *DocumentDB, source IteratorFunc) *Iterator {
return &Iterator{
source: source,
db: db,
next: true,
}
}
// Response returns *Response object from last call
func (di *Iterator) Response() *Response {
return di.response
}
// Errror returns error from last call
func (di *Iterator) Error() error {
return di.err
}
// Next will ask iterator source for results and checks whenever there some more pages left
func (di *Iterator) Next() bool {
if !di.next {
return false
}
di.response, di.err = di.source(di.db, Continuation(di.continuationToken))
if di.err != nil {
return false
}
di.continuationToken = di.response.Continuation()
next := di.next
di.next = di.continuationToken != ""
return next
}
// IteratorFunc is type that describes iterator source
type IteratorFunc func(db *DocumentDB, internalOpts ...CallOption) (*Response, error)
// NewDocumentIterator creates iterator source for fetching documents
func NewDocumentIterator(coll string, query *Query, docs interface{}, opts ...CallOption) IteratorFunc {
return func(db *DocumentDB, internalOpts ...CallOption) (*Response, error) {
return db.QueryDocuments(coll, query, docs, append(opts, internalOpts...)...)
}
}