forked from digisan/csv-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.go
More file actions
288 lines (255 loc) · 6.81 KB
/
scan.go
File metadata and controls
288 lines (255 loc) · 6.81 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package csvtool
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"
"strings"
. "github.com/digisan/go-generics"
fd "github.com/digisan/gotk/file-dir"
lk "github.com/digisan/logkit"
)
func CellEsc(cell string) string {
if len(cell) > 1 {
hasComma, hasDQ, hasLF := strings.Contains(cell, ","), strings.Contains(tryStripDQ(cell), "\""), strings.Contains(cell, "\n")
if hasDQ {
cell = strings.ReplaceAll(cell, "\"", "\"\"")
}
if hasComma || hasLF || hasDQ {
cell = tryWrapWithDQ(cell)
}
}
return cell
}
// Info : headers, nRow, error
func Info(r io.Reader) ([]string, int, error) {
rs, ok := r.(io.ReadSeeker)
if ok {
defer rs.Seek(0, io.SeekStart)
}
content, err := csv.NewReader(r).ReadAll()
if err != nil {
return nil, -1, err
}
if len(content) == 0 {
return []string{}, 0, nil
}
return content[0], len(content) - 1, nil
}
// CsvInfo : headers, nRow, error
func FileInfo(path string) ([]string, int, error) {
csvFile, err := os.Open(path)
if err != nil {
if csvFile != nil {
csvFile.Close()
}
return nil, 0, err
}
defer csvFile.Close()
return Info(csvFile)
}
func HeaderHasAll(r io.Reader, hdr ...string) (bool, error) {
headers, _, err := Info(r)
if err != nil {
return false, err
}
for _, h := range hdr {
if NotIn(h, headers...) {
return false, nil
}
}
return true, nil
}
func FileHeaderHasAll(path string, hdr ...string) (bool, error) {
csvFile, err := os.Open(path)
if err != nil {
if csvFile != nil {
csvFile.Close()
}
return false, err
}
defer csvFile.Close()
return HeaderHasAll(csvFile, hdr...)
}
func HeaderHasAny(r io.Reader, hdrs ...string) (bool, error) {
headers, _, err := Info(r)
if err != nil {
return false, err
}
for _, hdr := range hdrs {
if In(hdr, headers...) {
return true, nil
}
}
return false, nil
}
func FileHeaderHasAny(path string, hdr ...string) (bool, error) {
csvFile, err := os.Open(path)
if err != nil {
if csvFile != nil {
csvFile.Close()
}
return false, err
}
defer csvFile.Close()
return HeaderHasAny(csvFile, hdr...)
}
// CsvReader : streaming row-by-row processing. Malformed rows are skipped with a warning
// rather than aborting and leaving the output empty. n passed to callback is 0 (unknown
// ahead of time); callers must not rely on n being accurate.
func CsvReader(
r io.Reader,
f func(i, n int, headers, cells []string) (ok bool, hdr, row string),
keepOriHdr bool,
keepAnyRow bool,
w io.Writer,
) (string, []string, error) {
rs, ok := r.(io.ReadSeeker)
if ok {
defer rs.Seek(0, io.SeekStart)
}
reader := csv.NewReader(r)
reader.FieldsPerRecord = -1 // tolerate rows with varying field counts
// Read header row
rawHeader, err := reader.Read()
if err == io.EOF {
return "", []string{}, fmt.Errorf("FILE_EMPTY")
}
if err != nil {
return "", nil, err
}
headers := make([]string, 0, len(rawHeader))
for i, cell := range rawHeader {
if cell == "" {
cell = fmt.Sprintf("column_%d", i)
lk.WarnOnErr("%v: column[%d] is empty, marked [%s]", fmt.Errorf("CSV_COLUMN_HEADER_EMPTY"), i, cell)
}
headers = append(headers, CellEsc(cell))
}
hdrLine := strings.Join(headers, ",")
allRows := []string{}
// Pre-compute IsNil(w) once, outside the hot row loop.
// IsNil (from go-generics v0.5.4) falls back to fmt.Sprint(v) which
// serialises the entire writer struct via reflection on every call.
// For a *bufio.Writer with a 1 MiB buffer that means ~1 MB of reflect
// work per row: 2.3 M rows × 1 MB ≈ total freeze.
// Cache the result here so the loop pays O(1) per row.
wNotNil := !IsNil(w)
// If no callback: collect all rows and write them out
if f == nil {
rowIdx := 0
for {
rawRow, readErr := reader.Read()
if readErr == io.EOF {
break
}
if readErr != nil {
lk.Warn("CSV parse error at row %d: %v — row skipped", rowIdx+2, readErr)
rowIdx++
continue
}
allRows = append(allRows, strings.Join(rawRow, ","))
rowIdx++
}
if len(allRows) > 0 || keepOriHdr {
// hdrLine already set
} else {
hdrLine = ""
}
goto SAVE
}
// Streaming callback processing: write to w as each accepted row arrives.
// pendingHdr tracks the latest header value returned by callbacks; it is
// written to w just before the first accepted content row, so that Subset's
// column-filtered header is captured correctly even if early rows are skipped.
//
{
pendingHdr := hdrLine
wroteHdr := false
rowIdx := 0
for {
rawRow, readErr := reader.Read()
if readErr == io.EOF {
break
}
if readErr != nil {
lk.Warn("CSV parse error at row %d: %v — row skipped", rowIdx+2, readErr)
rowIdx++
continue
}
cbOk, hdr, row := f(rowIdx, 0, headers, rawRow)
rowIdx++
if cbOk {
if hdr != "" {
pendingHdr = hdr
}
if keepAnyRow || !isBlank(row) {
// Only accumulate allRows when NOT streaming to a writer.
// When wNotNil, the caller receives output via w; keeping
// 2.3 M row strings in allRows simultaneously doubles the
// memory footprint with no benefit — callers such as Query
// and QueryFile always discard the returned allRows.
if !wNotNil {
allRows = append(allRows, row)
}
if wNotNil {
if !wroteHdr {
if _, werr := fmt.Fprint(w, pendingHdr); werr != nil {
return "", nil, werr
}
wroteHdr = true
}
if _, werr := fmt.Fprintf(w, "\n%s", row); werr != nil {
return "", nil, werr
}
}
}
}
}
// Header-only file (no content rows passed the filter but keepOriHdr requested)
if wNotNil && !wroteHdr && keepOriHdr {
if _, werr := fmt.Fprint(w, pendingHdr); werr != nil {
return "", nil, werr
}
}
hdrLine = pendingHdr
return hdrLine, allRows, nil
}
SAVE:
if wNotNil {
data := []byte(strings.TrimSuffix(hdrLine+"\n"+strings.Join(allRows, "\n"), "\n"))
_, err = w.Write(data)
if err != nil {
return "", nil, err
}
}
return hdrLine, allRows, nil
}
// Scan : if [f arg: i==-1], it is pure HeaderRow csv
func Scan(in []byte, f func(i, n int, headers, cells []string) (ok bool, hdr, row string), keepOriHdr bool, w io.Writer) (string, []string, error) {
return CsvReader(bytes.NewReader(in), f, keepOriHdr, false, w)
}
// ScanFile :
func ScanFile(path string, f func(i, n int, headers, cells []string) (ok bool, hdr, row string), keepOriHdr bool, outPath string) (string, []string, error) {
fr, err := os.Open(path)
if err != nil {
return "", nil, err
}
defer fr.Close()
var fw *os.File = nil
if trimBlank(outPath) != "" {
fd.MustCreateDir(filepath.Dir(outPath))
fw, err = os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return "", nil, err
}
defer fw.Close()
}
hRow, rows, err := CsvReader(fr, f, keepOriHdr, false, fw)
if rows == nil && err != nil { // go internal csv func error
return "", nil, err
}
return hRow, rows, err
}