-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
74 lines (54 loc) · 1.84 KB
/
writer.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
package csvdict
import (
"encoding/csv"
"fmt"
"io"
"os"
)
// type Writer implements a `encoding/csv` style writer for CSV documents with named columns.
type Writer struct {
Writer *csv.Writer
Fieldnames []string
}
// NewWriter will return a new Writer that writes to wr using a set list of column names defined in fieldnames.
func NewWriter(wr io.Writer, fieldnames []string) (*Writer, error) {
writer := csv.NewWriter(wr)
dw := Writer{Writer: writer, Fieldnames: fieldnames}
return &dw, nil
}
// NewWriter will return a new Writer that writes to path using a set list of column names defined in fieldnames.
func NewWriterFromPath(path string, fieldnames []string) (*Writer, error) {
fh, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return nil, fmt.Errorf("Failed to open %s for writing, %w", path, err)
}
return NewWriter(fh, fieldnames)
}
// WriteHeader will write the CSV-encoded list of fieldnames passed to dw.
func (dw Writer) WriteHeader() error {
return dw.Writer.Write(dw.Fieldnames)
}
// to do - check flags for whether or not to be liberal when missing keys
// (20160516/thisisaaronland)
// WriteRow writes the values of row as CSV-encoded data. The order of those values is determined
// by their position defined in the list of fieldnames passed to dw.
func (dw Writer) WriteRow(row map[string]string) error {
out := make([]string, 0)
for _, k := range dw.Fieldnames {
v, ok := row[k]
if !ok {
v = ""
}
out = append(out, v)
}
return dw.Writer.Write(out)
}
// Flush writes any buffered data to the underlying writer. To check if an error occurred during the Flush, call Error.
func (dw Writer) Flush() error {
dw.Writer.Flush()
return nil
}
// Error reports any error that has occurred during a previous Write or Flush.
func (dw Writer) Error() error {
return dw.Writer.Error()
}