Skip to content

Commit b71c541

Browse files
Merge pull request #13 from tlipoca9/feature/basic_types
feature: add StringToBasicTypeHookFunc and support complex
2 parents 42da679 + 231f31b commit b71c541

File tree

4 files changed

+831
-0
lines changed

4 files changed

+831
-0
lines changed

decode_hooks.go

+243
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,246 @@ func StringToNetIPAddrPortHookFunc() DecodeHookFunc {
332332
return netip.ParseAddrPort(data.(string))
333333
}
334334
}
335+
336+
// StringToBasicTypeHookFunc returns a DecodeHookFunc that converts
337+
// strings to basic types.
338+
// int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, bool, byte, rune, complex64, complex128
339+
func StringToBasicTypeHookFunc() DecodeHookFunc {
340+
return ComposeDecodeHookFunc(
341+
StringToInt8HookFunc(),
342+
StringToUint8HookFunc(),
343+
StringToInt16HookFunc(),
344+
StringToUint16HookFunc(),
345+
StringToInt32HookFunc(),
346+
StringToUint32HookFunc(),
347+
StringToInt64HookFunc(),
348+
StringToUint64HookFunc(),
349+
StringToIntHookFunc(),
350+
StringToUintHookFunc(),
351+
StringToFloat32HookFunc(),
352+
StringToFloat64HookFunc(),
353+
StringToBoolHookFunc(),
354+
// byte and rune are aliases for uint8 and int32 respectively
355+
// StringToByteHookFunc(),
356+
// StringToRuneHookFunc(),
357+
StringToComplex64HookFunc(),
358+
StringToComplex128HookFunc(),
359+
)
360+
}
361+
362+
// StringToInt8HookFunc returns a DecodeHookFunc that converts
363+
// strings to int8.
364+
func StringToInt8HookFunc() DecodeHookFunc {
365+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
366+
if f.Kind() != reflect.String || t.Kind() != reflect.Int8 {
367+
return data, nil
368+
}
369+
370+
// Convert it by parsing
371+
i64, err := strconv.ParseInt(data.(string), 0, 8)
372+
return int8(i64), err
373+
}
374+
}
375+
376+
// StringToUint8HookFunc returns a DecodeHookFunc that converts
377+
// strings to uint8.
378+
func StringToUint8HookFunc() DecodeHookFunc {
379+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
380+
if f.Kind() != reflect.String || t.Kind() != reflect.Uint8 {
381+
return data, nil
382+
}
383+
384+
// Convert it by parsing
385+
u64, err := strconv.ParseUint(data.(string), 0, 8)
386+
return uint8(u64), err
387+
}
388+
}
389+
390+
// StringToInt16HookFunc returns a DecodeHookFunc that converts
391+
// strings to int16.
392+
func StringToInt16HookFunc() DecodeHookFunc {
393+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
394+
if f.Kind() != reflect.String || t.Kind() != reflect.Int16 {
395+
return data, nil
396+
}
397+
398+
// Convert it by parsing
399+
i64, err := strconv.ParseInt(data.(string), 0, 16)
400+
return int16(i64), err
401+
}
402+
}
403+
404+
// StringToUint16HookFunc returns a DecodeHookFunc that converts
405+
// strings to uint16.
406+
func StringToUint16HookFunc() DecodeHookFunc {
407+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
408+
if f.Kind() != reflect.String || t.Kind() != reflect.Uint16 {
409+
return data, nil
410+
}
411+
412+
// Convert it by parsing
413+
u64, err := strconv.ParseUint(data.(string), 0, 16)
414+
return uint16(u64), err
415+
}
416+
}
417+
418+
// StringToInt32HookFunc returns a DecodeHookFunc that converts
419+
// strings to int32.
420+
func StringToInt32HookFunc() DecodeHookFunc {
421+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
422+
if f.Kind() != reflect.String || t.Kind() != reflect.Int32 {
423+
return data, nil
424+
}
425+
426+
// Convert it by parsing
427+
i64, err := strconv.ParseInt(data.(string), 0, 32)
428+
return int32(i64), err
429+
}
430+
}
431+
432+
// StringToUint32HookFunc returns a DecodeHookFunc that converts
433+
// strings to uint32.
434+
func StringToUint32HookFunc() DecodeHookFunc {
435+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
436+
if f.Kind() != reflect.String || t.Kind() != reflect.Uint32 {
437+
return data, nil
438+
}
439+
440+
// Convert it by parsing
441+
u64, err := strconv.ParseUint(data.(string), 0, 32)
442+
return uint32(u64), err
443+
}
444+
}
445+
446+
// StringToInt64HookFunc returns a DecodeHookFunc that converts
447+
// strings to int64.
448+
func StringToInt64HookFunc() DecodeHookFunc {
449+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
450+
if f.Kind() != reflect.String || t.Kind() != reflect.Int64 {
451+
return data, nil
452+
}
453+
454+
// Convert it by parsing
455+
return strconv.ParseInt(data.(string), 0, 64)
456+
}
457+
}
458+
459+
// StringToUint64HookFunc returns a DecodeHookFunc that converts
460+
// strings to uint64.
461+
func StringToUint64HookFunc() DecodeHookFunc {
462+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
463+
if f.Kind() != reflect.String || t.Kind() != reflect.Uint64 {
464+
return data, nil
465+
}
466+
467+
// Convert it by parsing
468+
return strconv.ParseUint(data.(string), 0, 64)
469+
}
470+
}
471+
472+
// StringToIntHookFunc returns a DecodeHookFunc that converts
473+
// strings to int.
474+
func StringToIntHookFunc() DecodeHookFunc {
475+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
476+
if f.Kind() != reflect.String || t.Kind() != reflect.Int {
477+
return data, nil
478+
}
479+
480+
// Convert it by parsing
481+
i64, err := strconv.ParseInt(data.(string), 0, 0)
482+
return int(i64), err
483+
}
484+
}
485+
486+
// StringToUintHookFunc returns a DecodeHookFunc that converts
487+
// strings to uint.
488+
func StringToUintHookFunc() DecodeHookFunc {
489+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
490+
if f.Kind() != reflect.String || t.Kind() != reflect.Uint {
491+
return data, nil
492+
}
493+
494+
// Convert it by parsing
495+
u64, err := strconv.ParseUint(data.(string), 0, 0)
496+
return uint(u64), err
497+
}
498+
}
499+
500+
// StringToFloat32HookFunc returns a DecodeHookFunc that converts
501+
// strings to float32.
502+
func StringToFloat32HookFunc() DecodeHookFunc {
503+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
504+
if f.Kind() != reflect.String || t.Kind() != reflect.Float32 {
505+
return data, nil
506+
}
507+
508+
// Convert it by parsing
509+
f64, err := strconv.ParseFloat(data.(string), 32)
510+
return float32(f64), err
511+
}
512+
}
513+
514+
// StringToFloat64HookFunc returns a DecodeHookFunc that converts
515+
// strings to float64.
516+
func StringToFloat64HookFunc() DecodeHookFunc {
517+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
518+
if f.Kind() != reflect.String || t.Kind() != reflect.Float64 {
519+
return data, nil
520+
}
521+
522+
// Convert it by parsing
523+
return strconv.ParseFloat(data.(string), 64)
524+
}
525+
}
526+
527+
// StringToBoolHookFunc returns a DecodeHookFunc that converts
528+
// strings to bool.
529+
func StringToBoolHookFunc() DecodeHookFunc {
530+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
531+
if f.Kind() != reflect.String || t.Kind() != reflect.Bool {
532+
return data, nil
533+
}
534+
535+
// Convert it by parsing
536+
return strconv.ParseBool(data.(string))
537+
}
538+
}
539+
540+
// StringToByteHookFunc returns a DecodeHookFunc that converts
541+
// strings to byte.
542+
func StringToByteHookFunc() DecodeHookFunc {
543+
return StringToUint8HookFunc()
544+
}
545+
546+
// StringToRuneHookFunc returns a DecodeHookFunc that converts
547+
// strings to rune.
548+
func StringToRuneHookFunc() DecodeHookFunc {
549+
return StringToInt32HookFunc()
550+
}
551+
552+
// StringToComplex64HookFunc returns a DecodeHookFunc that converts
553+
// strings to complex64.
554+
func StringToComplex64HookFunc() DecodeHookFunc {
555+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
556+
if f.Kind() != reflect.String || t.Kind() != reflect.Complex64 {
557+
return data, nil
558+
}
559+
560+
// Convert it by parsing
561+
c128, err := strconv.ParseComplex(data.(string), 64)
562+
return complex64(c128), err
563+
}
564+
}
565+
566+
// StringToComplex128HookFunc returns a DecodeHookFunc that converts
567+
// strings to complex128.
568+
func StringToComplex128HookFunc() DecodeHookFunc {
569+
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
570+
if f.Kind() != reflect.String || t.Kind() != reflect.Complex128 {
571+
return data, nil
572+
}
573+
574+
// Convert it by parsing
575+
return strconv.ParseComplex(data.(string), 128)
576+
}
577+
}

0 commit comments

Comments
 (0)