Repository navigation
Expand file tree
/
Copy pathresult.go
More file actions
76 lines (68 loc) · 1.97 KB
/
Copy pathresult.go
File metadata and controls
76 lines (68 loc) · 1.97 KB
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
package fp
import (
"reflect"
"strings"
)
func (q *stream) ToSlice(dst interface{}) error {
val := reflect.ValueOf(dst)
if val.Kind() != reflect.Ptr {
panic(`fp: dst must be pointer`)
}
val.Elem().Set(q.getValue(val.Elem()))
return q.ctx.Err()
}
func (q *stream) JoinStrings(seq string) string {
return strings.Join(q.Strings(), seq)
}
func (q *stream) getResult() Value {
v := Value{
typ: reflect.SliceOf(q.expectElemTyp),
val: q.getValue(reflect.Value{}),
}
v.err = q.ctx.Err()
return v
}
func (q *stream) Run() {
q.getValOnce.Do(func() {
for {
if _, ok := q.iter(); !ok {
break
}
}
})
}
func (q *stream) Error() error {
if q.expectElemTyp.AssignableTo(errType) {
return q.SkipWhile(NoError()).First().Err()
}
q.Run()
return q.ctx.Err()
}
func (q *stream) getValue(slice reflect.Value) reflect.Value {
q.getValOnce.Do(func() {
if !slice.IsValid() {
slice = reflect.Zero(reflect.SliceOf(q.expectElemTyp))
} else if slice.Len() > 0 {
slice = reflect.MakeSlice(reflect.SliceOf(q.expectElemTyp), 0, slice.Len())
}
for {
if val, ok := q.iter(); ok {
slice = reflect.Append(slice, val)
} else {
break
}
}
q.val = slice
})
return q.val
}
func (q *stream) Strings() (s []string) { return q.getResult().Strings() }
func (q *stream) Ints() (s []int) { return q.getResult().Ints() }
func (q *stream) Int64s() (s []int64) { return q.getResult().Int64s() }
func (q *stream) Int32s() (s []int32) { return q.getResult().Int32s() }
func (q *stream) Uints() (s []uint) { return q.getResult().Uints() }
func (q *stream) Uint32s() (s []uint32) { return q.getResult().Uint32s() }
func (q *stream) Uint64s() (s []uint64) { return q.getResult().Uint64s() }
func (q *stream) Bytes() (s []byte) { return q.getResult().Bytes() }
func (q *stream) Float64s() (s []float64) { q.getResult().To(&s); return }
func (q *stream) StringsList() (s [][]string) { return q.getResult().StringsList() }