-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
416 lines (385 loc) · 10.2 KB
/
type.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
package ox
import (
"encoding"
"fmt"
"math/big"
"net/netip"
"net/url"
"reflect"
"regexp"
"time"
)
// Type is a variable type.
type Type string
// Types.
const (
BytesT Type = "bytes"
StringT Type = "string"
RunesT Type = "runes"
Base64T Type = "base64"
HexT Type = "hex"
BoolT Type = "bool"
ByteT Type = "byte"
RuneT Type = "rune"
Int64T Type = "int64"
Int32T Type = "int32"
Int16T Type = "int16"
Int8T Type = "int8"
IntT Type = "int"
Uint64T Type = "uint64"
Uint32T Type = "uint32"
Uint16T Type = "uint16"
Uint8T Type = "uint8"
UintT Type = "uint"
Float64T Type = "float64"
Float32T Type = "float32"
Complex128T Type = "complex128"
Complex64T Type = "complex64"
SizeT Type = "size"
RateT Type = "rate"
TimestampT Type = "timestamp"
DateTimeT Type = "datetime"
DateT Type = "date"
TimeT Type = "time"
DurationT Type = "duration"
CountT Type = "count"
PathT Type = "path"
BigIntT Type = "bigint"
BigFloatT Type = "bigfloat"
BigRatT Type = "bigrat"
AddrT Type = "addr"
AddrPortT Type = "addrport"
CIDRT Type = "cidr"
RegexpT Type = "regexp"
URLT Type = "url"
UUIDT Type = "uuid"
ColorT Type = "color"
GlobT Type = "glob"
SliceT Type = "slice"
ArrayT Type = "array"
MapT Type = "map"
HookT Type = "hook"
)
// Option returns an [option] for the type.
func (typ Type) Option() option {
return option{
name: "Type(" + typ.String() + ")",
flag: func(g *Flag) error {
if g.Type != SliceT && g.Type != ArrayT && g.Type != MapT && g.Type != HookT {
g.Type = typ
} else if g.Type != HookT {
g.Elem = typ
}
return nil
},
typ: func(t interface{ SetType(Type) }) error {
t.SetType(typ)
return nil
},
}
}
// String satisfies the [fmt.Stringer] interface.
func (typ Type) String() string {
return string(typ)
}
// New creates a new [Value] for the registered type.
func (typ Type) New() (Value, error) {
var v Value
var err error
switch typ {
case HookT:
return nil, fmt.Errorf("%w (hook)", ErrCouldNotCreateValue)
case SliceT:
v = NewSlice(StringT)
case ArrayT:
v = NewArray(StringT)
case MapT:
v, err = NewMap(StringT, StringT)
default:
f, ok := typeNews[typ]
if !ok {
return nil, fmt.Errorf("%w: type not registered (%q)", ErrCouldNotCreateValue, string(typ))
}
v, err = f()
}
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCouldNotCreateValue, err)
}
return v, nil
}
// typeNews are registered type creation funcs.
var typeNews map[Type]func() (Value, error)
// typeFlagOpts are registered type flag options.
var typeFlagOpts map[Type][]Option
// reflectTypes are reflect type name lookups for registered types.
var reflectTypes map[string]Type
// typeTextNews are registered type creation funcs for text marshalable types.
var typeTextNews map[Type]func() (any, error)
// typeBinaryNews are registered type creation funcs for binary marshalable
// types.
var typeBinaryNews map[Type]func() (any, error)
func init() {
typeNews = make(map[Type]func() (Value, error))
typeFlagOpts = make(map[Type][]Option)
reflectTypes = make(map[string]Type)
typeTextNews = make(map[Type]func() (any, error))
typeBinaryNews = make(map[Type]func() (any, error))
// register basic types
RegisterType(BytesT, NewVal[[]byte]())
RegisterType(StringT, NewVal[string]())
RegisterType(RunesT, NewVal[[]rune]())
RegisterType(Base64T, NewVal[[]byte](Base64T))
RegisterType(HexT, NewVal[[]byte](HexT))
RegisterType(BoolT, NewVal[bool](), NoArg(true, true))
RegisterType(ByteT, NewVal[string](ByteT))
RegisterType(RuneT, NewVal[string](RuneT))
RegisterType(Int64T, NewVal[int64]())
RegisterType(Int32T, NewVal[int32]())
RegisterType(Int16T, NewVal[int16]())
RegisterType(Int8T, NewVal[int8]())
RegisterType(IntT, NewVal[int]())
RegisterType(Uint64T, NewVal[uint64]())
RegisterType(Uint32T, NewVal[uint32]())
RegisterType(Uint16T, NewVal[uint16]())
RegisterType(Uint8T, NewVal[uint8]())
RegisterType(UintT, NewVal[uint]())
RegisterType(Float64T, NewVal[float64]())
RegisterType(Float32T, NewVal[float32]())
RegisterType(Complex128T, NewVal[complex128]())
RegisterType(Complex64T, NewVal[complex64]())
RegisterType(RateT, NewVal[Rate]())
RegisterType(SizeT, NewVal[Size]())
RegisterType(TimestampT, NewTime(TimestampT, ""))
RegisterType(DateTimeT, NewTime(DateTimeT, time.DateTime))
RegisterType(DateT, NewTime(DateT, time.DateOnly))
RegisterType(TimeT, NewTime(TimeT, time.TimeOnly))
RegisterType(DurationT, NewVal[time.Duration]())
RegisterType(CountT, NewVal[uint64](CountT), NoArg(true, ""))
RegisterType(PathT, NewVal[string](PathT))
// register text marshal types
RegisterTextType(func() (*big.Int, error) {
return big.NewInt(0), nil
})
RegisterTextType(func() (*big.Float, error) {
return big.NewFloat(0), nil
})
RegisterTextType(func() (*big.Rat, error) {
return big.NewRat(0, 1), nil
})
RegisterTextType(func() (*netip.Addr, error) {
return new(netip.Addr), nil
})
RegisterTextType(func() (*netip.AddrPort, error) {
return new(netip.AddrPort), nil
})
RegisterTextType(func() (*netip.Prefix, error) {
return new(netip.Prefix), nil
})
RegisterTextType(func() (*regexp.Regexp, error) {
return new(regexp.Regexp), nil
})
// register binary marshal types
RegisterBinaryType(func() (*url.URL, error) {
return new(url.URL), nil
})
}
// RegisterType registers a type.
func RegisterType(typ Type, f func() (Value, error), opts ...Option) {
typeNews[typ], typeFlagOpts[typ] = f, opts
}
// RegisterTypeName registers a type name.
func RegisterTypeName(typ Type, names ...string) {
for _, name := range names {
reflectTypes[name] = typ
}
}
// RegisterTextType registers a new text type.
func RegisterTextType[T TextMarshalUnmarshaler](f func() (T, error)) {
registerMarshaler[T](func() (any, error) { return f() }, typeTextNews)
}
// RegisterBinaryType registers a new binary type.
func RegisterBinaryType[T BinaryMarshalUnmarshaler](f func() (T, error)) {
registerMarshaler[T](func() (any, error) { return f() }, typeBinaryNews)
}
// registerMarshaler registers a type marshaler.
func registerMarshaler[T any](f func() (any, error), m map[Type]func() (any, error)) {
typ := typeType[T]()
if _, ok := typeNews[typ]; ok {
return
}
if _, ok := m[typ]; ok {
return
}
typeNews[typ], m[typ] = NewVal[T](typ), f
}
// TextMarshalUnmarshaler is the text marshal interface.
type TextMarshalUnmarshaler interface {
encoding.TextMarshaler
encoding.TextUnmarshaler
}
// BinaryMarshalUnmarshaler is the binary marshal interface.
type BinaryMarshalUnmarshaler interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
// typeType returns the type for T.
func typeType[T any]() Type {
var v T
return typeRef(v)
}
// typeRef returns the type for val.
func typeRef(val any) Type {
switch val.(type) {
case []byte:
return BytesT
case string:
return StringT
case []rune:
return RunesT
case bool:
return BoolT
case int64:
return Int64T
case int32:
return Int32T
case int16:
return Int16T
case int8:
return Int8T
case int:
return IntT
case uint64:
return Uint64T
case uint32:
return Uint32T
case uint16:
return Uint16T
case uint8:
return Uint8T
case uint:
return UintT
case float64:
return Float64T
case float32:
return Float32T
case complex128:
return Complex128T
case complex64:
return Complex64T
case time.Time:
return TimestampT
case time.Duration:
return DurationT
case Size:
return SizeT
case Rate:
return RateT
case *big.Int:
return BigIntT
case *big.Float:
return BigFloatT
case *big.Rat:
return BigRatT
case *netip.Addr:
return AddrT
case *netip.AddrPort:
return AddrPortT
case *netip.Prefix:
return CIDRT
case *regexp.Regexp:
return RegexpT
case *url.URL:
return URLT
}
s := reflect.TypeOf(val).String()
if typ, ok := reflectTypes[s]; ok {
return typ
}
return Type(s)
}
// defaultType returns the type, map key type, and element type of v.
func defaultType(refType reflect.Type) (Type, Type, Type, error) {
switch refType.Kind() {
case reflect.String:
return StringT, StringT, StringT, nil
case reflect.Bool:
return BoolT, StringT, StringT, nil
case reflect.Int64:
return Int64T, StringT, StringT, nil
case reflect.Int32:
return Int32T, StringT, StringT, nil
case reflect.Int16:
return Int16T, StringT, StringT, nil
case reflect.Int8:
return Int8T, StringT, StringT, nil
case reflect.Int:
return IntT, StringT, StringT, nil
case reflect.Uint64:
return Uint64T, StringT, StringT, nil
case reflect.Uint32:
return Uint32T, StringT, StringT, nil
case reflect.Uint16:
return Uint16T, StringT, StringT, nil
case reflect.Uint8:
return Uint8T, StringT, StringT, nil
case reflect.Uint:
return UintT, StringT, StringT, nil
case reflect.Float64:
return Float64T, StringT, StringT, nil
case reflect.Float32:
return Float32T, StringT, StringT, nil
case reflect.Complex128:
return Complex128T, StringT, StringT, nil
case reflect.Complex64:
return Complex128T, StringT, StringT, nil
case reflect.Pointer:
if typ := reflectType(refType); typ != "" {
return typ, StringT, StringT, nil
}
case reflect.Slice:
elem := refType.Elem()
if elem.Kind() == reflect.Slice && elem.Elem().Kind() == reflect.Uint8 {
return SliceT, StringT, BytesT, nil
}
if typ, _, _, err := defaultType(elem); err == nil {
return SliceT, StringT, typ, nil
}
case reflect.Map:
if mapKey, _, _, err := defaultType(refType.Key()); err == nil {
if elem, _, _, err := defaultType(refType.Elem()); err == nil {
return MapT, mapKey, elem, nil
}
}
}
if refType == timeType {
return TimestampT, StringT, StringT, nil
}
return "", "", "", ErrInvalidType
}
// reflectType returns the [Type] for the reflect type.
func reflectType(refType reflect.Type) Type {
s := refType.String()
switch s {
case "*big.Int":
return BigIntT
case "*big.Float":
return BigFloatT
case "*big.Rat":
return BigRatT
case "*netip.Addr":
return AddrT
case "*netip.AddrPort":
return AddrPortT
case "*netip.Prefix":
return CIDRT
case "*regexp.Regexp":
return RegexpT
case "*url.URL":
return URLT
}
if typ, ok := reflectTypes[s]; ok {
return typ
}
return ""
}
var timeType = reflect.TypeOf(time.Time{})