55 "fmt"
66 "reflect"
77 "strconv"
8+ "unsafe"
89)
910
1011// per validate construct
@@ -156,7 +157,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
156157 structNs : v .str2 ,
157158 fieldLen : uint8 (len (cf .altName )),
158159 structfieldLen : uint8 (len (cf .name )),
159- value : current . Interface ( ),
160+ value : getValue ( current ),
160161 param : ct .param ,
161162 kind : kind ,
162163 typ : current .Type (),
@@ -410,7 +411,7 @@ OUTER:
410411 structNs : v .str2 ,
411412 fieldLen : uint8 (len (cf .altName )),
412413 structfieldLen : uint8 (len (cf .name )),
413- value : current . Interface ( ),
414+ value : getValue ( current ),
414415 param : ct .param ,
415416 kind : kind ,
416417 typ : typ ,
@@ -430,7 +431,7 @@ OUTER:
430431 structNs : v .str2 ,
431432 fieldLen : uint8 (len (cf .altName )),
432433 structfieldLen : uint8 (len (cf .name )),
433- value : current . Interface ( ),
434+ value : getValue ( current ),
434435 param : ct .param ,
435436 kind : kind ,
436437 typ : typ ,
@@ -470,7 +471,7 @@ OUTER:
470471 structNs : v .str2 ,
471472 fieldLen : uint8 (len (cf .altName )),
472473 structfieldLen : uint8 (len (cf .name )),
473- value : current . Interface ( ),
474+ value : getValue ( current ),
474475 param : ct .param ,
475476 kind : kind ,
476477 typ : typ ,
@@ -484,3 +485,26 @@ OUTER:
484485 }
485486
486487}
488+
489+ func getValue (val reflect.Value ) interface {} {
490+ if val .CanInterface () {
491+ return val .Interface ()
492+ }
493+
494+ if val .CanAddr () {
495+ return reflect .NewAt (val .Type (), unsafe .Pointer (val .UnsafeAddr ())).Elem ().Interface ()
496+ }
497+
498+ switch val .Kind () {
499+ case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
500+ return val .Int ()
501+ case reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr :
502+ return val .Uint ()
503+ case reflect .Complex64 , reflect .Complex128 :
504+ return val .Complex ()
505+ case reflect .Float32 , reflect .Float64 :
506+ return val .Float ()
507+ default :
508+ return val .String ()
509+ }
510+ }
0 commit comments