-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
55 lines (42 loc) · 1 KB
/
helpers.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
package filecache
import (
"errors"
"io"
"reflect"
"github.com/beeker1121/goque"
)
// DequeueAll dequeues all elements of type dest from q
// and stores it into dest.
func DequeueAll(q Queue, dest interface{}) error {
if dest == nil {
return errors.New("destination cannot be nil")
}
typ := reflect.TypeOf(dest)
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Slice {
return errors.New("destination is not pointer to slice")
}
outKind := typ.Elem().Elem().Kind()
for typ.Kind() == reflect.Ptr ||
typ.Kind() == reflect.Interface ||
typ.Kind() == reflect.Slice ||
typ.Kind() == reflect.Array {
typ = typ.Elem()
}
out := reflect.ValueOf(dest).Elem()
for {
vPtr := reflect.New(typ)
err := q.Dequeue(vPtr.Interface())
switch err {
case nil:
case io.EOF, goque.ErrEmpty, goque.ErrOutOfBounds:
return nil
default:
return err
}
if outKind == reflect.Ptr {
out.Set(reflect.Append(out, vPtr))
} else {
out.Set(reflect.Append(out, vPtr.Elem()))
}
}
}