-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_reader.go
81 lines (63 loc) · 1.25 KB
/
csv_reader.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
package testmatrix
import (
"encoding/csv"
"io"
)
type csvReader struct {
r *csv.Reader
firstRowRead bool
}
func NewCsvReader(r *csv.Reader) Reader {
return &csvReader{r, false}
}
func (reader *csvReader) ReadHeaders() (Headers, error) {
var headers Headers
r := reader.r
if reader.firstRowRead {
return headers, InvalidPositionErr
}
csvRow, err := r.Read()
if err == io.EOF {
return headers, EOF
}
if err != nil {
return headers, err
}
reader.firstRowRead = true
headers = make(Headers, len(csvRow)-1)
for index, header := range csvRow {
if index == 0 {
continue
}
headers[index-1] = header
}
return headers, nil
}
func (reader *csvReader) Read() (ResultsRow, error) {
results := ResultsRow{}
r := reader.r
if !reader.firstRowRead {
return results, InvalidPositionErr
}
csvRow, err := r.Read()
if err == io.EOF {
return results, EOF
}
if err != nil {
return results, err
}
expecteds := make([]bool, len(csvRow)-1)
results.Expected = expecteds
for index, resultText := range csvRow {
if index == 0 {
results.Subject = resultText
continue
}
expected, ok := StringToBool(resultText)
if !ok {
return results, UnparsableErr
}
expecteds[index-1] = expected
}
return results, nil
}