utils.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package utils
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "reflect"
  6. "regexp"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "unicode"
  11. )
  12. var gormSourceDir string
  13. func init() {
  14. _, file, _, _ := runtime.Caller(0)
  15. gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
  16. }
  17. func FileWithLineNum() string {
  18. for i := 2; i < 15; i++ {
  19. _, file, line, ok := runtime.Caller(i)
  20. if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
  21. return file + ":" + strconv.FormatInt(int64(line), 10)
  22. }
  23. }
  24. return ""
  25. }
  26. func IsValidDBNameChar(c rune) bool {
  27. return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && c != '@'
  28. }
  29. func CheckTruth(val interface{}) bool {
  30. if v, ok := val.(bool); ok {
  31. return v
  32. }
  33. if v, ok := val.(string); ok {
  34. v = strings.ToLower(v)
  35. return v != "false"
  36. }
  37. return !reflect.ValueOf(val).IsZero()
  38. }
  39. func ToStringKey(values ...interface{}) string {
  40. results := make([]string, len(values))
  41. for idx, value := range values {
  42. if valuer, ok := value.(driver.Valuer); ok {
  43. value, _ = valuer.Value()
  44. }
  45. switch v := value.(type) {
  46. case string:
  47. results[idx] = v
  48. case []byte:
  49. results[idx] = string(v)
  50. case uint:
  51. results[idx] = strconv.FormatUint(uint64(v), 10)
  52. default:
  53. results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
  54. }
  55. }
  56. return strings.Join(results, "_")
  57. }
  58. func AssertEqual(src, dst interface{}) bool {
  59. if !reflect.DeepEqual(src, dst) {
  60. if valuer, ok := src.(driver.Valuer); ok {
  61. src, _ = valuer.Value()
  62. }
  63. if valuer, ok := dst.(driver.Valuer); ok {
  64. dst, _ = valuer.Value()
  65. }
  66. return reflect.DeepEqual(src, dst)
  67. }
  68. return true
  69. }
  70. func ToString(value interface{}) string {
  71. switch v := value.(type) {
  72. case string:
  73. return v
  74. case int:
  75. return strconv.FormatInt(int64(v), 10)
  76. case int8:
  77. return strconv.FormatInt(int64(v), 10)
  78. case int16:
  79. return strconv.FormatInt(int64(v), 10)
  80. case int32:
  81. return strconv.FormatInt(int64(v), 10)
  82. case int64:
  83. return strconv.FormatInt(v, 10)
  84. case uint:
  85. return strconv.FormatUint(uint64(v), 10)
  86. case uint8:
  87. return strconv.FormatUint(uint64(v), 10)
  88. case uint16:
  89. return strconv.FormatUint(uint64(v), 10)
  90. case uint32:
  91. return strconv.FormatUint(uint64(v), 10)
  92. case uint64:
  93. return strconv.FormatUint(v, 10)
  94. }
  95. return ""
  96. }