util.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package client
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func ToStrings(data interface{}) []string {
  7. var i []string
  8. if vv, ok := data.([]interface{}); ok {
  9. for _, in := range vv {
  10. i = append(i, fmt.Sprint(in))
  11. }
  12. }
  13. return i
  14. }
  15. func ToString(data interface{}) string {
  16. if data == nil {
  17. return ""
  18. }
  19. return fmt.Sprint(data)
  20. }
  21. func ToFloat64(data interface{}) float64 {
  22. var i float64
  23. if vv, ok := data.(float64); ok {
  24. i = vv
  25. return i
  26. }
  27. if vv, ok := data.(int64); ok {
  28. i = float64(vv)
  29. return i
  30. }
  31. return i
  32. }
  33. func ToFloat32(data interface{}) float32 {
  34. if v, ok := data.(float32); ok {
  35. return v
  36. }
  37. if v, ok := data.(float64); ok {
  38. return float32(v)
  39. }
  40. if v, ok := data.(int64); ok {
  41. return float32(v)
  42. }
  43. return 0
  44. }
  45. const (
  46. GRPC_DATETIME_FROM_SQL_FORMAT = "2006-01-02T15:04:05.999999999-07:00"
  47. GRPC_DATETIME_FROM_SQL_FORMAT2 = "2006-01-02T15:04:05.999999999Z"
  48. )
  49. func ToTime(data interface{}) (*time.Time, error) {
  50. if v, ok := data.(time.Time); ok {
  51. return &v, nil
  52. }
  53. str := ToString(data)
  54. dateTime, err := time.Parse(GRPC_DATETIME_FROM_SQL_FORMAT, str)
  55. if err != nil {
  56. dateTime, err = time.Parse(GRPC_DATETIME_FROM_SQL_FORMAT2, str)
  57. if err != nil {
  58. return nil, err
  59. }
  60. }
  61. return &dateTime, nil
  62. }