forked from CloudyKit/jet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranger.go
179 lines (147 loc) · 3.74 KB
/
ranger.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
170
171
172
173
174
175
176
177
178
179
package jet
import (
"errors"
"fmt"
"reflect"
"sync"
)
// Ranger describes an interface for types that iterate over something.
// Implementing this interface means the ranger will be used when it's
// encountered on the right hand side of a range's "let" expression.
type Ranger interface {
// Range calls should return a key, a value and a done bool to indicate
// whether there are more values to be generated.
//
// When the done flag is true, then the loop ends.
Range() (reflect.Value, reflect.Value, bool)
// ProvidesIndex should return true if keys are produced during Range()
// calls. This call should be idempotent across Range() calls (i.e.
// its return value must not change during an iteration).
ProvidesIndex() bool
}
type intsRanger struct {
i, val, to int64
}
var _ Ranger = &intsRanger{}
func (r *intsRanger) Range() (index, value reflect.Value, end bool) {
r.i++
r.val++
end = r.val == r.to
// The indirection in the ValueOf calls avoids an allocation versus
// using the concrete value of 'i' and 'val'. The downside is having
// to interpret 'r.i' as "the current value" after Range() returns,
// and so it needs to be initialized as -1.
index = reflect.ValueOf(&r.i).Elem()
value = reflect.ValueOf(&r.val).Elem()
return
}
func (r *intsRanger) ProvidesIndex() bool { return true }
func newIntsRanger(from, to int64) *intsRanger {
r := &intsRanger{
to: to,
i: -1,
val: from - 1,
}
return r
}
type pooledRanger interface {
Ranger
Setup(reflect.Value)
}
type sliceRanger struct {
v reflect.Value
i int
}
var _ Ranger = &sliceRanger{}
var _ pooledRanger = &sliceRanger{}
func (r *sliceRanger) Setup(v reflect.Value) {
r.i = 0
r.v = v
}
func (r *sliceRanger) Range() (index, value reflect.Value, end bool) {
if r.i == r.v.Len() {
end = true
return
}
index = reflect.ValueOf(r.i)
value = r.v.Index(r.i)
r.i++
return
}
func (r *sliceRanger) ProvidesIndex() bool { return true }
type mapRanger struct {
iter *reflect.MapIter
hasMore bool
}
var _ Ranger = &mapRanger{}
var _ pooledRanger = &mapRanger{}
func (r *mapRanger) Setup(v reflect.Value) {
r.iter = v.MapRange()
r.hasMore = r.iter.Next()
}
func (r *mapRanger) Range() (key, value reflect.Value, end bool) {
if !r.hasMore {
end = true
return
}
key, value = r.iter.Key(), r.iter.Value()
r.hasMore = r.iter.Next()
return
}
func (r *mapRanger) ProvidesIndex() bool { return true }
type chanRanger struct {
v reflect.Value
}
var _ Ranger = &chanRanger{}
var _ pooledRanger = &chanRanger{}
func (r *chanRanger) Setup(v reflect.Value) {
r.v = v
}
func (r *chanRanger) Range() (_, value reflect.Value, end bool) {
v, ok := r.v.Recv()
value, end = v, !ok
return
}
func (r *chanRanger) ProvidesIndex() bool { return false }
// ranger pooling
var (
poolSliceRanger = &sync.Pool{
New: func() interface{} {
return new(sliceRanger)
},
}
poolsByKind = map[reflect.Kind]*sync.Pool{
reflect.Slice: poolSliceRanger,
reflect.Array: poolSliceRanger,
reflect.Map: &sync.Pool{
New: func() interface{} {
return new(mapRanger)
},
},
reflect.Chan: &sync.Pool{
New: func() interface{} {
return new(chanRanger)
},
},
}
)
func getRanger(v reflect.Value) (r Ranger, cleanup func(), err error) {
if !v.IsValid() {
return nil, nil, errors.New("can't range over invalid value")
}
t := v.Type()
if t.Implements(rangerType) {
return v.Interface().(Ranger), func() { /* no cleanup needed */ }, nil
}
v, isNil := indirect(v)
if isNil {
return nil, nil, fmt.Errorf("cannot range over nil pointer/interface (%s)", t)
}
pool, ok := poolsByKind[v.Kind()]
if !ok {
return nil, nil, fmt.Errorf("value %v (type %s) is not rangeable", v, t)
}
pr := pool.Get().(pooledRanger)
pr.Setup(v)
return pr, func() { pool.Put(pr) }, nil
}