-
Notifications
You must be signed in to change notification settings - Fork 12
/
parse.go
103 lines (89 loc) · 2.28 KB
/
parse.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
97
98
99
100
101
102
103
package csv
import (
"encoding/csv"
"fmt"
"io"
"os"
"sync"
)
// ParseCsvFile takes the full path to a .csv file and parses it. Each line yielded gets fed to an
// output channel that is expected to "do stuff" with it. The csv file MUST:
// * have a header row
// * be valid csv (consistent number of columns per row)
// * be newline delimited
func ParseCsvFile(filepath string) (output chan map[string]string, err error) {
output = make(chan map[string]string)
f, err := os.Open(filepath)
if err != nil {
err = fmt.Errorf("failed to open file: %s: %w", filepath, err)
return
}
reader := csv.NewReader(f)
header, err := reader.Read()
if err != nil {
err = fmt.Errorf("failed to read csv file: %s: %w", filepath, err)
return
}
go func() {
defer f.Close() // this needs to be done here, or will result in error reading a closed file
for {
line, err := reader.Read()
if err == io.EOF {
break // reached EOF
} else if err != nil {
panic(err) // This is likely an unrecoverable error, so just quit
}
if len(line) != len(header) {
// Log the error. consider using a struct with err atttribute, or
// list of errors, to track overall errors
fmt.Println("[ERROR] incorrect number of columns in line:", line)
continue
}
data := make(map[string]string, len(line))
for columnNumber, item := range line {
data[header[columnNumber]] = item
}
output <- data
}
close(output) // Calling close here ensures workers exit properly
}()
return
}
func WriteCsvFile(
filename string,
headers []string,
input <-chan []string,
) (wg *sync.WaitGroup, err error) {
f, err := os.Create(filename)
if err != nil {
err = fmt.Errorf("failed to open file for writing: %s: %w", filename, err)
return
}
err = f.Truncate(0)
if err != nil {
f.Close()
err = fmt.Errorf("file is not writable: %w", err)
return
}
csvWriter := csv.NewWriter(f)
err = csvWriter.Write(headers)
if err != nil {
err = fmt.Errorf("could not write csv header: %w", err)
return
}
wg = new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
defer f.Close()
defer csvWriter.Flush()
for record := range input {
err = csvWriter.Write(record)
if err != nil {
err = fmt.Errorf("could not write csv line: %w", err)
return
}
}
}()
return
}