-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
reader.go
252 lines (219 loc) · 5.59 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
241
242
243
244
245
246
247
248
249
250
251
252
package trdsql
import (
"io"
"log"
"sync"
)
// extToFormat is a map of file extensions to formats.
var extToFormat map[string]Format = map[string]Format{
"CSV": CSV,
"LTSV": LTSV,
"JSON": JSON,
"JSONL": JSON,
"YAML": YAML,
"YML": YAML,
"TBLN": TBLN,
"TSV": TSV,
"PSV": PSV,
"WIDTH": WIDTH,
"TEXT": TEXT,
}
// ReaderFunc is a function that creates a new Reader.
type ReaderFunc func(io.Reader, *ReadOpts) (Reader, error)
// readerFuncs maps formats to their corresponding ReaderFunc.
var readerFuncs = map[Format]ReaderFunc{
CSV: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewCSVReader(reader, opts)
},
LTSV: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewLTSVReader(reader, opts)
},
JSON: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewJSONReader(reader, opts)
},
YAML: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewYAMLReader(reader, opts)
},
TBLN: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewTBLNReader(reader, opts)
},
TSV: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewTSVReader(reader, opts)
},
PSV: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewPSVReader(reader, opts)
},
WIDTH: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewGWReader(reader, opts)
},
TEXT: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewTextReader(reader, opts)
},
}
var (
// extFormat is the next format number to be assigned.
extFormat Format = 100
// registerMux is a mutex to protect access to the register.
registerMux = &sync.Mutex{}
)
func RegisterReaderFunc(ext string, readerFunc ReaderFunc) {
registerMux.Lock()
defer registerMux.Unlock()
extToFormat[ext] = extFormat
readerFuncs[extFormat] = readerFunc
extFormat++
}
// Reader is wrap the reader.
// Reader reads from tabular files.
type Reader interface {
// Names returns column names.
Names() ([]string, error)
// Types returns column types.
Types() ([]string, error)
// PreReadRow is returns only columns that store preRead rows.
PreReadRow() [][]any
// ReadRow is read the rest of the row.
ReadRow(row []any) ([]any, error)
}
// ReadOpts represents options that determine the behavior of the reader.
type ReadOpts struct {
// InDelimiter is the field delimiter.
// default is ','
InDelimiter string
// InNULL is a string to replace with NULL.
InNULL string
// InJQuery is a jq expression.
InJQuery string
// InFormat is read format.
// The supported format is CSV/LTSV/JSON/TBLN.
InFormat Format
realFormat Format
// InPreRead is number of rows to read ahead.
// CSV/LTSV reads the specified number of rows to
// determine the number of columns.
InPreRead int
// InSkip is number of rows to skip.
// Skip reading specified number of lines.
InSkip int
// InLimitRead is limit read.
InLimitRead bool
// InHeader is true if there is a header.
// It is used as a column name.
InHeader bool
// InNeedNULL is true, replace InNULL with NULL.
InNeedNULL bool
// IsTemporary is a flag whether to make temporary table.
// default is true.
IsTemporary bool
// InRowNumber is row number.
InRowNumber bool
}
// NewReadOpts Returns ReadOpts.
func NewReadOpts(options ...ReadOpt) *ReadOpts {
readOpts := &ReadOpts{
InFormat: GUESS,
InPreRead: 1,
InLimitRead: false,
InSkip: 0,
InDelimiter: ",",
InHeader: false,
IsTemporary: true,
InJQuery: "",
InNeedNULL: false,
InNULL: "",
}
for _, option := range options {
option(readOpts)
}
return readOpts
}
// ReadOpt returns a *ReadOpts structure.
// Used when calling NewImporter.
type ReadOpt func(*ReadOpts)
// InFormat is read format.
func InFormat(f Format) ReadOpt {
return func(args *ReadOpts) {
args.InFormat = f
}
}
// InPreRead is number of lines to read ahead.
func InPreRead(p int) ReadOpt {
return func(args *ReadOpts) {
args.InPreRead = p
}
}
func InLimitRead(p bool) ReadOpt {
return func(args *ReadOpts) {
args.InLimitRead = p
}
}
// InJQ is jq expression.
func InJQ(p string) ReadOpt {
return func(args *ReadOpts) {
args.InJQuery = p
}
}
// InSkip is number of lines to skip.
func InSkip(s int) ReadOpt {
return func(args *ReadOpts) {
args.InSkip = s
}
}
// InDelimiter is the field delimiter.
func InDelimiter(d string) ReadOpt {
return func(args *ReadOpts) {
args.InDelimiter = d
}
}
// InHeader is true if there is a header.
func InHeader(h bool) ReadOpt {
return func(args *ReadOpts) {
args.InHeader = h
}
}
// InNeedNULL sets a flag as to whether it should be replaced with NULL.
func InNeedNULL(n bool) ReadOpt {
return func(args *ReadOpts) {
args.InNeedNULL = n
}
}
// In NULL is a string to replace with NULL.
func InNULL(s string) ReadOpt {
return func(args *ReadOpts) {
args.InNULL = s
}
}
// IsTemporary is a flag whether to make temporary table.
func IsTemporary(t bool) ReadOpt {
return func(args *ReadOpts) {
args.IsTemporary = t
}
}
func InRowNumber(t bool) ReadOpt {
return func(args *ReadOpts) {
args.InRowNumber = t
}
}
// NewReader returns an Reader interface
// depending on the file to be imported.
func NewReader(reader io.Reader, readOpts *ReadOpts) (Reader, error) {
if reader == nil {
return nil, ErrNoReader
}
readerFunc, ok := readerFuncs[readOpts.realFormat]
if !ok {
return nil, ErrUnknownFormat
}
return readerFunc(reader, readOpts)
}
func skipRead(r Reader, skipNum int) {
skip := make([]any, 1)
for i := 0; i < skipNum; i++ {
row, err := r.ReadRow(skip)
if err != nil {
log.Printf("ERROR: skip error %s", err)
break
}
debug.Printf("Skip row:%s\n", row)
}
}