-
Notifications
You must be signed in to change notification settings - Fork 181
/
iterators.go
169 lines (133 loc) · 4.17 KB
/
iterators.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package storage
import (
"errors"
openfgav1 "github.com/openfga/api/proto/openfga/v1"
)
var ErrIteratorDone = errors.New("iterator done")
type Iterator[T any] interface {
// Next will return the next available item.
Next() (T, error)
// Stop terminates iteration over the underlying iterator.
Stop()
}
// TupleIterator is an iterator for Tuples. It is closed by explicitly calling Stop() or by calling Next() until it
// returns an ErrIteratorDone error.
type TupleIterator = Iterator[*openfgav1.Tuple]
// TupleKeyIterator is an iterator for TupleKeys. It is closed by explicitly calling Stop() or by calling Next() until it
// returns an ErrIteratorDone error.
type TupleKeyIterator = Iterator[*openfgav1.TupleKey]
type emptyTupleIterator struct{}
var _ TupleIterator = (*emptyTupleIterator)(nil)
func (e *emptyTupleIterator) Next() (*openfgav1.Tuple, error) {
return nil, ErrIteratorDone
}
func (e *emptyTupleIterator) Stop() {}
type combinedIterator[T any] struct {
iters []Iterator[T]
}
func (c *combinedIterator[T]) Next() (T, error) {
for i, iter := range c.iters {
if iter == nil {
continue
}
val, err := iter.Next()
if err != nil {
if !errors.Is(err, ErrIteratorDone) {
return val, err
}
c.iters[i] = nil // end of this iterator
continue
}
return val, nil
}
// all iterators ended
var val T
return val, ErrIteratorDone
}
func (c *combinedIterator[T]) Stop() {
for _, iter := range c.iters {
if iter != nil {
iter.Stop()
}
}
}
// NewCombinedIterator takes generic iterators of a given type T and combines them into a single iterator that yields
// all the values from all iterators. Duplicates can be returned.
func NewCombinedIterator[T any](iters ...Iterator[T]) Iterator[T] {
return &combinedIterator[T]{iters}
}
// NewStaticTupleIterator returns a TupleIterator that iterates over the provided slice.
func NewStaticTupleIterator(tuples []*openfgav1.Tuple) TupleIterator {
iter := &staticIterator[*openfgav1.Tuple]{
items: tuples,
}
return iter
}
// NewStaticTupleKeyIterator returns a TupleKeyIterator that iterates over the provided slice.
func NewStaticTupleKeyIterator(tupleKeys []*openfgav1.TupleKey) TupleKeyIterator {
iter := &staticIterator[*openfgav1.TupleKey]{
items: tupleKeys,
}
return iter
}
type tupleKeyIterator struct {
iter TupleIterator
}
var _ TupleKeyIterator = (*tupleKeyIterator)(nil)
func (t *tupleKeyIterator) Next() (*openfgav1.TupleKey, error) {
tuple, err := t.iter.Next()
return tuple.GetKey(), err
}
func (t *tupleKeyIterator) Stop() {
t.iter.Stop()
}
// NewTupleKeyIteratorFromTupleIterator takes a TupleIterator and yields all of the TupleKeys from it as a TupleKeyIterator.
func NewTupleKeyIteratorFromTupleIterator(iter TupleIterator) TupleKeyIterator {
return &tupleKeyIterator{iter}
}
type staticIterator[T any] struct {
items []T
}
func (s *staticIterator[T]) Next() (T, error) {
var val T
if len(s.items) == 0 {
return val, ErrIteratorDone
}
next, rest := s.items[0], s.items[1:]
s.items = rest
return next, nil
}
func (s *staticIterator[T]) Stop() {}
// TupleKeyFilterFunc is a filter function that is used to filter out tuples from a TupleKey iterator
// that don't meet some criteria. Implementations should return true if the tuple should be returned
// and false if it should be filtered out.
type TupleKeyFilterFunc func(tupleKey *openfgav1.TupleKey) bool
type filteredTupleKeyIterator struct {
iter TupleKeyIterator
filter TupleKeyFilterFunc
}
var _ TupleKeyIterator = &filteredTupleKeyIterator{}
// Next returns the next most tuple in the underlying iterator that meets
// the filter function this iterator was constructed with.
func (f *filteredTupleKeyIterator) Next() (*openfgav1.TupleKey, error) {
for {
tuple, err := f.iter.Next()
if err != nil {
return nil, err
}
if f.filter(tuple) {
return tuple, nil
}
}
}
func (f *filteredTupleKeyIterator) Stop() {
f.iter.Stop()
}
// NewFilteredTupleKeyIterator returns an iterator that filters out all tuples that don't
// meet the conditions of the provided TupleFilterFunc.
func NewFilteredTupleKeyIterator(iter TupleKeyIterator, filter TupleKeyFilterFunc) TupleKeyIterator {
return &filteredTupleKeyIterator{
iter,
filter,
}
}