-
Notifications
You must be signed in to change notification settings - Fork 1
/
pagable.go
66 lines (61 loc) · 1.76 KB
/
pagable.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
package gocosmosdb
import "errors"
// NewPagableQuery - Creates a pagable query that populates the passed docs interface
func (c *CosmosDB) NewPagableQuery(coll string, query *QueryWithParameters, limit int, docs interface{}, opts ...CallOption) *PagableQuery {
return &PagableQuery{
client: c,
coll: coll,
query: query,
limit: Limit(limit),
offset: 0,
docs: docs,
opts: opts,
}
}
func (q *PagableQuery) doQuery(coll string, query *QueryWithParameters, docs interface{}, opts ...CallOption) (*Response, error) {
data := struct {
Documents interface{} `json:"Documents,omitempty"`
Count int `json:"_count,omitempty"`
}{Documents: docs}
if query != nil {
return q.client.client.queryWithParameters(coll+"docs/", query, &data, opts...)
}
return nil, errors.New("QueryWithParameters cannot be nil")
}
// Next - marshals the next page of docs into the passed interface
func (q *PagableQuery) Next() error {
if q.offset > 0 {
opts := append(q.opts, q.limit)
opts = append(opts, q.continuation)
opts = append(opts, q.sessionToken)
resp, err := q.doQuery(q.coll, q.query, q.docs, opts...)
if err != nil {
return err
}
q.offset = q.offset + 1
if resp.Continuation() != "" {
q.continuation = Continuation(resp.Continuation())
} else {
q.done = true
}
}
if q.offset == 0 {
opts := append(q.opts, q.limit)
resp, err := q.doQuery(q.coll, q.query, q.docs, opts...)
if err != nil {
return err
}
q.offset = q.offset + 1
if resp.Continuation() != "" {
q.continuation = Continuation(resp.Continuation())
} else {
q.done = true
}
q.sessionToken = SessionToken(resp.SessionToken())
}
return nil
}
// Done - returns true if no more pages are available
func (q *PagableQuery) Done() bool {
return q.done
}