-
Notifications
You must be signed in to change notification settings - Fork 11
/
ankit.go
58 lines (47 loc) · 953 Bytes
/
ankit.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
package ankit
import (
"encoding/csv"
"io"
)
// Reader is the interface that wraps the basic Read method.
type Reader interface {
Read() (fields []string, err error)
}
// Copy copies from src to dst until either EOF is reached on src or an error occurs.
func Copy(dst io.Writer, src Reader) error {
cw := csv.NewWriter(dst)
for {
fields, err := src.Read()
if err != nil {
if err == io.EOF {
break
}
return err
}
if err := cw.Write(fields); err != nil {
return err
}
}
cw.Flush()
return cw.Error()
}
// Note consists of fields.
type Note interface {
Fields() []string
}
type oneNoteReader struct {
Note
readed bool
}
// Read reads fields only once.
func (r *oneNoteReader) Read() ([]string, error) {
if r.readed {
return nil, io.EOF
}
r.readed = true
return r.Fields(), nil
}
// OneNoteReader returns a Reader with only one Note.
func OneNoteReader(n Note) Reader {
return &oneNoteReader{Note: n}
}