-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues.go
More file actions
267 lines (234 loc) · 4.97 KB
/
Copy pathvalues.go
File metadata and controls
267 lines (234 loc) · 4.97 KB
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
package appcfg
import (
"fmt"
"math"
"net"
urlpkg "net/url"
"slices"
"strconv"
"strings"
"time"
)
// Duration can be set only to string valid for [time.ParseDuration].
type Duration struct {
value *time.Duration
}
func (v *Duration) set(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
v.value = &d
return nil
}
// Bool can be set to 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
type Bool struct {
value *bool
}
func (v *Bool) set(s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
v.value = &b
return nil
}
// String can be set to any string, even empty.
type String struct {
value *string
}
func (v *String) set(s string) error {
v.value = &s
return nil
}
// NotEmptyString can be set to any string which contains at least one
// non-whitespace symbol.
type NotEmptyString struct {
value *string
}
func (v *NotEmptyString) set(s string) error {
if strings.TrimSpace(s) == "" {
return errEmptyOrWhite
}
v.value = &s
return nil
}
// OneOfString can be set to any of predefined (using NewOneOfString or
// MustOneOfString) values.
type OneOfString struct {
value *string
oneOf []string
}
func (v *OneOfString) set(s string) error {
if slices.Contains(v.oneOf, s) {
v.value = &s
return nil
}
return fmt.Errorf("%w %q", errNotOneOf, v.oneOf)
}
// Endpoint can be set only to valid url with hostname. Also it'll trim
// all / symbols at end, to make it easier to append paths to endpoint.
type Endpoint struct {
value *string
}
func (v *Endpoint) set(s string) error {
s = strings.TrimRight(s, "/")
p, err := urlpkg.Parse(s)
if err != nil {
return err
} else if p.Host == "" {
return errNoHost
}
v.value = &s
return nil
}
// Int can be set to integer value.
// It's allowed to use 0b, 0o and 0x prefixes, and also underscores.
type Int struct {
value *int
}
func (v *Int) set(s string) error {
i64, err := strconv.ParseInt(s, 0, strconv.IntSize)
if err != nil {
return err
}
i := int(i64)
if int64(i) != i64 {
return fmt.Errorf("%w int: %s", errOverflows, s)
}
v.value = &i
return nil
}
// Int64 can be set to 64-bit integer value.
// It's allowed to use 0b, 0o and 0x prefixes, and also underscores.
type Int64 struct {
value *int64
}
func (v *Int64) set(s string) error {
i, err := strconv.ParseInt(s, 0, parseBits)
if err != nil {
return err
}
v.value = &i
return nil
}
// Uint can be set to unsigned integer value.
// It's allowed to use 0b, 0o and 0x prefixes, and also underscores.
type Uint struct {
value *uint
}
func (v *Uint) set(s string) error {
i64, err := strconv.ParseUint(s, 0, strconv.IntSize)
if err != nil {
return err
}
i := uint(i64)
if uint64(i) != i64 {
return fmt.Errorf("%w unsigned int: %s", errOverflows, s)
}
v.value = &i
return nil
}
// Uint64 can be set to unsigned 64-bit integer value.
// It's allowed to use 0b, 0o and 0x prefixes, and also underscores.
type Uint64 struct {
value *uint64
}
func (v *Uint64) set(s string) error {
i, err := strconv.ParseUint(s, 0, parseBits)
if err != nil {
return err
}
v.value = &i
return nil
}
// Float64 can be set to 64-bit floating-point number.
type Float64 struct {
value *float64
}
func (v *Float64) set(s string) error {
i, err := strconv.ParseFloat(s, parseBits)
if err != nil {
return err
}
v.value = &i
return nil
}
// IntBetween can be set to integer value between given (using
// NewIntBetween or MustIntBetween) min/max values (inclusive).
type IntBetween struct {
value *int
min, max int
}
func (v *IntBetween) set(s string) error {
i, err := strconv.Atoi(s)
if err != nil {
return err
} else if v.min > i || i > v.max {
return fmt.Errorf("%w %d and %d", errNotBetween, v.min, v.max)
}
v.value = &i
return nil
}
// Port can be set to integer value between 1 and 65535.
type Port struct {
value *int
}
func (v *Port) set(s string) error {
i, err := strconv.Atoi(s)
if err != nil {
return err
} else if 0 >= i || i > math.MaxUint16 {
return fmt.Errorf("%w 1 and %d", errNotBetween, math.MaxUint16)
}
v.value = &i
return nil
}
// ListenPort can be set to integer value between 0 and 65535.
type ListenPort struct {
value *int
}
func (v *ListenPort) set(s string) error {
i, err := strconv.Atoi(s)
if err != nil {
return err
} else if 0 > i || i > math.MaxUint16 {
return fmt.Errorf("%w 0 and %d", errNotBetween, math.MaxUint16)
}
v.value = &i
return nil
}
// IPNet can be set to CIDR address.
type IPNet struct {
value **net.IPNet
}
func (v *IPNet) set(s string) error {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
return err
}
v.value = &ipNet
return nil
}
// HostPort can be set to CIDR address.
type HostPort struct {
value *string
host string
port int
}
func (v *HostPort) set(s string) error {
host, port, err := net.SplitHostPort(s)
if err != nil {
return err
}
if host == "" {
return errNoHost
}
v.port, err = strconv.Atoi(port)
if err != nil {
return fmt.Errorf("port: %w", err)
}
v.host = host
v.value = &s
return nil
}