-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcsv.go
128 lines (117 loc) · 2.59 KB
/
csv.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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"io/ioutil"
"strconv"
)
type CSVRow struct {
Iteration int64
Query string
Seconds float64
}
func (r *CSVRow) UnmarshalRecord(record []string) error {
for i, val := range record {
if err := csvColumns[i].UnmarshalColumn(val, r); err != nil {
return err
}
}
return nil
}
func (r *CSVRow) MarshalRecord() ([]string, error) {
record := make([]string, len(csvColumns))
for i, col := range csvColumns {
val, err := col.MarshalColumn(r)
if err != nil {
return nil, err
}
record[i] = val
}
return record, nil
}
type csvColumn struct {
Name string
UnmarshalColumn func(string, *CSVRow) error
MarshalColumn func(*CSVRow) (string, error)
}
var csvColumns = []csvColumn{
{
"iteration",
func(val string, r *CSVRow) (err error) {
r.Iteration, err = strconv.ParseInt(val, 10, 64)
return
},
func(r *CSVRow) (string, error) {
return fmt.Sprintf("%d", r.Iteration), nil
},
},
{
"query",
func(val string, r *CSVRow) error {
r.Query = val
return nil
},
func(r *CSVRow) (string, error) {
return r.Query, nil
},
},
{
"seconds",
func(val string, r *CSVRow) (err error) {
r.Seconds, err = strconv.ParseFloat(val, 64)
return
},
func(r *CSVRow) (string, error) {
return fmt.Sprintf("%f", r.Seconds), nil
},
},
}
// csvHeader returns the CSV header columns.
func csvHeader() []string {
header := make([]string, len(csvColumns))
for i, col := range csvColumns {
header[i] = col.Name
}
return header
}
func loadCSVRows(csvPath string) ([]*CSVRow, error) {
data, err := ioutil.ReadFile(csvPath)
if err != nil {
return nil, err
}
cr := csv.NewReader(bytes.NewBuffer(data))
records, err := cr.ReadAll()
if err != nil {
return nil, err
}
var rows []*CSVRow
for i, record := range records {
if err := checkCSVColumns(record); err != nil {
return nil, fmt.Errorf("row=%d: %w", i+1, err)
}
switch i {
case 0:
for i, got := range record {
if want := csvColumns[i].Name; got != want {
return nil, fmt.Errorf("unexpected header column %d: got=%q want=%q", i, got, want)
}
}
default:
row := &CSVRow{}
if err := row.UnmarshalRecord(record); err != nil {
return nil, fmt.Errorf("row=%d: %w", i+1, err)
}
rows = append(rows, row)
}
}
return rows, nil
}
// checkCSVColumns returns an error if record doesn't have the right number
// of columns.
func checkCSVColumns(record []string) error {
if got, want := len(record), len(csvColumns); got != want {
return fmt.Errorf("bad number of columns: got=%d want=%d", got, want)
}
return nil
}