type.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package reflectutils
  2. import "reflect"
  3. // GroupTypeKind 将反射的Kind值进行聚集
  4. func GroupTypeKind(t reflect.Type) reflect.Kind {
  5. kind := t.Kind()
  6. switch {
  7. case kind >= reflect.Int && kind <= reflect.Int64:
  8. return reflect.Int64
  9. case kind >= reflect.Uint && kind <= reflect.Uint64:
  10. return reflect.Uint64
  11. case kind >= reflect.Float32 && kind <= reflect.Float64:
  12. return reflect.Float64
  13. default:
  14. return kind
  15. }
  16. }
  17. func PointerTypeElem(t reflect.Type) reflect.Type {
  18. elem := t
  19. if t.Kind() == reflect.Ptr {
  20. elem = t.Elem()
  21. }
  22. return elem
  23. }
  24. func SliceTypeElem(t reflect.Type) reflect.Type {
  25. elem := t
  26. if t.Kind() == reflect.Slice {
  27. elem = t.Elem()
  28. }
  29. return elem
  30. }
  31. func IsSliceTypeOf(t reflect.Type, elementKind reflect.Kind) bool {
  32. return t.Kind() == reflect.Slice && t.Elem().Kind() == elementKind
  33. }
  34. func IsTypeInteger(t reflect.Type) bool {
  35. return t.Kind() >= reflect.Int && t.Kind() <= reflect.Int64
  36. }
  37. func IsTypeUnsignedInteger(t reflect.Type) bool {
  38. return t.Kind() >= reflect.Uint && t.Kind() <= reflect.Uint64
  39. }
  40. func IsTypeFloat(t reflect.Type) bool {
  41. return t.Kind() >= reflect.Float32 && t.Kind() <= reflect.Float64
  42. }
  43. func IsTypeStructPointer(t reflect.Type) bool {
  44. return t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct
  45. }
  46. func IsTypeStructOrStructPointer(t reflect.Type) bool {
  47. return t.Kind() == reflect.Struct || IsTypeStructPointer(t)
  48. }
  49. func IsTypeStructSliceOrStructSlicePointer(t reflect.Type) bool {
  50. return IsSliceTypeOf(t, reflect.Struct) || (t.Kind() == reflect.Pointer && IsSliceTypeOf(t.Elem(), reflect.Struct))
  51. }
  52. func IsTypeTime(t reflect.Type) bool {
  53. return t.Kind() == reflect.Struct && t.String() == "time.Time"
  54. }
  55. func IsTypeTimePointer(t reflect.Type) bool {
  56. return t.Kind() == reflect.Pointer && IsTypeTime(t.Elem())
  57. }
  58. func ZeroType(t reflect.Type) reflect.Value {
  59. zeroValue := reflect.New(t).Elem()
  60. if zeroValue.Kind() != reflect.Pointer {
  61. zero(&zeroValue)
  62. return zeroValue
  63. }
  64. zeroValue.Set(reflect.New(zeroValue.Type().Elem()))
  65. elemValue := PointerValueElem(zeroValue)
  66. zero(&elemValue)
  67. return zeroValue
  68. }
  69. func ZeroTypeToAny(t reflect.Type) any {
  70. zeroValue := reflect.New(t).Elem()
  71. if zeroValue.Kind() != reflect.Pointer {
  72. zero(&zeroValue)
  73. return zeroValue.Interface()
  74. }
  75. zeroValue.Set(reflect.New(zeroValue.Type().Elem()))
  76. elemValue := PointerValueElem(zeroValue)
  77. zero(&elemValue)
  78. return zeroValue.Interface()
  79. }