-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
96 lines (85 loc) · 1.83 KB
/
stream.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
package tuna
import (
"encoding/csv"
"io"
"os"
)
// A Stream returns Rows one by one until it's source is depleted.
type Stream chan ErrRow
// NewStream returns a Stream from a slice of Rows. It is mainly here for
// demonstration and testing purposes.
func NewStream(rows ...Row) Stream {
s := make(Stream)
go func() {
for _, r := range rows {
s <- ErrRow{r, nil}
}
close(s)
}()
return s
}
// NewFuncStream returns a Stream that calls function n times and returns the
// resulting Rows.
func NewFuncStream(f func() Row, n uint) Stream {
s := make(Stream)
go func() {
for i := uint(0); i < n; i++ {
s <- ErrRow{f(), nil}
}
close(s)
}()
return s
}
// NewCSVStream returns a Stream from an io.Reader that reads strings that are
// assumed to CSV-parsable.
func NewCSVStream(reader io.Reader) (Stream, error) {
csvr := csv.NewReader(reader)
// The first row is assumed to contain the column names
cols, err := csvr.Read()
if err != nil {
return nil, err
}
s := make(Stream)
go func() {
for {
r, err := csvr.Read()
if err != nil {
if err == io.EOF {
break
}
s <- ErrRow{nil, err}
}
row := make(Row)
for i, name := range cols {
row[name] = r[i]
}
s <- ErrRow{row, nil}
}
close(s)
}()
return s, nil
}
// NewCSVStreamFromPath returns a Stream from a CSV file.
func NewCSVStreamFromPath(path string) (Stream, error) {
file, err := os.Open(path)
if err != nil {
file.Close()
return nil, err
}
return NewCSVStream(file)
}
// ZipStreams returns a Stream that iterates over multiple streams one by one.
// This is quite convinient for going through a dataset which has been split
// into multiple parts.
func ZipStreams(ss ...Stream) Stream {
zs := make(Stream)
go func() {
for _, s := range ss {
for r := range s {
zs <- r
}
}
close(zs)
}()
return zs
}