-
Notifications
You must be signed in to change notification settings - Fork 32
/
decode.go
408 lines (363 loc) · 9.98 KB
/
decode.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package fixedwidth
import (
"bufio"
"bytes"
"encoding"
"errors"
"io"
"reflect"
"strconv"
"strings"
)
var (
// ErrTooLong indicates a line was too long to decode. Currently, the maximum
// decodable line length is bufio.MaxScanTokenSize-1.
ErrTooLong = bufio.ErrTooLong
)
// Unmarshal parses fixed width encoded data and stores the
// result in the value pointed to by v. If v is nil or not a
// pointer, Unmarshal returns an InvalidUnmarshalError.
func Unmarshal(data []byte, v interface{}) error {
return NewDecoder(bytes.NewReader(data)).Decode(v)
}
// A Decoder reads and decodes fixed width data from an input stream.
type Decoder struct {
scanner *bufio.Scanner
lineTerminator []byte
done bool
useCodepointIndices bool
lastType reflect.Type
lastValuSetter valueSetter
}
// NewDecoder returns a new decoder that reads from r.
func NewDecoder(r io.Reader) *Decoder {
dec := &Decoder{
scanner: bufio.NewScanner(r),
lineTerminator: []byte("\n"),
}
dec.scanner.Split(dec.scan)
return dec
}
// An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
// (The argument to Unmarshal must be a non-nil pointer.)
type InvalidUnmarshalError struct {
Type reflect.Type
}
func (e *InvalidUnmarshalError) Error() string {
if e.Type == nil {
return "fixedwidth: Unmarshal(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "fixedwidth: Unmarshal(non-pointer " + e.Type.String() + ")"
}
return "fixedwidth: Unmarshal(nil " + e.Type.String() + ")"
}
// An UnmarshalTypeError describes a value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
Value string // the raw value
Type reflect.Type // type of Go value it could not be assigned to
Struct string // name of the struct type containing the field
Field string // name of the field holding the Go value
Cause error // original error
}
func (e *UnmarshalTypeError) Error() string {
var s string
if e.Struct != "" || e.Field != "" {
s = "fixedwidth: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String()
} else {
s = "fixedwidth: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
}
if e.Cause != nil {
return s + ":" + e.Cause.Error()
}
return s
}
// SetUseCodepointIndices configures `Decoder` on whether the indices in the
// `fixedwidth` struct tags are expressed in terms of bytes (the default
// behavior) or in terms of UTF-8 decoded codepoints.
func (d *Decoder) SetUseCodepointIndices(use bool) {
d.useCodepointIndices = use
}
// Decode reads from its input and stores the decoded data to the value
// pointed to by v.
//
// In the case that v points to a struct value, Decode will read a
// single line from the input. If there is no data remaining in the file,
// returns io.EOF
//
// In the case that v points to a slice value, Decode will read until
// the end of its input.
//
// Currently, the maximum decodable line length is bufio.MaxScanTokenSize-1. ErrTooLong
// is returned if a line is encountered that too long to decode.
func (d *Decoder) Decode(v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}
if rv.Elem().Kind() == reflect.Slice {
return d.readLines(rv.Elem())
}
err, ok := d.readLine(rv)
if d.done && err == nil && !ok {
// d.done means we've reached the end of the file. err == nil && !ok
// indicates that there was no data to read, so we propagate an io.EOF
// upwards so our caller knows there is no data left.
return io.EOF
}
return err
}
func (d *Decoder) readLines(v reflect.Value) (err error) {
ct := v.Type().Elem()
for {
nv := reflect.New(ct).Elem()
err, ok := d.readLine(nv)
if err != nil {
return err
}
if ok {
v.Set(reflect.Append(v, nv))
}
if d.done {
break
}
}
return nil
}
// SetLineTerminator sets the character(s) that will be used to terminate lines.
//
// The default value is "\n".
func (d *Decoder) SetLineTerminator(lineTerminator []byte) {
if len(lineTerminator) > 0 {
d.lineTerminator = lineTerminator
}
}
func (d *Decoder) scan(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, d.lineTerminator); i >= 0 {
// We have a full newline-terminated line.
return i + len(d.lineTerminator), data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
// readLine reads the next line of data. False is returned if there is no remaining data
// to read.
func (d *Decoder) readLine(v reflect.Value) (err error, ok bool) {
ok = d.scanner.Scan()
if !ok {
if d.scanner.Err() != nil {
return d.scanner.Err(), false
}
d.done = true
return nil, false
}
line := string(d.scanner.Bytes())
rawValue, err := newRawValue(line, d.useCodepointIndices)
if err != nil {
return
}
t := v.Type()
if t == d.lastType {
return d.lastValuSetter(v, rawValue), true
}
valueSetter := newValueSetter(t)
d.lastType = t
d.lastValuSetter = valueSetter
return valueSetter(v, rawValue), true
}
func rawValueFromLine(value rawValue, startPos, endPos int, format format) rawValue {
var trimFunc func(string) string
switch format.alignment {
case left:
trimFunc = func(s string) string {
return strings.TrimRight(s, string(format.padChar))
}
case right:
trimFunc = func(s string) string {
return strings.TrimLeft(s, string(format.padChar))
}
default:
trimFunc = func(s string) string {
return strings.Trim(s, string(format.padChar))
}
}
if value.codepointIndices != nil {
if len(value.codepointIndices) == 0 || startPos > len(value.codepointIndices) {
return rawValue{data: ""}
}
var relevantIndices []int
var lineData string
if endPos >= len(value.codepointIndices) {
relevantIndices = value.codepointIndices[startPos-1:]
lineData = value.data[relevantIndices[0]:]
} else {
relevantIndices = value.codepointIndices[startPos-1 : endPos]
lineData = value.data[relevantIndices[0]:value.codepointIndices[endPos]]
}
return rawValue{
data: trimFunc(lineData),
codepointIndices: relevantIndices,
}
} else {
if len(value.data) == 0 || startPos > len(value.data) {
return rawValue{data: ""}
}
if endPos > len(value.data) {
endPos = len(value.data)
}
return rawValue{
data: trimFunc(value.data[startPos-1 : endPos]),
}
}
}
type valueSetter func(v reflect.Value, raw rawValue) error
var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
func newValueSetter(t reflect.Type) valueSetter {
if t.Implements(textUnmarshalerType) {
return textUnmarshalerSetter(t, false)
}
if reflect.PtrTo(t).Implements(textUnmarshalerType) {
return textUnmarshalerSetter(t, true)
}
switch t.Kind() {
case reflect.Ptr:
return ptrSetter(t)
case reflect.Interface:
return interfaceSetter
case reflect.Struct:
return structSetter(t)
case reflect.String:
return stringSetter
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
return intSetter
case reflect.Float32:
return floatSetter(32)
case reflect.Float64:
return floatSetter(64)
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
return uintSetter
case reflect.Bool:
return boolSetter
}
return unknownSetter
}
func structSetter(t reflect.Type) valueSetter {
spec := cachedStructSpec(t)
return func(v reflect.Value, raw rawValue) error {
for i, fieldSpec := range spec.fieldSpecs {
if !fieldSpec.ok {
continue
}
rawValue := rawValueFromLine(raw, fieldSpec.startPos, fieldSpec.endPos, fieldSpec.format)
err := fieldSpec.setter(v.Field(i), rawValue)
if err != nil {
sf := t.Field(i)
return &UnmarshalTypeError{raw.data, sf.Type, t.Name(), sf.Name, err}
}
}
return nil
}
}
func unknownSetter(v reflect.Value, raw rawValue) error {
return errors.New("fixedwidth: unknown type")
}
func nilSetter(v reflect.Value, _ rawValue) error {
if v.IsNil() {
return nil
}
if v.CanSet() {
v.Set(reflect.Zero(v.Type()))
return nil
}
v = reflect.Indirect(v)
if v.CanSet() {
v.Set(reflect.Zero(v.Type()))
return nil
}
return nil
}
func textUnmarshalerSetter(t reflect.Type, shouldAddr bool) valueSetter {
return func(v reflect.Value, raw rawValue) error {
if shouldAddr {
v = v.Addr()
}
// set to zero value if this is nil
if t.Kind() == reflect.Ptr && v.IsNil() {
v.Set(reflect.New(t.Elem()))
}
return v.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(raw.data))
}
}
func interfaceSetter(v reflect.Value, raw rawValue) error {
return newValueSetter(v.Elem().Type())(v.Elem(), raw)
}
func ptrSetter(t reflect.Type) valueSetter {
innerSetter := newValueSetter(t.Elem())
return func(v reflect.Value, raw rawValue) error {
if len(raw.data) <= 0 {
return nilSetter(v, raw)
}
if v.IsNil() {
v.Set(reflect.New(t.Elem()))
}
return innerSetter(reflect.Indirect(v), raw)
}
}
func stringSetter(v reflect.Value, raw rawValue) error {
v.SetString(raw.data)
return nil
}
func intSetter(v reflect.Value, raw rawValue) error {
if len(raw.data) < 1 {
return nil
}
i, err := strconv.Atoi(raw.data)
if err != nil {
return err
}
v.SetInt(int64(i))
return nil
}
func floatSetter(bitSize int) valueSetter {
return func(v reflect.Value, raw rawValue) error {
if len(raw.data) < 1 {
return nil
}
f, err := strconv.ParseFloat(raw.data, bitSize)
if err != nil {
return err
}
v.SetFloat(f)
return nil
}
}
func uintSetter(v reflect.Value, raw rawValue) error {
if len(raw.data) < 1 {
return nil
}
i, err := strconv.ParseUint(raw.data, 10, 64)
if err != nil {
return err
}
v.SetUint(uint64(i))
return nil
}
func boolSetter(v reflect.Value, raw rawValue) error {
if len(raw.data) == 0 {
return nil
}
val, err := strconv.ParseBool(raw.data)
if err != nil {
return err
}
v.SetBool(val)
return nil
}