forked from mkideal/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoders.go
365 lines (330 loc) · 6.36 KB
/
decoders.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
package ext
import (
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"unsafe"
)
// Time wrap time.Time
type Time struct {
time.Time
isSet bool
}
var timeFormats = []string{
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
"2006-01-02",
"2006/01/02",
"2006:01:02",
"15:04:05",
"2006-01-02 15:04:05",
"2006/01/02 15:04:05",
"2006:01:02 15:04:05",
"15:04:05 2006-01-02",
"15:04:05 2006/01/02",
"15:04:05 2006:01:02",
}
func (t *Time) Decode(s string) error {
now := time.Now()
if s == "" {
t.Time = now
return nil
}
for _, format := range timeFormats {
v, err := time.Parse(format, s)
if err == nil {
newYear, newMonth, newDay := v.Year(), v.Month(), v.Day()
reset := false
if newYear == 0 {
reset = true
newYear = now.Year()
newMonth = now.Month()
newDay = now.Day()
}
if reset {
v = time.Date(newYear, newMonth, newDay, v.Hour(), v.Minute(), v.Second(), v.Nanosecond(), v.Location())
}
t.Time = v
t.isSet = true
return nil
}
}
return fmt.Errorf("unsupported time format")
}
func (t Time) Encode() string {
return t.Format(time.RFC3339Nano)
}
func (t Time) IsSet() bool {
return t.isSet
}
// Duration wrap time.Duration
type Duration struct {
time.Duration
}
func (d *Duration) Decode(s string) error {
if i, err := strconv.ParseUint(s, 10, 64); err == nil {
d.Duration = time.Duration(i) * time.Second
return nil
}
v, err := time.ParseDuration(s)
if err != nil {
return err
}
d.Duration = v
return nil
}
func (d Duration) Encode() string {
return d.Duration.String()
}
// File reads data from file or stdin(if filename is empty)
type File struct {
filename string
data []byte
}
func (f File) Data() []byte {
return f.data
}
func (f File) String() string {
if f.data == nil {
return ""
}
return string(f.data)
}
func (f *File) Decode(s string) error {
var (
data []byte
err error
)
if s == "" {
data, err = ioutil.ReadAll(os.Stdin)
} else {
data, err = ioutil.ReadFile(s)
}
if err != nil {
return err
}
f.data = data
f.filename = s
return nil
}
func (f File) Encode() string {
return f.filename
}
// Reader
type Reader struct {
reader io.Reader
filename string
}
func (r *Reader) Decode(s string) error {
if s == "" {
r.reader = os.Stdin
r.filename = os.Stdin.Name()
} else if strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://") {
response, err := http.Get(s)
if err != nil {
return err
}
r.reader = response.Body
r.filename = s
} else {
r.filename = s
file, err := os.Open(s)
if err != nil {
return err
}
r.reader = file
}
return nil
}
// SetReader replaces the native reader
func (r *Reader) SetReader(reader io.Reader) {
r.Close()
r.reader = reader
if file, ok := reader.(*os.File); ok {
r.filename = file.Name()
} else {
r.filename = ""
}
}
// Read implementes io.Reader
func (r Reader) Read(data []byte) (n int, err error) {
if r.reader == nil {
return os.Stdin.Read(data)
}
return r.reader.Read(data)
}
func (r Reader) Close() error {
if r.reader != nil && !r.IsStdin() {
if c, ok := r.reader.(io.Closer); ok {
return c.Close()
}
}
return nil
}
func (r Reader) Name() string {
if r.reader == nil {
return os.Stdin.Name()
}
return r.filename
}
func (r Reader) IsStdin() bool {
if r.reader == nil {
return true
}
if stdin, ok := r.reader.(*os.File); ok {
return uintptr(unsafe.Pointer(stdin)) == uintptr(unsafe.Pointer(os.Stdin))
}
return false
}
// Writer
type Writer struct {
writer io.Writer
filename string
}
func (w *Writer) Decode(s string) error {
if w.writer != nil {
return nil
}
if s == "" {
w.writer = os.Stdout
w.filename = os.Stdout.Name()
return nil
}
w.filename = s
return nil
}
// SetWriter replaces the native writer
func (w *Writer) SetWriter(writer io.Writer) {
w.Close()
w.writer = writer
if file, ok := w.writer.(*os.File); ok {
w.filename = file.Name()
} else {
w.filename = ""
}
}
// Write implementes io.Writer interface
func (w *Writer) Write(data []byte) (n int, err error) {
if w.writer == nil {
if w.filename == "" {
w.writer = os.Stdout
w.filename = os.Stdout.Name()
} else {
file, err := os.Create(w.filename)
if err != nil {
return 0, err
}
w.writer = file
}
}
return w.writer.Write(data)
}
func (w *Writer) Close() error {
if w.writer != nil && !w.IsStdout() {
if c, ok := w.writer.(io.Closer); ok {
return c.Close()
}
}
return nil
}
func (w *Writer) Name() string {
if w.writer == nil {
return os.Stdout.Name()
}
return w.filename
}
func (w Writer) IsStdout() bool {
if w.writer == nil {
return true
}
if stdout, ok := w.writer.(*os.File); ok {
return uintptr(unsafe.Pointer(stdout)) == uintptr(unsafe.Pointer(os.Stdout))
}
return false
}
// CSV reads one csv record
type CSVRecord struct {
raw []string
}
func (d *CSVRecord) Decode(s string) error {
reader := csv.NewReader(strings.NewReader(s))
record, err := reader.Read()
if err != nil {
return err
}
d.raw = record
return nil
}
func (d CSVRecord) Strings() []string {
return d.raw
}
func (d CSVRecord) Bools() ([]bool, error) {
ret := make([]bool, len(d.raw))
for _, s := range d.raw {
s = strings.ToLower(s)
if s == "y" || s == "yes" || s == "true" {
ret = append(ret, true)
} else if s == "n" || s == "no" || s == "false" {
ret = append(ret, false)
} else {
v, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("parse %s to boolean fail", s)
}
ret = append(ret, v != 0)
}
}
return ret, nil
}
func (d CSVRecord) Ints() ([]int64, error) {
ret := make([]int64, len(d.raw))
for _, s := range d.raw {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil, err
}
ret = append(ret, v)
}
return ret, nil
}
func (d CSVRecord) Uints() ([]uint64, error) {
ret := make([]uint64, len(d.raw))
for _, s := range d.raw {
v, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return nil, err
}
ret = append(ret, v)
}
return ret, nil
}
func (d CSVRecord) Floats() ([]float64, error) {
ret := make([]float64, len(d.raw))
for _, s := range d.raw {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return nil, err
}
ret = append(ret, v)
}
return ret, nil
}