convert.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package utilx
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "reflect"
  7. "strconv"
  8. )
  9. // MustString 类型转换-返回string
  10. func MustString(v interface{}, defaultval ...string) string {
  11. val, ok := TryString(v)
  12. if ok {
  13. return val
  14. }
  15. if len(defaultval) > 0 {
  16. return defaultval[0]
  17. }
  18. return ""
  19. }
  20. // TryString 类型转换-转换为string
  21. func TryString(v interface{}) (string, bool) {
  22. switch tv := v.(type) {
  23. case string:
  24. return tv, true
  25. case []byte:
  26. return string(tv), true
  27. case int64:
  28. return strconv.FormatInt(int64(tv), 10), true
  29. case uint64:
  30. return strconv.FormatUint(uint64(tv), 10), true
  31. case int32:
  32. return strconv.FormatInt(int64(tv), 10), true
  33. case uint32:
  34. return strconv.FormatUint(uint64(tv), 10), true
  35. case int16:
  36. return strconv.FormatInt(int64(tv), 10), true
  37. case uint16:
  38. return strconv.FormatUint(uint64(tv), 10), true
  39. case int8:
  40. return strconv.FormatInt(int64(tv), 10), true
  41. case uint8:
  42. return strconv.FormatUint(uint64(tv), 10), true
  43. case float32:
  44. return strconv.FormatFloat(float64(tv), 'f', -1, 64), true
  45. case float64:
  46. return strconv.FormatFloat(float64(tv), 'f', -1, 64), true
  47. case int:
  48. return strconv.Itoa(int(tv)), true
  49. case json.Number:
  50. return tv.String(), true
  51. case bool:
  52. if tv {
  53. return "true", true
  54. } else {
  55. return "false", true
  56. }
  57. }
  58. return "", false
  59. }
  60. // StructToMap 结构体转换为 map[string]interface{}
  61. func StructToMap(data interface{}) map[string]interface{} {
  62. result := make(map[string]interface{})
  63. if data == nil {
  64. return result
  65. }
  66. val := reflect.ValueOf(data)
  67. typ := val.Type()
  68. for i := 0; i < val.NumField(); i++ {
  69. field := typ.Field(i)
  70. fieldName := field.Tag.Get("json") //优先读取json标签内容
  71. if fieldName == "" {
  72. fieldName = field.Name
  73. }
  74. fieldValue := val.Field(i).Interface()
  75. result[fieldName] = fieldValue
  76. }
  77. return result
  78. }
  79. // MapToStruct map[string]interface{} 转换为结构体
  80. func MapToStruct(data map[string]interface{}, result interface{}) error {
  81. val := reflect.ValueOf(result)
  82. if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
  83. return fmt.Errorf("result must be a pointer to a struct")
  84. }
  85. val = val.Elem()
  86. typ := val.Type()
  87. for i := 0; i < val.NumField(); i++ {
  88. fieldName := typ.Field(i).Name
  89. fieldValue, ok := data[fieldName]
  90. if !ok {
  91. // 如果 map 中没有结构体字段对应的键,跳过
  92. continue
  93. }
  94. val.Field(i).Set(reflect.ValueOf(fieldValue))
  95. }
  96. return nil
  97. }
  98. // MustInt64 强制返回int64
  99. func MustInt64(v interface{}, defaultval ...int64) int64 {
  100. var defaultValue int64 = 0
  101. if len(defaultval) > 0 {
  102. defaultValue = defaultval[0]
  103. }
  104. if v == nil {
  105. return defaultValue
  106. }
  107. switch tv := v.(type) {
  108. case float32:
  109. if tv > float32(math.MaxInt64) {
  110. return defaultValue
  111. }
  112. return int64(tv)
  113. case float64:
  114. if tv > float64(math.MaxInt64) {
  115. return defaultValue
  116. }
  117. return int64(tv)
  118. }
  119. val, ok := TryInt64(v)
  120. if ok {
  121. return val
  122. }
  123. return defaultValue
  124. }
  125. // TryInt64 类型转换-int64
  126. func TryInt64(v interface{}) (int64, bool) {
  127. if v == nil {
  128. return -1, false
  129. }
  130. switch tv := v.(type) {
  131. case []byte:
  132. res, err := strconv.ParseInt(string(tv), 10, 0)
  133. if err != nil {
  134. return -1, false
  135. }
  136. return res, true
  137. case string:
  138. res, err := strconv.ParseInt(tv, 10, 0)
  139. if err != nil {
  140. return -1, false
  141. }
  142. return res, true
  143. case int64:
  144. return tv, true
  145. case uint64:
  146. if tv > uint64(math.MaxInt64) {
  147. return -1, false
  148. }
  149. return int64(tv), true
  150. case int32:
  151. return int64(tv), true
  152. case uint32:
  153. return int64(tv), true
  154. case int:
  155. return int64(tv), true
  156. case int16:
  157. return int64(tv), true
  158. case uint16:
  159. return int64(tv), true
  160. case int8:
  161. return int64(tv), true
  162. case uint8:
  163. return int64(tv), true
  164. case json.Number:
  165. val, err := tv.Int64()
  166. if err == nil {
  167. return val, true
  168. }
  169. }
  170. return -1, false
  171. }