utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package utils
  2. import (
  3. "database/sql"
  4. "reflect"
  5. "hash/crc32"
  6. "strconv"
  7. "time"
  8. uuid "github.com/satori/go.uuid"
  9. )
  10. func GetUUID() string {
  11. return uuid.NewV4().String()
  12. }
  13. //the result likes 1423361979
  14. func GetTimestamp() int64 {
  15. return time.Now().Unix()
  16. }
  17. func Int2String(val int64) string {
  18. return strconv.Itoa(int(val))
  19. }
  20. func ValidTime(t time.Time) bool {
  21. t1970, _ := time.Parse("2006-01-02", "1970-01-01")
  22. return t.After(t1970)
  23. }
  24. func String2Int64(str string) int64 {
  25. number, _ := strconv.ParseInt(str, 10, 64)
  26. return number
  27. }
  28. func HashCode(s string) int {
  29. v := int(crc32.ChecksumIEEE([]byte(s)))
  30. if v >= 0 {
  31. return v
  32. }
  33. if -v >= 0 {
  34. return -v
  35. }
  36. return 0
  37. }
  38. func GetTimeYmd() time.Time {
  39. timeNow := time.Now()
  40. return time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
  41. }
  42. func UnmarshalMysqlRows(row interface{}) []map[string]interface{} {
  43. tyR := reflect.TypeOf(row)
  44. if tyR.Kind() != reflect.Slice {
  45. panic("row must slice")
  46. }
  47. vR := reflect.ValueOf(row)
  48. var rows []map[string]interface{}
  49. for i := 0; i < vR.Len(); i++ {
  50. value := vR.Index(i).Interface()
  51. r := UnmarshalMysqlRow(value)
  52. rows = append(rows, r)
  53. }
  54. return rows
  55. }
  56. func UnmarshalMysqlRow(row interface{}) map[string]interface{} {
  57. tyR := reflect.TypeOf(row)
  58. if tyR.Kind() != reflect.Struct {
  59. panic("row must struct")
  60. }
  61. tyV := reflect.ValueOf(row)
  62. var Row = make(map[string]interface{})
  63. for i := 0; i < tyR.NumField(); i++ {
  64. tag := tyR.Field(i).Tag.Get("db")
  65. if tag == "" {
  66. tag = tyR.Field(i).Name
  67. }
  68. tyF := reflect.TypeOf(tyV.Field(i).Interface())
  69. switch tyF.Kind() {
  70. case reflect.Struct:
  71. switch tyV.Field(i).Type().String() {
  72. case "sql.NullTime":
  73. reStruct := tyV.Field(i).Interface().(sql.NullTime)
  74. if !reStruct.Time.Equal(time.Time{}) && reStruct.Time.Unix() != 0 {
  75. Row[tag] = reStruct.Time
  76. } else {
  77. Row[tag] = ""
  78. }
  79. case "sql.NullBool":
  80. reStruct := tyV.Field(i).Interface().(sql.NullBool)
  81. Row[tag] = reStruct.Bool
  82. case "sql.NullFloat64":
  83. reStruct := tyV.Field(i).Interface().(sql.NullFloat64)
  84. Row[tag] = reStruct.Float64
  85. case "sql.NullInt32":
  86. reStruct := tyV.Field(i).Interface().(sql.NullInt32)
  87. Row[tag] = reStruct.Int32
  88. case "sql.NullInt64":
  89. reStruct := tyV.Field(i).Interface().(sql.NullInt64)
  90. Row[tag] = reStruct.Int64
  91. case "sql.NullString":
  92. reStruct := tyV.Field(i).Interface().(sql.NullString)
  93. Row[tag] = reStruct.String
  94. case "time.Time":
  95. reStruct := tyV.Field(i).Interface().(time.Time)
  96. if !reStruct.Equal(time.Time{}) && reStruct.Unix() != 0 {
  97. Row[tag] = tyV.Field(i).Interface().(time.Time)
  98. } else {
  99. Row[tag] = ""
  100. }
  101. }
  102. case reflect.Bool, reflect.Int,
  103. reflect.Int8, reflect.Int16,
  104. reflect.Int32, reflect.Int64,
  105. reflect.Complex128, reflect.Uint,
  106. reflect.Uint8, reflect.Uint16,
  107. reflect.Uint32, reflect.Uint64,
  108. reflect.Uintptr, reflect.Float32,
  109. reflect.String, reflect.Float64, reflect.Complex64:
  110. Row[tag] = tyV.Field(i).Interface()
  111. }
  112. }
  113. return Row
  114. }