utils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // compatible solution to get gorm source directory with various operating systems
  16. gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
  17. }
  18. // FileWithLineNum return the file name and line number of the current file
  19. func FileWithLineNum() string {
  20. // the second caller usually from gorm internal, so set i start from 2
  21. for i := 2; i < 15; i++ {
  22. _, file, line, ok := runtime.Caller(i)
  23. if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
  24. return file + ":" + strconv.FormatInt(int64(line), 10)
  25. }
  26. }
  27. return ""
  28. }
  29. func IsValidDBNameChar(c rune) bool {
  30. return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' && c != '_' && c != '$' && c != '@'
  31. }
  32. func CheckTruth(val interface{}) bool {
  33. if v, ok := val.(bool); ok {
  34. return v
  35. }
  36. if v, ok := val.(string); ok {
  37. v = strings.ToLower(v)
  38. return v != "false"
  39. }
  40. return !reflect.ValueOf(val).IsZero()
  41. }
  42. func ToStringKey(values ...interface{}) string {
  43. results := make([]string, len(values))
  44. for idx, value := range values {
  45. if valuer, ok := value.(driver.Valuer); ok {
  46. value, _ = valuer.Value()
  47. }
  48. switch v := value.(type) {
  49. case string:
  50. results[idx] = v
  51. case []byte:
  52. results[idx] = string(v)
  53. case uint:
  54. results[idx] = strconv.FormatUint(uint64(v), 10)
  55. default:
  56. results[idx] = fmt.Sprint(reflect.Indirect(reflect.ValueOf(v)).Interface())
  57. }
  58. }
  59. return strings.Join(results, "_")
  60. }
  61. func AssertEqual(src, dst interface{}) bool {
  62. if !reflect.DeepEqual(src, dst) {
  63. if valuer, ok := src.(driver.Valuer); ok {
  64. src, _ = valuer.Value()
  65. }
  66. if valuer, ok := dst.(driver.Valuer); ok {
  67. dst, _ = valuer.Value()
  68. }
  69. return reflect.DeepEqual(src, dst)
  70. }
  71. return true
  72. }
  73. func ToString(value interface{}) string {
  74. switch v := value.(type) {
  75. case string:
  76. return v
  77. case int:
  78. return strconv.FormatInt(int64(v), 10)
  79. case int8:
  80. return strconv.FormatInt(int64(v), 10)
  81. case int16:
  82. return strconv.FormatInt(int64(v), 10)
  83. case int32:
  84. return strconv.FormatInt(int64(v), 10)
  85. case int64:
  86. return strconv.FormatInt(v, 10)
  87. case uint:
  88. return strconv.FormatUint(uint64(v), 10)
  89. case uint8:
  90. return strconv.FormatUint(uint64(v), 10)
  91. case uint16:
  92. return strconv.FormatUint(uint64(v), 10)
  93. case uint32:
  94. return strconv.FormatUint(uint64(v), 10)
  95. case uint64:
  96. return strconv.FormatUint(v, 10)
  97. }
  98. return ""
  99. }
  100. func ExistsIn(a string, list *[]string) bool {
  101. if list == nil {
  102. return false
  103. }
  104. for _, b := range *list {
  105. if b == a {
  106. return true
  107. }
  108. }
  109. return false
  110. }