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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package logs
import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"io"
"strconv"
"strings"
)
type (
CSVConfig struct {
FieldsPerRecord int `yaml:"fields_per_record" json:"fields_per_record"`
Delimiter string `yaml:"delimiter" json:"delimiter"`
TrimLeadingSpace bool `yaml:"trim_leading_space" json:"trim_leading_space"`
Format string `yaml:"format" json:"format"`
CheckField func(string) (string, int, bool) `yaml:"-" json:"-"`
}
CSVParser struct {
Config CSVConfig
reader *csv.Reader
format *csvFormat
}
csvFormat struct {
raw string
maxIndex int
fields []csvField
}
csvField struct {
name string
idx int
}
)
func NewCSVParser(config CSVConfig, in io.Reader) (*CSVParser, error) {
if config.Format == "" {
return nil, errors.New("empty csv format")
}
format, err := newCSVFormat(config)
if err != nil {
return nil, fmt.Errorf("bad csv format '%s': %v", config.Format, err)
}
p := &CSVParser{
Config: config,
reader: newCSVReader(in, config),
format: format,
}
return p, nil
}
func (p *CSVParser) ReadLine(line LogLine) error {
record, err := p.reader.Read()
if err != nil {
return handleCSVReaderError(err)
}
return p.format.parse(record, line)
}
func (p *CSVParser) Parse(row []byte, line LogLine) error {
r := newCSVReader(bytes.NewBuffer(row), p.Config)
record, err := r.Read()
if err != nil {
return handleCSVReaderError(err)
}
return p.format.parse(record, line)
}
func (p CSVParser) Info() string {
return fmt.Sprintf("csv: %s", p.format.raw)
}
func (f *csvFormat) parse(record []string, line LogLine) error {
if len(record) <= f.maxIndex {
return &ParseError{msg: "csv parse: unmatched line"}
}
for _, v := range f.fields {
if err := line.Assign(v.name, record[v.idx]); err != nil {
return &ParseError{msg: fmt.Sprintf("csv parse: %v", err), err: err}
}
}
return nil
}
func newCSVReader(in io.Reader, config CSVConfig) *csv.Reader {
r := csv.NewReader(in)
if config.Delimiter != "" {
if d, err := parseCSVDelimiter(config.Delimiter); err == nil {
r.Comma = d
}
}
r.TrimLeadingSpace = config.TrimLeadingSpace
r.FieldsPerRecord = config.FieldsPerRecord
r.ReuseRecord = true
return r
}
func newCSVFormat(config CSVConfig) (*csvFormat, error) {
r := csv.NewReader(strings.NewReader(config.Format))
if config.Delimiter != "" {
if d, err := parseCSVDelimiter(config.Delimiter); err == nil {
r.Comma = d
}
}
r.TrimLeadingSpace = config.TrimLeadingSpace
record, err := r.Read()
if err != nil {
return nil, err
}
fields, err := createCSVFields(record, config.CheckField)
if err != nil {
return nil, err
}
if len(fields) == 0 {
return nil, errors.New("zero fields")
}
format := &csvFormat{
raw: config.Format,
maxIndex: fields[len(fields)-1].idx,
fields: fields,
}
return format, nil
}
func createCSVFields(format []string, check func(string) (string, int, bool)) ([]csvField, error) {
if check == nil {
check = checkCSVFormatField
}
var fields []csvField
var offset int
seen := make(map[string]bool)
for i, name := range format {
name = strings.Trim(name, `"`)
name, addOffset, valid := check(name)
offset += addOffset
if !valid {
continue
}
if seen[name] {
return nil, fmt.Errorf("duplicate field: %s", name)
}
seen[name] = true
idx := i + offset
fields = append(fields, csvField{name, idx})
}
return fields, nil
}
func handleCSVReaderError(err error) error {
if isCSVParseError(err) {
return &ParseError{msg: fmt.Sprintf("csv parse: %v", err), err: err}
}
return err
}
func isCSVParseError(err error) bool {
return errors.Is(err, csv.ErrBareQuote) || errors.Is(err, csv.ErrFieldCount) || errors.Is(err, csv.ErrQuote)
}
func checkCSVFormatField(name string) (newName string, offset int, valid bool) {
if len(name) < 2 || !strings.HasPrefix(name, "$") {
return "", 0, false
}
return name, 0, true
}
func parseCSVDelimiter(s string) (rune, error) {
if isNumber(s) {
d, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid CSV delimiter: %v", err)
}
return rune(d), nil
}
if len(s) != 1 {
return 0, errors.New("invalid CSV delimiter: must be a single character")
}
return rune(s[0]), nil
}
|