-
Notifications
You must be signed in to change notification settings - Fork 8
/
syncitemscursor.go
143 lines (109 loc) · 3.13 KB
/
syncitemscursor.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package v3io
import (
"errors"
)
var ErrInvalidTypeConversion = errors.New("Invalid type conversion")
type SyncItemsCursor struct {
currentItem Item
currentError error
currentResponse *Response
nextMarker string
moreItemsExist bool
itemIndex int
items []Item
input *GetItemsInput
container *SyncContainer
}
func newSyncItemsCursor(container *SyncContainer, input *GetItemsInput) (*SyncItemsCursor, error) {
newSyncItemsCursor := &SyncItemsCursor{
container: container,
input: input,
}
response, err := container.GetItems(input)
if err != nil {
return nil, err
}
newSyncItemsCursor.setResponse(response)
return newSyncItemsCursor, nil
}
// Err returns the last error
func (ic *SyncItemsCursor) Err() error {
return ic.currentError
}
// Release releases a cursor and its underlying resources
func (ic *SyncItemsCursor) Release() {
if ic.currentResponse != nil {
ic.currentResponse.Release()
}
}
// Next gets the next matching item. this may potentially block as this lazy loads items from the collection
func (ic *SyncItemsCursor) Next() bool {
item, err := ic.NextItem()
if item == nil || err != nil {
return false
}
return true
}
// NextItem gets the next matching item. this may potentially block as this lazy loads items from the collection
func (ic *SyncItemsCursor) NextItem() (Item, error) {
// are there any more items left in the previous response we received?
if ic.itemIndex < len(ic.items) {
ic.currentItem = ic.items[ic.itemIndex]
ic.currentError = nil
// next time we'll give next item
ic.itemIndex++
return ic.currentItem, nil
}
// are there any more items up stream?
if !ic.moreItemsExist {
ic.currentError = nil
return nil, nil
}
// get the previous request input and modify it with the marker
ic.input.Marker = ic.nextMarker
// invoke get items
newResponse, err := ic.container.GetItems(ic.input)
if err != nil {
return nil, err
}
// release the previous response
ic.currentResponse.Release()
// set the new response - read all the sub information from it
ic.setResponse(newResponse)
// and recurse into next now that we repopulated response
return ic.NextItem()
}
// gets all items
func (ic *SyncItemsCursor) All() ([]Item, error) {
var items []Item
for ic.Next() {
items = append(items, ic.GetItem())
}
if ic.Err() != nil {
return nil, ic.Err()
}
return items, nil
}
func (ic *SyncItemsCursor) GetField(name string) interface{} {
return ic.currentItem[name]
}
func (ic *SyncItemsCursor) GetFieldInt(name string) (int, error) {
return ic.currentItem.GetFieldInt(name)
}
func (ic *SyncItemsCursor) GetFieldString(name string) (string, error) {
return ic.currentItem.GetFieldString(name)
}
func (ic *SyncItemsCursor) GetFields() map[string]interface{} {
return ic.currentItem
}
func (ic *SyncItemsCursor) GetItem() Item {
return ic.currentItem
}
func (ic *SyncItemsCursor) setResponse(response *Response) {
ic.currentResponse = response
getItemsOutput := response.Output.(*GetItemsOutput)
ic.moreItemsExist = !getItemsOutput.Last
ic.nextMarker = getItemsOutput.NextMarker
ic.items = getItemsOutput.Items
ic.itemIndex = 0
}