-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
333 lines (313 loc) · 7.92 KB
/
Copy pathvalue.go
File metadata and controls
333 lines (313 loc) · 7.92 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
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
package dyfields
import (
"encoding/json"
"fmt"
"math"
"math/big"
"net"
"net/mail"
"net/url"
"regexp"
"strings"
"time"
)
// ---- coercion -------------------------------------------------------------
// toFloat accepts every numeric shape encoding/json can produce.
func toFloat(v any) (float64, bool) {
switch t := v.(type) {
case float64:
return t, true
case float32:
return float64(t), true
case int:
return float64(t), true
case int32:
return float64(t), true
case int64:
return float64(t), true
case uint:
return float64(t), true
case uint64:
return float64(t), true
case json.Number:
f, err := t.Float64()
return f, err == nil
}
return 0, false
}
func toString(v any) (string, bool) {
s, ok := v.(string)
return s, ok
}
func toBool(v any) (bool, bool) {
b, ok := v.(bool)
return b, ok
}
var decimalRe = regexp.MustCompile(`^-?\d+(\.\d+)?$`)
// toRat parses a decimal, which travels as a string precisely because JSON has
// no exact decimal type and float64 would silently lose cents.
func toRat(v any) (*big.Rat, bool) {
switch t := v.(type) {
case string:
if !decimalRe.MatchString(t) {
return nil, false
}
r, ok := new(big.Rat).SetString(t)
return r, ok
case json.Number:
r, ok := new(big.Rat).SetString(t.String())
return r, ok
}
return nil, false
}
// isEmptyValue defines "not set" uniformly: a hidden field's value is removed,
// so conditions converge predictably.
func isEmptyValue(v any) bool {
switch t := v.(type) {
case nil:
return true
case string:
return t == ""
case []any:
return len(t) == 0
case map[string]any:
return len(t) == 0
}
return false
}
// ---- type checking --------------------------------------------------------
var uuidRe = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
var hostnameRe = regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`)
var bytesizeRe = regexp.MustCompile(`^\d+(\.\d+)?\s*(B|[KMGTP]i?B?)?$`)
var itemIDRe = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)
// checkScalarType reports whether v has the shape the type demands.
func checkScalarType(v any, t Type) (string, bool) {
switch t {
case TypeString, TypeSecret, TypeFile, TypeDatetime:
if _, ok := toString(v); !ok {
return "must be a string", false
}
case TypeBoolean:
if _, ok := toBool(v); !ok {
return "must be a boolean", false
}
case TypeNumber:
if _, ok := toFloat(v); !ok {
return "must be a number", false
}
case TypeInteger:
f, ok := toFloat(v)
if !ok {
return "must be an integer", false
}
if f != math.Trunc(f) {
return "must be an integer, not a fraction", false
}
case TypeDecimal:
if _, ok := toRat(v); !ok {
return "must be a decimal string such as \"19.99\"", false
}
}
return "", true
}
// checkFormat runs the built-in validation for a format. The rules live here,
// in one place: if every caller wrote its own email pattern they would drift.
// An unknown format is not an error, it degrades to a pure UI hint.
func checkFormat(v any, format string) (string, bool) {
s, ok := toString(v)
if !ok || s == "" {
return "", true
}
switch format {
case "email":
if _, err := mail.ParseAddress(s); err != nil || !strings.Contains(s, "@") {
return "must be a valid email address", false
}
case "uri":
u, err := url.Parse(s)
if err != nil || u.Scheme == "" || u.Host == "" {
return "must be an absolute URI", false
}
case "hostname":
if len(s) > 253 || !hostnameRe.MatchString(s) {
return "must be a valid hostname", false
}
case "ipv4":
ip := net.ParseIP(s)
if ip == nil || ip.To4() == nil {
return "must be a valid IPv4 address", false
}
case "ipv6":
ip := net.ParseIP(s)
if ip == nil || ip.To4() != nil {
return "must be a valid IPv6 address", false
}
case "uuid":
if !uuidRe.MatchString(s) {
return "must be a valid UUID", false
}
case "duration":
if _, err := time.ParseDuration(s); err != nil {
return "must be a duration such as \"30s\"", false
}
case "bytesize":
if !bytesizeRe.MatchString(s) {
return "must be a byte size such as \"512Mi\"", false
}
case "cron":
n := len(strings.Fields(s))
if n < 5 || n > 6 {
return "must be a cron expression with 5 or 6 fields", false
}
case "regex":
if _, err := regexp.Compile(s); err != nil {
return "must be a valid regular expression", false
}
case "date":
if _, err := time.Parse("2006-01-02", s); err != nil {
return "must be a date such as \"2026-11-12\"", false
}
case "time":
if _, err := time.Parse("15:04:05", s); err != nil {
return "must be a time such as \"14:30:00\"", false
}
case "datetime", "":
// A datetime field with no explicit format is RFC3339.
}
return "", true
}
// checkDatetime enforces RFC3339 when the field carries no narrower format.
func checkDatetime(v any, format string) (string, bool) {
s, ok := toString(v)
if !ok || s == "" {
return "", true
}
switch format {
case "date", "time":
return checkFormat(v, format)
default:
if _, err := time.Parse(time.RFC3339, s); err != nil {
return "must be an RFC3339 timestamp", false
}
}
return "", true
}
func parseTimeValue(v any, format string) (time.Time, bool) {
s, ok := toString(v)
if !ok {
return time.Time{}, false
}
layouts := []string{time.RFC3339, "2006-01-02", "15:04:05"}
switch format {
case "date":
layouts = []string{"2006-01-02", time.RFC3339}
case "time":
layouts = []string{"15:04:05", time.RFC3339}
}
for _, l := range layouts {
if t, err := time.Parse(l, s); err == nil {
return t, true
}
}
return time.Time{}, false
}
// ---- comparison -----------------------------------------------------------
// equalValues compares two JSON values for equality, tolerating the numeric
// shapes encoding/json produces.
func equalValues(a, b any) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
if af, ok := toFloat(a); ok {
if bf, ok2 := toFloat(b); ok2 {
return af == bf
}
}
as, aok := toString(a)
bs, bok := toString(b)
if aok && bok {
return as == bs
}
ab, aok2 := toBool(a)
bb, bok2 := toBool(b)
if aok2 && bok2 {
return ab == bb
}
ja, err1 := json.Marshal(a)
jb, err2 := json.Marshal(b)
return err1 == nil && err2 == nil && string(ja) == string(jb)
}
// compareOrdered returns -1, 0, 1. Comparison is type-aware: datetime compares
// on the time axis, numbers numerically, strings lexicographically.
func compareOrdered(a, b any, t Type, format string) (int, bool) {
switch t {
case TypeDatetime:
ta, ok1 := parseTimeValue(a, format)
tb, ok2 := parseTimeValue(b, format)
if !ok1 || !ok2 {
return 0, false
}
switch {
case ta.Before(tb):
return -1, true
case ta.After(tb):
return 1, true
}
return 0, true
case TypeDecimal:
ra, ok1 := toRat(a)
rb, ok2 := toRat(b)
if !ok1 || !ok2 {
return 0, false
}
return ra.Cmp(rb), true
case TypeInteger, TypeNumber:
fa, ok1 := toFloat(a)
fb, ok2 := toFloat(b)
if !ok1 || !ok2 {
return 0, false
}
switch {
case fa < fb:
return -1, true
case fa > fb:
return 1, true
}
return 0, true
case TypeString, TypeSecret, TypeFile:
sa, ok1 := toString(a)
sb, ok2 := toString(b)
if !ok1 || !ok2 {
return 0, false
}
return strings.Compare(sa, sb), true
}
return 0, false
}
// isOrderable reports whether a type supports gt/gte/lt/lte at all.
func isOrderable(t Type) bool {
switch t {
case TypeDatetime, TypeDecimal, TypeInteger, TypeNumber, TypeString, TypeSecret, TypeFile:
return true
}
return false
}
// normalizeForUnique canonicalises a value before uniqueness comparison.
func normalizeForUnique(v any, format string) string {
s, ok := toString(v)
if !ok {
b, _ := json.Marshal(v)
return string(b)
}
s = strings.TrimSpace(s)
if format == "email" || format == "hostname" {
s = strings.ToLower(s)
}
return s
}
func plural(n int, one, many string) string {
if n == 1 {
return fmt.Sprintf("%d %s", n, one)
}
return fmt.Sprintf("%d %s", n, many)
}