type.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package pkg
  2. import (
  3. "encoding/json"
  4. "gorm.io/gorm"
  5. "strconv"
  6. )
  7. func Int64Val(value interface{}) (valueNum int64, err error) {
  8. switch value.(type) { // 判断是int类型再处理
  9. case int64:
  10. valueNum = value.(int64)
  11. case int:
  12. valueNumInt := value.(int)
  13. valueNum = int64(valueNumInt)
  14. case float64:
  15. valueNumFloat := value.(float64)
  16. valueNum = int64(valueNumFloat)
  17. default:
  18. err = gorm.ErrInvalidValue
  19. }
  20. return
  21. }
  22. func StrVal(value interface{}) string {
  23. var key string
  24. if value == nil {
  25. return key
  26. }
  27. switch value.(type) {
  28. case float64:
  29. ft := value.(float64)
  30. key = strconv.FormatFloat(ft, 'f', -1, 64)
  31. case float32:
  32. ft := value.(float32)
  33. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  34. case int:
  35. it := value.(int)
  36. key = strconv.Itoa(it)
  37. case uint:
  38. it := value.(uint)
  39. key = strconv.Itoa(int(it))
  40. case int8:
  41. it := value.(int8)
  42. key = strconv.Itoa(int(it))
  43. case uint8:
  44. it := value.(uint8)
  45. key = strconv.Itoa(int(it))
  46. case int16:
  47. it := value.(int16)
  48. key = strconv.Itoa(int(it))
  49. case uint16:
  50. it := value.(uint16)
  51. key = strconv.Itoa(int(it))
  52. case int32:
  53. it := value.(int32)
  54. key = strconv.Itoa(int(it))
  55. case uint32:
  56. it := value.(uint32)
  57. key = strconv.Itoa(int(it))
  58. case int64:
  59. it := value.(int64)
  60. key = strconv.FormatInt(it, 10)
  61. case uint64:
  62. it := value.(uint64)
  63. key = strconv.FormatUint(it, 10)
  64. case string:
  65. key = value.(string)
  66. case []byte:
  67. key = string(value.([]byte))
  68. default:
  69. newValue, _ := json.Marshal(value)
  70. key = string(newValue)
  71. }
  72. return key
  73. }