-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice.go
220 lines (195 loc) · 4.58 KB
/
slice.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
package flagx
import (
"fmt"
"strconv"
"strings"
"time"
)
type boolSlice struct {
sep string
value *[]bool
}
// Set implements the flag.Value interface.
func (s boolSlice) Set(str string) error {
var bools []bool
for _, v := range strings.Split(str, s.sep) {
b, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("parsing bool: %w", err)
}
bools = append(bools, b)
}
*s.value = bools
return nil
}
// String implements the flag.Value interface.
func (s boolSlice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = strconv.FormatBool(v)
}
return strings.Join(res, s.sep)
}
type intSlice struct {
sep string
value *[]int
}
// Set implements the flag.Value interface.
func (s intSlice) Set(str string) error {
var ints []int
for _, v := range strings.Split(str, s.sep) {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("parsing int: %w", err)
}
ints = append(ints, int(i))
}
*s.value = ints
return nil
}
// String implements the flag.Value interface.
func (s intSlice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = strconv.FormatInt(int64(v), 10)
}
return strings.Join(res, s.sep)
}
type int64Slice struct {
sep string
value *[]int64
}
// Set implements the flag.Value interface.
func (s int64Slice) Set(str string) error {
var ints []int64
for _, v := range strings.Split(str, s.sep) {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("parsing int: %w", err)
}
ints = append(ints, i)
}
*s.value = ints
return nil
}
// String implements the flag.Value interface.
func (s int64Slice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = strconv.FormatInt(v, 10)
}
return strings.Join(res, s.sep)
}
type uintSlice struct {
sep string
value *[]uint
}
// Set implements the flag.Value interface.
func (s uintSlice) Set(str string) error {
var uints []uint
for _, v := range strings.Split(str, s.sep) {
i, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return fmt.Errorf("parsing uint: %w", err)
}
uints = append(uints, uint(i))
}
*s.value = uints
return nil
}
// String implements the flag.Value interface.
func (s uintSlice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = strconv.FormatUint(uint64(v), 10)
}
return strings.Join(res, s.sep)
}
type uint64Slice struct {
sep string
value *[]uint64
}
// Set implements the flag.Value interface.
func (s uint64Slice) Set(str string) error {
var uints []uint64
for _, v := range strings.Split(str, s.sep) {
i, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return fmt.Errorf("parsing uint: %w", err)
}
uints = append(uints, i)
}
*s.value = uints
return nil
}
// String implements the flag.Value interface.
func (s uint64Slice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = strconv.FormatUint(v, 10)
}
return strings.Join(res, s.sep)
}
type stringSlice struct {
sep string
value *[]string
}
// Set implements the flag.Value interface.
func (s stringSlice) Set(str string) error {
*s.value = strings.Split(str, s.sep)
return nil
}
// String implements the flag.Value interface.
func (s stringSlice) String() string {
return strings.Join(*s.value, s.sep)
}
type float64Slice struct {
sep string
value *[]float64
}
// Set implements the flag.Value interface.
func (s float64Slice) Set(str string) error {
var floats []float64
for _, v := range strings.Split(str, s.sep) {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return fmt.Errorf("parsing float: %w", err)
}
floats = append(floats, f)
}
*s.value = floats
return nil
}
// String implements the flag.Value interface.
func (s float64Slice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = strconv.FormatFloat(v, 'g', 10, 64)
}
return strings.Join(res, s.sep)
}
type durationSlice struct {
sep string
value *[]time.Duration
}
// Set implements the flag.Value interface.
func (s durationSlice) Set(str string) error {
var durs []time.Duration
for _, v := range strings.Split(str, s.sep) {
d, err := time.ParseDuration(v)
if err != nil {
return fmt.Errorf("parsing duration: %w", err)
}
durs = append(durs, d)
}
*s.value = durs
return nil
}
// String implements the flag.Value interface.
func (s durationSlice) String() string {
res := make([]string, len(*s.value))
for i, v := range *s.value {
res[i] = v.String()
}
return strings.Join(res, s.sep)
}