common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package column
  2. import (
  3. "fmt"
  4. "net"
  5. "reflect"
  6. "time"
  7. )
  8. type ErrUnexpectedType struct {
  9. Column Column
  10. T interface{}
  11. }
  12. func (err *ErrUnexpectedType) Error() string {
  13. return fmt.Sprintf("%s: unexpected type %T", err.Column, err.T)
  14. }
  15. var columnBaseTypes = map[interface{}]reflect.Value{
  16. int8(0): reflect.ValueOf(int8(0)),
  17. int16(0): reflect.ValueOf(int16(0)),
  18. int32(0): reflect.ValueOf(int32(0)),
  19. int64(0): reflect.ValueOf(int64(0)),
  20. uint8(0): reflect.ValueOf(uint8(0)),
  21. uint16(0): reflect.ValueOf(uint16(0)),
  22. uint32(0): reflect.ValueOf(uint32(0)),
  23. uint64(0): reflect.ValueOf(uint64(0)),
  24. float32(0): reflect.ValueOf(float32(0)),
  25. float64(0): reflect.ValueOf(float64(0)),
  26. string(""): reflect.ValueOf(string("")),
  27. time.Time{}: reflect.ValueOf(time.Time{}),
  28. IPv4{}: reflect.ValueOf(net.IPv4zero),
  29. IPv6{}: reflect.ValueOf(net.IPv6unspecified),
  30. }
  31. type ptrTo uint8
  32. const (
  33. ptrInt8T ptrTo = iota
  34. ptrInt16T
  35. ptrInt32T
  36. ptrInt64T
  37. ptrUInt8T
  38. ptrUInt16T
  39. ptrUInt32T
  40. ptrUInt64T
  41. ptrFloat32
  42. ptrFloat64
  43. ptrString
  44. ptrTime
  45. ptrIPv4
  46. ptrIPv6
  47. )
  48. var arrayBaseTypes = map[interface{}]reflect.Type{
  49. int8(0): reflect.ValueOf(int8(0)).Type(),
  50. int16(0): reflect.ValueOf(int16(0)).Type(),
  51. int32(0): reflect.ValueOf(int32(0)).Type(),
  52. int64(0): reflect.ValueOf(int64(0)).Type(),
  53. uint8(0): reflect.ValueOf(uint8(0)).Type(),
  54. uint16(0): reflect.ValueOf(uint16(0)).Type(),
  55. uint32(0): reflect.ValueOf(uint32(0)).Type(),
  56. uint64(0): reflect.ValueOf(uint64(0)).Type(),
  57. float32(0): reflect.ValueOf(float32(0)).Type(),
  58. float64(0): reflect.ValueOf(float64(0)).Type(),
  59. string(""): reflect.ValueOf(string("")).Type(),
  60. time.Time{}: reflect.ValueOf(time.Time{}).Type(),
  61. IPv4{}: reflect.ValueOf(net.IPv4zero).Type(),
  62. IPv6{}: reflect.ValueOf(net.IPv6unspecified).Type(),
  63. // nullable
  64. ptrInt8T: reflect.PtrTo(reflect.ValueOf(int8(0)).Type()),
  65. ptrInt16T: reflect.PtrTo(reflect.ValueOf(int16(0)).Type()),
  66. ptrInt32T: reflect.PtrTo(reflect.ValueOf(int32(0)).Type()),
  67. ptrInt64T: reflect.PtrTo(reflect.ValueOf(int64(0)).Type()),
  68. ptrUInt8T: reflect.PtrTo(reflect.ValueOf(uint8(0)).Type()),
  69. ptrUInt16T: reflect.PtrTo(reflect.ValueOf(uint16(0)).Type()),
  70. ptrUInt32T: reflect.PtrTo(reflect.ValueOf(uint32(0)).Type()),
  71. ptrUInt64T: reflect.PtrTo(reflect.ValueOf(uint64(0)).Type()),
  72. ptrFloat32: reflect.PtrTo(reflect.ValueOf(float32(0)).Type()),
  73. ptrFloat64: reflect.PtrTo(reflect.ValueOf(float64(0)).Type()),
  74. ptrString: reflect.PtrTo(reflect.ValueOf(string("")).Type()),
  75. ptrTime: reflect.PtrTo(reflect.ValueOf(time.Time{}).Type()),
  76. ptrIPv4: reflect.PtrTo(reflect.ValueOf(net.IPv4zero).Type()),
  77. ptrIPv6: reflect.PtrTo(reflect.ValueOf(net.IPv6unspecified).Type()),
  78. }
  79. type base struct {
  80. name, chType string
  81. valueOf reflect.Value
  82. }
  83. func (base *base) Name() string {
  84. return base.name
  85. }
  86. func (base *base) CHType() string {
  87. return base.chType
  88. }
  89. func (base *base) ScanType() reflect.Type {
  90. return base.valueOf.Type()
  91. }
  92. func (base *base) defaultValue() interface{} {
  93. return base.valueOf.Interface()
  94. }
  95. func (base *base) String() string {
  96. return fmt.Sprintf("%s (%s)", base.name, base.chType)
  97. }
  98. func (base *base) Depth() int {
  99. return 0
  100. }