-
Notifications
You must be signed in to change notification settings - Fork 10
/
parser.go
161 lines (141 loc) · 2.99 KB
/
parser.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
package tsv
import (
"encoding/csv"
"errors"
"golang.org/x/text/unicode/norm"
"io"
"reflect"
"strconv"
)
// Parser has information for parser
type Parser struct {
Headers []string
Reader *csv.Reader
Data interface{}
ref reflect.Value
indices []int // indices is field index list of header array
structMode bool
normalize norm.Form
}
// NewStructModeParser creates new TSV parser with given io.Reader as struct mode
func NewParser(reader io.Reader, data interface{}) (*Parser, error) {
r := csv.NewReader(reader)
r.Comma = '\t'
// first line should be fields
headers, err := r.Read()
if err != nil {
return nil, err
}
for i, header := range headers {
headers[i] = header
}
p := &Parser{
Reader: r,
Headers: headers,
Data: data,
ref: reflect.ValueOf(data).Elem(),
indices: make([]int, len(headers)),
structMode: false,
normalize: -1,
}
// get type information
t := p.ref.Type()
for i := 0; i < t.NumField(); i++ {
// get TSV tag
tsvtag := t.Field(i).Tag.Get("tsv")
if tsvtag != "" {
// find tsv position by header
for j := 0; j < len(headers); j++ {
if headers[j] == tsvtag {
// indices are 1 start
p.indices[j] = i + 1
p.structMode = true
}
}
}
}
if !p.structMode {
for i := 0; i < len(headers); i++ {
p.indices[i] = i + 1
}
}
return p, nil
}
// NewParserWithoutHeader creates new TSV parser with given io.Reader
func NewParserWithoutHeader(reader io.Reader, data interface{}) *Parser {
r := csv.NewReader(reader)
r.Comma = '\t'
p := &Parser{
Reader: r,
Data: data,
ref: reflect.ValueOf(data).Elem(),
normalize: -1,
}
return p
}
// Next puts reader forward by a line
func (p *Parser) Next() (eof bool, err error) {
// Get next record
var records []string
for {
// read until valid record
records, err = p.Reader.Read()
if err != nil {
if err.Error() == "EOF" {
return true, nil
}
return false, err
}
if len(records) > 0 {
break
}
}
if len(p.indices) == 0 {
p.indices = make([]int, len(records))
// mapping simple index
for i := 0; i < len(records); i++ {
p.indices[i] = i + 1
}
}
// record should be a pointer
for i, record := range records {
idx := p.indices[i]
if idx == 0 {
// skip empty index
continue
}
// get target field
field := p.ref.Field(idx - 1)
switch field.Kind() {
case reflect.String:
// Normalize text
if p.normalize >= 0 {
record = p.normalize.String(record)
}
field.SetString(record)
case reflect.Bool:
if record == "" {
field.SetBool(false)
} else {
col, err := strconv.ParseBool(record)
if err != nil {
return false, err
}
field.SetBool(col)
}
case reflect.Int:
if record == "" {
field.SetInt(0)
} else {
col, err := strconv.ParseInt(record, 10, 0)
if err != nil {
return false, err
}
field.SetInt(col)
}
default:
return false, errors.New("Unsupported field type")
}
}
return false, nil
}