convert.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package convert
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. )
  8. var (
  9. ErrUnsupported = errors.New("unsupported")
  10. )
  11. func isBytes(v reflect.Value) bool {
  12. if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
  13. return true
  14. }
  15. return false
  16. }
  17. func StringValue(v interface{}) (s string, err error) {
  18. if v == nil {
  19. return
  20. }
  21. refValue := reflect.Indirect(reflect.ValueOf(v))
  22. switch refValue.Kind() {
  23. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  24. s = strconv.FormatInt(refValue.Int(), 10)
  25. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  26. s = strconv.FormatUint(refValue.Uint(), 10)
  27. case reflect.Float32, reflect.Float64:
  28. s = strconv.FormatFloat(refValue.Float(), 'f', -1, 64)
  29. default:
  30. if isBytes(refValue) {
  31. s = string(refValue.Bytes())
  32. } else {
  33. s = fmt.Sprint(refValue.Interface())
  34. }
  35. }
  36. return
  37. }
  38. func IntegerValue(v interface{}) (n int64, err error) {
  39. if v == nil {
  40. return
  41. }
  42. refValue := reflect.Indirect(reflect.ValueOf(v))
  43. switch refValue.Kind() {
  44. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  45. n = refValue.Int()
  46. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  47. n = int64(refValue.Uint())
  48. case reflect.Float32, reflect.Float64:
  49. n = int64(refValue.Float())
  50. case reflect.String:
  51. n, err = strconv.ParseInt(refValue.String(), 10, 64)
  52. default:
  53. if isBytes(refValue) {
  54. n, err = strconv.ParseInt(string(refValue.Bytes()), 10, 64)
  55. } else {
  56. err = ErrUnsupported
  57. }
  58. }
  59. return
  60. }
  61. func FloatValue(v interface{}) (n float64, err error) {
  62. if v == nil {
  63. return
  64. }
  65. refValue := reflect.Indirect(reflect.ValueOf(v))
  66. switch refValue.Kind() {
  67. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  68. n = float64(refValue.Int())
  69. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  70. n = float64(refValue.Uint())
  71. case reflect.Float32, reflect.Float64:
  72. n = refValue.Float()
  73. case reflect.String:
  74. n, err = strconv.ParseFloat(refValue.String(), 64)
  75. default:
  76. if isBytes(refValue) {
  77. n, err = strconv.ParseFloat(string(refValue.Bytes()), 64)
  78. } else {
  79. err = ErrUnsupported
  80. }
  81. }
  82. return
  83. }
  84. func MustString(v interface{}) (s string) {
  85. s, _ = StringValue(v)
  86. return
  87. }
  88. func MustInteger(v interface{}) (n int64) {
  89. n, _ = IntegerValue(v)
  90. return
  91. }
  92. func MustFloat(v interface{}) (n float64) {
  93. n, _ = FloatValue(v)
  94. return
  95. }