-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsvparser.go
273 lines (239 loc) · 8.3 KB
/
csvparser.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
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
package csvparser
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"strings"
)
// ParserFunc is the callback that will be called at each column parsing/reading
// The value parameter is the column value, and the destination is the struct to add values from the parsing
type ParserFunc[ReadTo any] func(value string, destination *ReadTo) error
// AfterParsingRowFunc is a callback/hook that will run after each row is parsed.
type AfterParsingRowFunc[ReadTo any] func(parsedObject ReadTo)
// OnErrorFunc is a callback that will run after every parsing error.
type OnErrorFunc func(row []string, err error)
// CsvParser is the internal object that will keep all the references needed to parse the file
type CsvParser[ReadTo any] struct {
fileReader *csv.Reader
columnParsers map[string]ParserFunc[ReadTo]
onError OnErrorFunc
afterParsingHook AfterParsingRowFunc[ReadTo]
headers []string
onFinish func()
onStart func()
terminateOnParsingError bool
}
// NewCsvParserFromBytes instantiates a new CsvParser from a []byte input
// The *headers parameter are necessary if your .csv file doesn't contain headers
// by default. Adding headers to the constructor will make the parser know what to handle.
func NewCsvParserFromBytes[ReadTo any](input []byte, headers ...string) *CsvParser[ReadTo] {
reader := bytes.NewReader(input)
return NewCsvParserFromReader[ReadTo](reader, headers...)
}
// NewCsvParserFromReader instantiates a new CsvParser from an io.Reader directly.
// Useful when reading from multipart files.
// The *headers parameter are necessary if your .csv file doesn't contain headers
// by default. Adding headers to the constructor will make the parser know what to handle.
func NewCsvParserFromReader[ReadTo any](input io.Reader, headers ...string) *CsvParser[ReadTo] {
return &CsvParser[ReadTo]{
fileReader: csv.NewReader(input),
headers: headers,
columnParsers: map[string]ParserFunc[ReadTo]{},
}
}
// TerminateOnParsingError sets a flag to finish the parsing if a single row throws an error.
// if flag is set to false, it will continue to parse and skip the record with the error.
func (c *CsvParser[ReadTo]) TerminateOnParsingError() *CsvParser[ReadTo] {
c.terminateOnParsingError = true
return c
}
// OnParseError sets a callback that is supposed to be run after a row has a parsing error
func (c *CsvParser[ReadTo]) OnParseError(callback OnErrorFunc) *CsvParser[ReadTo] {
c.onError = callback
return c
}
// AfterEachParsingHook adds a handler that will run after every single parsing
func (c *CsvParser[ReadTo]) AfterEachParsingHook(handler AfterParsingRowFunc[ReadTo]) *CsvParser[ReadTo] {
c.afterParsingHook = handler
return c
}
// OnFinish adds a handler that will run at the end of the file parsing.
func (c *CsvParser[ReadTo]) OnFinish(handler func()) *CsvParser[ReadTo] {
c.onFinish = handler
return c
}
// OnStart adds a handler that will run at the start of the file parsing.
func (c *CsvParser[ReadTo]) OnStart(handler func()) *CsvParser[ReadTo] {
c.onStart = handler
return c
}
// AddColumnParser adds a parser for each column to the internal parser list
func (c *CsvParser[ReadTo]) AddColumnParser(headerName string, parser ParserFunc[ReadTo]) *CsvParser[ReadTo] {
c.columnParsers[headerName] = parser
return c
}
// Parse returns an array of the object to return ([]ReadTo) from the input data and parsers provided.
func (c *CsvParser[ReadTo]) Parse() ([]ReadTo, error) {
c.runOnStart()
err := c.prepareHeaders()
if err != nil {
return []ReadTo{}, err
}
res, err := c.parseResults()
c.runOnFinish()
return res, err
}
// prepareHeaders verifies if the headers and parsers are matched. If the headers are not passed in the constructor,
// it will load the headers from the file data.
func (c *CsvParser[ReadTo]) prepareHeaders() error {
if c.areHeadersEmpty() {
return c.loadHeadersFromFile()
}
header, existsUnparsableHeader := c.isThereAnUnparsableHeader()
if existsUnparsableHeader {
return newUnparsableHeaderErr(header)
}
return nil
}
// areHeadersEmpty checks if the headers are empty
func (c *CsvParser[ReadTo]) areHeadersEmpty() bool {
return len(c.headers) == 0
}
// areHeadersAndParsersMatched makes sure that each header has an equivalent parser.
func (c *CsvParser[ReadTo]) isThereAnUnparsableHeader() (string, bool) {
for _, header := range c.headers {
if !c.existsParserForHeader(header) {
return header, true
}
}
return "", false
}
// existsParserForHeader verifies if there is a parser defined for a specific header
func (c *CsvParser[ReadTo]) existsParserForHeader(header string) bool {
_, ok := c.getParserFor(header)
return ok
}
// loadHeadersFromFile reads the first row in the file and loads it into the headers
func (c *CsvParser[ReadTo]) loadHeadersFromFile() error {
headers, err := c.fileReader.Read()
if err != nil {
return parseError{Msg: fmt.Sprintf("couldn't read headers from file: %s", err.Error())}
}
return c.loadHeaders(headers)
}
// loadHeaders loads a set of headers into the struct.
func (c *CsvParser[ReadTo]) loadHeaders(headers []string) error {
for _, header := range headers {
if err := c.loadHeader(header); err != nil {
return err
}
}
return nil
}
// loadHeader loads one header into the struct. If it is not able to be parsed
// (doesn't have an associated parser), it will return an error.
func (c *CsvParser[ReadTo]) loadHeader(header string) error {
header = strings.Trim(header, " ")
if !c.isHeaderAbleToBeParsed(header) {
return newUnparsableHeaderErr(header)
}
c.headers = append(c.headers, header)
return nil
}
// isHeaderAbleToBeParsed verifies if there is a correspondent parser for said header.
func (c *CsvParser[ReadTo]) isHeaderAbleToBeParsed(header string) bool {
_, ok := c.getParserFor(header)
return ok
}
// getParserFor gets a parser for a specific header.
func (c *CsvParser[ReadTo]) getParserFor(header string) (ParserFunc[ReadTo], bool) {
res, ok := c.columnParsers[header]
return res, ok
}
// parseResults returns the slice of objects to be parsed from the .csv file.
func (c *CsvParser[ReadTo]) parseResults() ([]ReadTo, error) {
result := make([]ReadTo, 0)
for {
object, err := c.readRowAndParseObject()
if err == io.EOF {
break
}
if err != nil {
if !c.terminateOnParsingError {
continue
}
return []ReadTo{}, newparseError(err)
}
result = append(result, *object)
}
return result, nil
}
// readRowAndParseObject reads a file row and parses it into an object.
func (c *CsvParser[ReadTo]) readRowAndParseObject() (*ReadTo, error) {
row, err := c.fileReader.Read()
if err != nil {
return nil, err
}
return c.parseRow(row)
}
// parseRow parses a single row into the target object. Runs the hook for the object if success.
func (c *CsvParser[ReadTo]) parseRow(row []string) (*ReadTo, error) {
object := new(ReadTo)
err := c.parseColumns(row, object)
if err != nil {
c.runOnError(row, err)
return nil, err
}
c.runAfterParsingHook(object)
return object, nil
}
// runOnError runs the onError callback.
func (c *CsvParser[ReadTo]) runOnError(row []string, err error) {
if c.onErrorExists() {
c.onError(row, err)
}
}
func (c *CsvParser[ReadTo]) onErrorExists() bool {
return c.onError != nil
}
// runHook runs the hook that is set up in the struct
func (c *CsvParser[ReadTo]) runAfterParsingHook(object *ReadTo) {
if c.afterParsingHookExists() {
c.afterParsingHook(*object)
}
}
func (c *CsvParser[ReadTo]) afterParsingHookExists() bool {
return c.afterParsingHook != nil
}
// parseColumns parses all the columns into a destination object.
func (c *CsvParser[ReadTo]) parseColumns(row []string, destination *ReadTo) error {
for i, columnValue := range row {
err := c.parseColumn(columnValue, c.headers[i], destination)
if err != nil {
return err
}
}
return nil
}
// parseColumn parses a single column. Uses columnParsers from the columnHeader to do it.
func (c *CsvParser[ReadTo]) parseColumn(columnValue, columnHeader string, destination *ReadTo) error {
parser, ok := c.getParserFor(columnHeader)
if !ok {
return newUnparsableHeaderErr(columnHeader)
}
if err := parser(columnValue, destination); err != nil {
return err
}
return nil
}
func (c *CsvParser[ReadTo]) runOnStart() {
if c.onStart != nil {
c.onStart()
}
}
func (c *CsvParser[ReadTo]) runOnFinish() {
if c.onFinish != nil {
c.onFinish()
}
}