-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
iter.go
49 lines (41 loc) · 774 Bytes
/
iter.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
package gojq
// Iter is an interface for an iterator.
type Iter interface {
Next() (any, bool)
}
// NewIter creates a new [Iter] from values.
func NewIter(values ...any) Iter {
switch len(values) {
case 0:
return emptyIter{}
case 1:
return &unitIter{value: values[0]}
default:
iter := sliceIter(values)
return &iter
}
}
type emptyIter struct{}
func (emptyIter) Next() (any, bool) {
return nil, false
}
type unitIter struct {
value any
done bool
}
func (iter *unitIter) Next() (any, bool) {
if iter.done {
return nil, false
}
iter.done = true
return iter.value, true
}
type sliceIter []any
func (iter *sliceIter) Next() (any, bool) {
if len(*iter) == 0 {
return nil, false
}
value := (*iter)[0]
*iter = (*iter)[1:]
return value, true
}