-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
607 lines (482 loc) · 10.8 KB
/
value.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package flagx
import (
"flag"
"os"
"strconv"
"strings"
"time"
)
type generic interface {
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64 | string |
bool | time.Duration | *os.File
}
func parseInt(v string, bitSize int, callback func(r int64)) error {
r, err := strconv.ParseInt(v, 10, bitSize)
if err != nil {
return err
}
callback(r)
return nil
}
type intValue int
func (i *intValue) Set(v string) error {
return parseInt(v, 0, func(r int64) {
*i = intValue(r)
})
}
func (i *intValue) String() string {
return strconv.FormatInt(int64(*i), 10)
}
type int8Value int8
func (i *int8Value) Set(v string) error {
return parseInt(v, 8, func(r int64) {
*i = int8Value(r)
})
}
func (i *int8Value) String() string {
return strconv.FormatInt(int64(*i), 10)
}
type int16Value int16
func (i *int16Value) Set(v string) error {
return parseInt(v, 16, func(r int64) {
*i = int16Value(r)
})
}
func (i *int16Value) String() string {
return strconv.FormatInt(int64(*i), 10)
}
type int32Value int32
func (i *int32Value) Set(v string) error {
return parseInt(v, 32, func(r int64) {
*i = int32Value(r)
})
}
func (i *int32Value) String() string {
return strconv.FormatInt(int64(*i), 10)
}
type int64Value int64
func (i *int64Value) Set(v string) error {
return parseInt(v, 64, func(r int64) {
*i = int64Value(r)
})
}
func (i *int64Value) String() string {
return strconv.FormatInt(int64(*i), 10)
}
func parseUint(v string, bitSize int, callback func(r uint64)) error {
r, err := strconv.ParseUint(v, 10, bitSize)
if err != nil {
return err
}
callback(r)
return nil
}
type uintValue uint
func (i *uintValue) Set(v string) error {
return parseUint(v, 0, func(r uint64) {
*i = uintValue(r)
})
}
func (i *uintValue) String() string {
return strconv.FormatUint(uint64(*i), 10)
}
type uint8Value uint8
func (i *uint8Value) Set(v string) error {
return parseUint(v, 8, func(r uint64) {
*i = uint8Value(r)
})
}
func (i *uint8Value) String() string {
return strconv.FormatUint(uint64(*i), 10)
}
type uint16Value uint16
func (i *uint16Value) Set(v string) error {
return parseUint(v, 16, func(r uint64) {
*i = uint16Value(r)
})
}
func (i *uint16Value) String() string {
return strconv.FormatUint(uint64(*i), 10)
}
type uint32Value uint32
func (i *uint32Value) Set(v string) error {
return parseUint(v, 32, func(r uint64) {
*i = uint32Value(r)
})
}
func (i *uint32Value) String() string {
return strconv.FormatUint(uint64(*i), 10)
}
type uint64Value uint64
func (i *uint64Value) Set(v string) error {
return parseUint(v, 64, func(r uint64) {
*i = uint64Value(r)
})
}
func (i *uint64Value) String() string {
return strconv.FormatUint(uint64(*i), 10)
}
func parseFloat(v string, bitSize int, callback func(r float64)) error {
r, err := strconv.ParseFloat(v, bitSize)
if err != nil {
return err
}
callback(r)
return nil
}
type float32Value float32
func (i *float32Value) Set(v string) error {
return parseFloat(v, 32, func(r float64) {
*i = float32Value(r)
})
}
func (i *float32Value) String() string {
return strconv.FormatFloat(float64(*i), 'f', -1, 32)
}
type float64Value float64
func (i *float64Value) Set(v string) error {
return parseFloat(v, 64, func(r float64) {
*i = float64Value(r)
})
}
func (i *float64Value) String() string {
return strconv.FormatFloat(float64(*i), 'f', -1, 64)
}
type boolValue bool
func (b *boolValue) Set(v string) error {
bl, err := strconv.ParseBool(v)
if err != nil {
return err
}
*b = boolValue(bl)
return nil
}
func (b *boolValue) String() string {
return strconv.FormatBool(bool(*b))
}
type stringValue string
func (s *stringValue) Set(v string) error {
*s = stringValue(v)
return nil
}
func (s *stringValue) String() string {
return string(*s)
}
type durationValue time.Duration
func (d *durationValue) Set(v string) error {
r, err := time.ParseDuration(v)
if err != nil {
return err
}
*d = durationValue(r)
return nil
}
func (d *durationValue) String() string {
return time.Duration(*d).String()
}
type fileValue os.File
func (f *fileValue) Set(v string) error {
file, err := os.Open(v)
if err != nil {
return err
}
*f = fileValue(*file)
return nil
}
func (f *fileValue) String() string {
if (*os.File)(f).Fd() == 0 {
return ""
}
return (*os.File)(f).Name()
}
type funcValue func(string) error
func (f funcValue) Set(s string) error { return f(s) }
func (f funcValue) String() string { return "" }
// List Value
type listValue interface {
IsList() bool
}
func parseList(v string, callback func(r string) error) error {
if strings.Contains(v, ",") {
for _, s := range strings.Split(v, ",") {
if err := callback(s); err != nil {
return err
}
}
return nil
}
return callback(v)
}
func toListString[T generic](src []T, convert func(T) flag.Value) string {
list := make([]string, 0, len(src))
for _, i := range src {
list = append(list, convert(i).String())
}
return strings.Join(list, ",")
}
type intList []int
func (l *intList) Set(v string) error {
return parseList(v, func(s string) error {
return parseInt(s, 0, func(r int64) {
*l = append(*l, int(r))
})
})
}
func (l *intList) String() string {
return toListString(*l, func(i int) flag.Value {
return (*intValue)(&i)
})
}
func (l *intList) IsList() bool {
return true
}
type int8List []int8
func (l *int8List) Set(v string) error {
return parseList(v, func(s string) error {
return parseInt(s, 8, func(r int64) {
*l = append(*l, int8(r))
})
})
}
func (l *int8List) String() string {
return toListString(*l, func(i int8) flag.Value {
return (*int8Value)(&i)
})
}
func (l *int8List) IsList() bool {
return true
}
type int16List []int16
func (l *int16List) Set(v string) error {
return parseList(v, func(s string) error {
return parseInt(s, 16, func(r int64) {
*l = append(*l, int16(r))
})
})
}
func (l *int16List) String() string {
return toListString(*l, func(i int16) flag.Value {
return (*int16Value)(&i)
})
}
func (l *int16List) IsList() bool {
return true
}
type int32List []int32
func (l *int32List) Set(v string) error {
return parseList(v, func(s string) error {
return parseInt(s, 32, func(r int64) {
*l = append(*l, int32(r))
})
})
}
func (l *int32List) String() string {
return toListString(*l, func(i int32) flag.Value {
return (*int32Value)(&i)
})
}
func (l *int32List) IsList() bool {
return true
}
type int64List []int64
func (l *int64List) Set(v string) error {
return parseList(v, func(s string) error {
return parseInt(s, 64, func(r int64) {
*l = append(*l, r)
})
})
}
func (l *int64List) String() string {
return toListString(*l, func(i int64) flag.Value {
return (*int64Value)(&i)
})
}
func (l *int64List) IsList() bool {
return true
}
type uintList []uint
func (l *uintList) Set(v string) error {
return parseList(v, func(s string) error {
return parseUint(s, 0, func(r uint64) {
*l = append(*l, uint(r))
})
})
}
func (l *uintList) String() string {
return toListString(*l, func(i uint) flag.Value {
return (*uintValue)(&i)
})
}
func (l *uintList) IsList() bool {
return true
}
type uint8List []uint8
func (l *uint8List) Set(v string) error {
return parseList(v, func(s string) error {
return parseUint(s, 8, func(r uint64) {
*l = append(*l, uint8(r))
})
})
}
func (l *uint8List) String() string {
return toListString(*l, func(i uint8) flag.Value {
return (*uint8Value)(&i)
})
}
func (l *uint8List) IsList() bool {
return true
}
type uint16List []uint16
func (l *uint16List) Set(v string) error {
return parseList(v, func(s string) error {
return parseUint(s, 16, func(r uint64) {
*l = append(*l, uint16(r))
})
})
}
func (l *uint16List) String() string {
return toListString(*l, func(i uint16) flag.Value {
return (*uint16Value)(&i)
})
}
func (l *uint16List) IsList() bool {
return true
}
type uint32List []uint32
func (l *uint32List) Set(v string) error {
return parseList(v, func(s string) error {
return parseUint(s, 32, func(r uint64) {
*l = append(*l, uint32(r))
})
})
}
func (l *uint32List) String() string {
return toListString(*l, func(i uint32) flag.Value {
return (*uint32Value)(&i)
})
}
func (l *uint32List) IsList() bool {
return true
}
type uint64List []uint64
func (l *uint64List) Set(v string) error {
return parseList(v, func(s string) error {
return parseUint(s, 64, func(r uint64) {
*l = append(*l, r)
})
})
}
func (l *uint64List) String() string {
return toListString(*l, func(i uint64) flag.Value {
return (*uint64Value)(&i)
})
}
func (l *uint64List) IsList() bool {
return true
}
type float32List []float32
func (l *float32List) Set(v string) error {
return parseList(v, func(s string) error {
return parseFloat(s, 32, func(r float64) {
*l = append(*l, float32(r))
})
})
}
func (l *float32List) String() string {
return toListString(*l, func(i float32) flag.Value {
return (*float32Value)(&i)
})
}
func (l *float32List) IsList() bool {
return true
}
type float64List []float64
func (l *float64List) Set(v string) error {
return parseList(v, func(s string) error {
return parseFloat(s, 64, func(r float64) {
*l = append(*l, r)
})
})
}
func (l *float64List) String() string {
return toListString(*l, func(i float64) flag.Value {
return (*float64Value)(&i)
})
}
func (l *float64List) IsList() bool {
return true
}
type boolList []bool
func (l *boolList) Set(v string) error {
return parseList(v, func(s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
*l = append(*l, b)
return nil
})
}
func (l *boolList) String() string {
return toListString(*l, func(i bool) flag.Value {
return (*boolValue)(&i)
})
}
func (l *boolList) IsList() bool {
return true
}
type stringList []string
func (l *stringList) Set(v string) error {
return parseList(v, func(s string) error {
*l = append(*l, s)
return nil
})
}
func (l *stringList) String() string {
return toListString(*l, func(i string) flag.Value {
return (*stringValue)(&i)
})
}
func (l *stringList) IsList() bool {
return true
}
type durationList []time.Duration
func (l *durationList) Set(v string) error {
return parseList(v, func(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
*l = append(*l, d)
return nil
})
}
func (l *durationList) String() string {
return toListString(*l, func(i time.Duration) flag.Value {
return (*durationValue)(&i)
})
}
func (l *durationList) IsList() bool {
return true
}
type fileList []*os.File
func (l *fileList) Set(v string) error {
return parseList(v, func(s string) error {
f, err := os.Open(s)
if err != nil {
return err
}
*l = append(*l, f)
return nil
})
}
func (l *fileList) String() string {
return toListString(*l, func(i *os.File) flag.Value {
return (*fileValue)(i)
})
}
func (l *fileList) IsList() bool {
return true
}