-
Notifications
You must be signed in to change notification settings - Fork 3
/
reader.go
240 lines (217 loc) · 4.69 KB
/
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
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
// based on encoding/csv
package ltsv
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"reflect"
"strings"
"unicode"
)
// A Reader reads records from a LTSV-encoded file.
//
// As returned by NewReader, a Reader expects input LTSV-encoded file.
// The exported fields can be changed to customize the details before the
// first call to Read or ReadAll.
//
// Delimiter is the field delimiter. It defaults to '\t'.
//
// Comment, if not 0, is the comment character. Lines beginning with the
// Comment character are ignored.
type Reader struct {
Delimiter rune // Field delimiter (set to '\t' by NewReader)
Comment rune // Comment character for start of line
line int
r *bufio.Reader
label bytes.Buffer
field bytes.Buffer
}
type structLabelIndexMap map[string]int
// NewReader returns a new Reader that reads from r.
func NewReader(r io.Reader) *Reader {
return &Reader{
Delimiter: '\t',
r: bufio.NewReader(r),
}
}
type recorder interface {
record(label, field string) error
len() int
}
type mapRecorder struct {
records map[string]string
}
func (r *mapRecorder) record(label, field string) error {
r.records[label] = field
return nil
}
func (r *mapRecorder) len() int {
return len(r.records)
}
type structRecorder struct {
count int
value reflect.Value
labels structLabelIndexMap
}
func (r *structRecorder) record(label, field string) error {
idx, ok := r.labels[label]
if !ok {
return nil
}
r.value.Field(idx).SetString(field)
r.count++
return nil
}
func (r *structRecorder) len() int {
return r.count
}
// Read reads one record from r. The record is a slice of strings with each
// string representing one field.
func (r *Reader) Read() (map[string]string, error) {
rec := mapRecorder{records: make(map[string]string)}
err := r.readRecord(&rec)
if err != nil {
return nil, err
}
return rec.records, nil
}
func (r *Reader) readRecord(rec recorder) error {
r.line++
for {
cm, err := r.readComment()
if err != nil {
return err
} else if cm {
continue
}
label, end, err := r.parseLabel()
if err != nil {
return err
}
if label == "" {
if end {
if rec.len() >= 1 {
return nil
}
}
continue // skip a empty line
}
field, end, err := r.parseField()
if err != nil {
return err
}
err = rec.record(label, field)
if err != nil {
return err
}
if end {
return nil
}
}
panic("unreachable")
}
func (r *Reader) readComment() (bool, error) {
// If we are support comments and it is the comment character
// then skip to the end of line.
r1, err := r.readRune()
if r.Comment != 0 && r1 == r.Comment {
for {
r1, err = r.readRune()
if err != nil {
return false, err
} else if r1 == '\n' {
return true, nil
}
}
}
r.r.UnreadRune()
return false, nil
}
func (r *Reader) Load(record interface{}) error {
v := reflect.ValueOf(record)
k := v.Kind()
if (k != reflect.Ptr && k != reflect.Interface) || v.IsNil() {
return ErrUnsupportedType
}
e := v.Elem()
rec := structRecorder{value: e, labels: structLabelIndex(e)}
err := r.readRecord(&rec)
if err != nil {
return err
}
return nil
}
func (r *Reader) parseLabel() (string, bool, error) {
r.label.Reset()
for {
r1, err := r.readRune()
if err != nil {
return "", false, err
} else if r1 == ':' {
return strings.TrimSpace(r.label.String()), false, nil
} else if r1 == '\n' {
return "", true, nil
} else if r1 == '\t' {
return "", false, nil // no label
} else if unicode.IsControl(r1) || !unicode.IsPrint(r1) {
return "", false, errors.New(fmt.Sprintf("line %d: invalid rune at label", r.line))
}
r.label.WriteRune(r1)
}
panic("unreachable")
}
func (r *Reader) parseField() (string, bool, error) {
r.field.Reset()
for {
r1, err := r.readRune()
if err != nil {
if err == io.EOF {
return r.field.String(), true, nil
}
return "", false, err
} else if r1 == '\t' {
return r.field.String(), false, nil
} else if r1 == '\n' {
return r.field.String(), true, nil
}
r.field.WriteRune(r1)
}
panic("unreachable")
}
func (r *Reader) readRune() (rune, error) {
r1, _, err := r.r.ReadRune()
if r1 == '\r' {
r1, _, err = r.r.ReadRune()
if err == nil {
if r1 != '\n' {
r.r.UnreadRune()
r1 = '\r'
}
}
}
return r1, err
}
func (r *Reader) ReadAll() ([]map[string]string, error) {
records := make([]map[string]string, 0)
for {
record, err := r.Read()
if err == io.EOF {
return records, nil
}
if err != nil {
return nil, err
}
records = append(records, record)
}
panic("unreachable")
}
func structLabelIndex(v reflect.Value) structLabelIndexMap {
sil := structIndexLabel(v)
sli := make(structLabelIndexMap)
for i, l := range sil {
sli[l] = i
}
return sli
}