Repository navigation
Expand file tree
/
Copy pathhelpers.go
More file actions
284 lines (267 loc) · 7.29 KB
/
Copy pathhelpers.go
File metadata and controls
284 lines (267 loc) · 7.29 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package validator
import (
"cmp"
"errors"
"math"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/libtnb/validator/conv"
)
const maxRegexCache = 10000
// regexCache holds compiled patterns; bounded since a pattern may come from data.
var (
regexCache sync.Map
regexCacheLen atomic.Int64
)
// IsEmpty reports whether v is absent or the empty value of its kind, the
// omitempty semantics the built-in rules share: nil, "", 0, false, an empty
// string, collection or channel, a nil pointer or func, and a zero time.Time.
// A non-time struct is never empty, so its rules still run. Custom rules use
// it to let empty values through and leave presence to required.
func IsEmpty(v any) bool {
if v == nil {
return true
}
return isEmptyV(reflect.ValueOf(v))
}
// IsEmptyValue is IsEmpty for a reflect.Value such as the result of
// Field.Reflect or Field.SiblingValue; an invalid Value is empty.
func IsEmptyValue(rv reflect.Value) bool { return isEmptyV(rv) }
func isEmptyV(rv reflect.Value) bool {
if !rv.IsValid() {
return true
}
if rv.Type() == timeType {
return rv.Interface().(time.Time).IsZero()
}
switch rv.Kind() {
case reflect.String, reflect.Array, reflect.Slice, reflect.Map, reflect.Chan:
return rv.Len() == 0
case reflect.Bool:
return !rv.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rv.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return rv.Uint() == 0
case reflect.Float32, reflect.Float64:
return rv.Float() == 0
case reflect.Complex64, reflect.Complex128:
return rv.Complex() == 0
case reflect.Pointer, reflect.Interface, reflect.Func, reflect.UnsafePointer:
return rv.IsNil()
default:
// Non-time struct: NOT empty, so omitempty keeps running rules.
return false
}
}
func numericValue(rv reflect.Value) (float64, bool) {
if !rv.IsValid() {
return 0, false
}
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(rv.Int()), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return float64(rv.Uint()), true
case reflect.Float32, reflect.Float64:
f := rv.Float()
if math.IsInf(f, 0) || math.IsNaN(f) {
return 0, false
}
return f, true
case reflect.String:
return parseNumericString(rv.String())
case reflect.Slice:
if rv.Type().Elem().Kind() == reflect.Uint8 {
return parseNumericString(string(rv.Bytes()))
}
return 0, false
default:
return 0, false
}
}
func valString(rv reflect.Value) string {
if !rv.IsValid() {
return ""
}
if rv.Kind() == reflect.String {
return rv.String()
}
if !rv.CanInterface() {
return ""
}
return conv.ToString(rv.Interface())
}
// parseNumericString parses a strict decimal (sign, digits, optional .fraction);
// no exponent/hex/underscores, since form data is not a Go literal. Blank fails.
func parseNumericString(s string) (float64, bool) {
s = strings.TrimSpace(s)
if !validDecimal(s) {
return 0, false
}
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, false
}
if math.IsInf(f, 0) || math.IsNaN(f) {
return 0, false
}
return f, true
}
func validDecimal(s string) bool {
var i int
if i < len(s) && (s[i] == '+' || s[i] == '-') {
i++
}
start := i
for i < len(s) && s[i] >= '0' && s[i] <= '9' {
i++
}
if i == start {
return false
}
if i < len(s) && s[i] == '.' {
i++
fstart := i
for i < len(s) && s[i] >= '0' && s[i] <= '9' {
i++
}
if i == fstart {
return false
}
}
return i == len(s)
}
// compareSize three-way compares rv's magnitude against arg. Integers compare
// exactly as int64/uint64 (no float64 rounding above 2^53); strings and []byte by
// rune count unless numericHint forces a numeric compare; other collections by
// length. ok=false (unmeasurable value or unparsable threshold) fails closed.
func compareSize(rv reflect.Value, arg string, numericHint bool) (c int, ok bool) {
if !rv.IsValid() {
return 0, false
}
arg = strings.TrimSpace(arg)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return cmpIntArg(rv.Int(), arg)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return cmpUintArg(rv.Uint(), arg)
case reflect.Float32, reflect.Float64:
f := rv.Float()
if math.IsInf(f, 0) || math.IsNaN(f) {
return 0, false
}
t, tok := parseNumericString(arg)
if !tok {
return 0, false
}
return cmp.Compare(f, t), true
case reflect.String:
if numericHint {
return cmpNumericString(rv.String(), arg)
}
return cmpLenArg(utf8.RuneCountInString(rv.String()), arg)
case reflect.Slice:
if numericHint && rv.Type().Elem().Kind() == reflect.Uint8 {
return cmpNumericString(string(rv.Bytes()), arg)
}
return cmpLenArg(rv.Len(), arg)
case reflect.Array, reflect.Map, reflect.Chan:
return cmpLenArg(rv.Len(), arg)
default:
return 0, false
}
}
// cmpIntArg compares v against arg: exact for integer thresholds (out-of-range
// integers still order correctly by sign), float for fractional thresholds.
func cmpIntArg(v int64, arg string) (int, bool) {
t, err := strconv.ParseInt(arg, 10, 64)
if err == nil {
return cmp.Compare(v, t), true
}
if errors.Is(err, strconv.ErrRange) {
// syntactically an integer, beyond int64: sign decides the order
if strings.HasPrefix(arg, "-") {
return 1, true
}
return -1, true
}
tf, ok := parseNumericString(arg)
if !ok {
return 0, false
}
return cmp.Compare(float64(v), tf), true
}
func cmpUintArg(v uint64, arg string) (int, bool) {
t, err := strconv.ParseUint(arg, 10, 64)
if err == nil {
return cmp.Compare(v, t), true
}
if errors.Is(err, strconv.ErrRange) {
return -1, true // beyond uint64: v is smaller
}
tf, ok := parseNumericString(arg)
if !ok {
return 0, false
}
return cmp.Compare(float64(v), tf), true
}
// cmpNumericString compares s against arg exactly as int64/uint64, else float64.
func cmpNumericString(s, arg string) (int, bool) {
s = strings.TrimSpace(s)
if v, err := strconv.ParseInt(s, 10, 64); err == nil {
return cmpIntArg(v, arg)
}
if v, err := strconv.ParseUint(s, 10, 64); err == nil {
return cmpUintArg(v, arg)
}
v, ok := parseNumericString(s)
if !ok {
return 0, false
}
t, ok := parseNumericString(arg)
if !ok {
return 0, false
}
return cmp.Compare(v, t), true
}
func cmpLenArg(n int, arg string) (int, bool) {
t, ok := parseNumericString(arg)
if !ok {
return 0, false
}
return cmp.Compare(float64(n), t), true
}
// compileRegex serves the generic regex path (a pattern that reached
// evaluation without leafCompiler pre-compilation); failures are cached too
// so a bad pattern is not recompiled on every value.
func compileRegex(pattern string) (*regexp.Regexp, error) {
if v, ok := regexCache.Load(pattern); ok {
switch t := v.(type) {
case *regexp.Regexp:
return t, nil
case error:
return nil, t
}
}
re, err := regexp.Compile(pattern)
// Counter is best-effort under concurrency, so the bound is approximate.
if regexCacheLen.Load() >= maxRegexCache {
regexCache.Clear()
regexCacheLen.Store(0)
}
if err != nil {
regexCache.Store(pattern, err)
regexCacheLen.Add(1)
return nil, err
}
regexCache.Store(pattern, re)
regexCacheLen.Add(1)
return re, nil
}