-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlinter.go
67 lines (62 loc) · 1.46 KB
/
linter.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
package csvlint
import (
"encoding/csv"
"fmt"
"io"
)
// CSVError returns information about an invalid record in a CSV file
type CSVError struct {
// Record is the invalid record. This will be nil when we were unable to parse a record.
Record []string
// Num is the record number of this record.
Num int
err error
}
// Error implements the error interface
func (e CSVError) Error() string {
return fmt.Sprintf("Record #%d has error: %s", e.Num, e.err.Error())
}
// Validate tests whether or not a CSV lints according to RFC 4180.
// The lazyquotes option will attempt to parse lines that aren't quoted properly.
func Validate(reader io.Reader, delimiter rune, lazyquotes bool) ([]CSVError, bool, error) {
r := csv.NewReader(reader)
r.TrailingComma = true
r.FieldsPerRecord = -1
r.LazyQuotes = lazyquotes
r.Comma = delimiter
var header []string
errors := []CSVError{}
records := 0
for {
record, err := r.Read()
if header != nil {
records++
}
if err != nil {
if err == io.EOF {
break
}
parsedErr, ok := err.(*csv.ParseError)
if !ok {
return errors, true, err
}
errors = append(errors, CSVError{
Record: nil,
Num: records,
err: parsedErr.Err,
})
return errors, true, nil
}
if header == nil {
header = record
continue
} else if len(record) != len(header) {
errors = append(errors, CSVError{
Record: record,
Num: records,
err: csv.ErrFieldCount,
})
}
}
return errors, false, nil
}