/
range.go
46 lines (40 loc) · 1.02 KB
/
range.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
package iterator
import (
"fmt"
"io"
"sync/atomic"
)
// RangeIterator returns value sequentially from start to end
type RangeIterator struct {
start int64
end int64
step int64
current int64
}
// NewRangeIterator creates a new range iterator to step through values from a start to end with step
func NewRangeIterator(start, end, step int64) *RangeIterator {
return &RangeIterator{
start: start,
end: end,
step: step,
current: start - 1,
}
}
// IsBound return true if the iterator is bound
func (i *RangeIterator) IsBound() bool {
return true
}
// GetNext returns the next value from the range
func (i *RangeIterator) GetNext() (line []byte, input interface{}, err error) {
nextValue := atomic.AddInt64(&i.current, i.step)
if nextValue > int64(i.end) {
err = io.EOF
} else {
line = []byte(fmt.Sprintf("%d", nextValue))
}
return line, nextValue, err
}
// MarshalJSON return the value in a json compatible value
func (i *RangeIterator) MarshalJSON() (line []byte, err error) {
return MarshalJSON(i)
}