-
Notifications
You must be signed in to change notification settings - Fork 3
/
iterator.go
54 lines (43 loc) · 846 Bytes
/
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
package wordpress
import (
"errors"
)
type Iterator interface {
Next() (int64, error)
Cursor() string
Slice() ([]int64, error)
}
var (
Done = errors.New("wordpress: no more rows to read")
zeroIter = &iteratorImpl{next: func() (int64, error) {
return 0, Done
}}
)
type iteratorImpl struct {
next func() (int64, error)
cursor string
}
func (it *iteratorImpl) Next() (int64, error) {
return it.next()
}
func (it *iteratorImpl) Cursor() string {
return it.cursor
}
func (it *iteratorImpl) Slice() (ret []int64, err error) {
for {
var id int64
if id, err = it.Next(); err == Done {
break
} else if err != nil {
return nil, err
}
ret = append(ret, id)
}
return ret, nil
}
func (it *iteratorImpl) exit(err error) (int64, error) {
it.next = func() (int64, error) {
return 0, err
}
return it.next()
}