-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
71 lines (55 loc) · 1.03 KB
/
common_test.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
package iterator
import "fmt"
type Item struct {
ID int
Name string
}
type Items []Item
func (a Items) Iterator() Iterator {
return NewCloseableIterator(next(a), func() error {
return nil
})
}
func next(items []Item) ComputeNext {
index := 0
return func() (interface{}, bool, error) {
if index >= len(items) {
return nil, true, nil
}
n := &items[index]
index++
return n, false, nil
}
}
func nextAndIndex(items []Item) (ComputeNext, *int) {
index := 0
return func() (interface{}, bool, error) {
if index >= len(items) {
return nil, true, nil
}
n := &items[index]
index++
return n, false, nil
}, &index
}
func generateItems(from int, to int) []Item {
var items []Item
for from < to {
items = append(items, Item{
ID: from,
Name: fmt.Sprintf("item_%04d", from),
})
from++
}
return items
}
func itemsFromIds(ids ...int) []Item {
var items []Item
for _, id := range ids {
items = append(items, Item{
ID: id,
Name: fmt.Sprintf("item_%04d", id),
})
}
return items
}