reflectutils.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package reflectutils
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. )
  8. // GroupValueKind 将反射的Kind值进行聚集
  9. func GroupValueKind(val reflect.Value) reflect.Kind {
  10. kind := val.Kind()
  11. switch {
  12. case kind >= reflect.Int && kind <= reflect.Int64:
  13. return reflect.Int64
  14. case kind >= reflect.Uint && kind <= reflect.Uint64:
  15. return reflect.Uint64
  16. case kind >= reflect.Float32 && kind <= reflect.Float64:
  17. return reflect.Float64
  18. default:
  19. return kind
  20. }
  21. }
  22. // AssignStringValue 将any类型的值进行转化,赋值给string的reflect.Value
  23. func AssignStringValue(data any, val reflect.Value) error {
  24. if data == nil {
  25. return nil
  26. }
  27. if val.Kind() != reflect.String {
  28. return errors.New("val应当为string类型的Value")
  29. }
  30. dataVal := reflect.Indirect(reflect.ValueOf(data))
  31. dataKind := GroupValueKind(dataVal)
  32. switch dataKind {
  33. case reflect.String:
  34. val.SetString(dataVal.String())
  35. case reflect.Bool:
  36. if dataVal.Bool() {
  37. val.SetString("1")
  38. } else {
  39. val.SetString("0")
  40. }
  41. case reflect.Int64:
  42. val.SetString(strconv.FormatInt(dataVal.Int(), 10))
  43. case reflect.Uint64:
  44. val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
  45. case reflect.Float64:
  46. val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
  47. case reflect.Slice, reflect.Array:
  48. elemKind := dataVal.Type().Elem().Kind()
  49. if elemKind != reflect.Uint8 {
  50. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  51. }
  52. var uints []uint8
  53. if dataKind == reflect.Array {
  54. uints = make([]uint8, dataVal.Len(), dataVal.Len())
  55. for i := range uints {
  56. uints[i] = dataVal.Index(i).Interface().(uint8)
  57. }
  58. } else {
  59. uints = dataVal.Interface().([]uint8)
  60. }
  61. val.SetString(string(uints))
  62. default:
  63. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  64. }
  65. return nil
  66. }
  67. // AssignIntValue 将any类型的值进行转化,赋值给int类型的reflect.Value
  68. func AssignIntValue(data any, val reflect.Value) error {
  69. if data == nil {
  70. return nil
  71. }
  72. if val.Kind() < reflect.Int && val.Kind() > reflect.Int64 {
  73. return errors.New("val应当为int类型的Value")
  74. }
  75. dataVal := reflect.Indirect(reflect.ValueOf(data))
  76. dataKind := GroupValueKind(dataVal)
  77. switch dataKind {
  78. case reflect.Int64:
  79. val.SetInt(dataVal.Int())
  80. case reflect.Uint64:
  81. val.SetInt(int64(dataVal.Uint()))
  82. case reflect.Float64:
  83. val.SetInt(int64(dataVal.Float()))
  84. case reflect.Bool:
  85. if dataVal.Bool() {
  86. val.SetInt(1)
  87. } else {
  88. val.SetInt(0)
  89. }
  90. case reflect.String:
  91. str := dataVal.String()
  92. if str == "" {
  93. str = "0"
  94. }
  95. i, err := strconv.ParseInt(str, 0, val.Type().Bits())
  96. if err != nil {
  97. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  98. }
  99. val.SetInt(i)
  100. default:
  101. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  102. }
  103. return nil
  104. }
  105. // AssignUintValue 将any类型的值进行转化,赋值给uint类型的reflect.Value
  106. func AssignUintValue(data any, val reflect.Value) error {
  107. if data == nil {
  108. return nil
  109. }
  110. if val.Kind() < reflect.Uint && val.Kind() > reflect.Uint64 {
  111. return errors.New("val应当为uint类型的Value")
  112. }
  113. dataVal := reflect.Indirect(reflect.ValueOf(data))
  114. dataKind := GroupValueKind(dataVal)
  115. switch dataKind {
  116. case reflect.Int64:
  117. i := dataVal.Int()
  118. if i < 0 {
  119. return fmt.Errorf("数值溢出: %d", i)
  120. }
  121. val.SetUint(uint64(i))
  122. case reflect.Uint64:
  123. val.SetUint(dataVal.Uint())
  124. case reflect.Float64:
  125. f := dataVal.Float()
  126. if f < 0 {
  127. return fmt.Errorf("数值溢出: %f", f)
  128. }
  129. val.SetUint(uint64(f))
  130. case reflect.Bool:
  131. if dataVal.Bool() {
  132. val.SetUint(1)
  133. } else {
  134. val.SetUint(0)
  135. }
  136. case reflect.String:
  137. str := dataVal.String()
  138. if str == "" {
  139. str = "0"
  140. }
  141. i, err := strconv.ParseUint(str, 0, val.Type().Bits())
  142. if err != nil {
  143. return err
  144. }
  145. val.SetUint(i)
  146. default:
  147. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  148. }
  149. return nil
  150. }
  151. // AssignBoolValue 将any类型的值进行转化,赋值给bool类型的reflect.Value
  152. func AssignBoolValue(data any, val reflect.Value) error {
  153. if data == nil {
  154. return nil
  155. }
  156. if val.Kind() != reflect.Bool {
  157. return errors.New("val应当为bool类型的Value")
  158. }
  159. dataVal := reflect.Indirect(reflect.ValueOf(data))
  160. dataKind := GroupValueKind(dataVal)
  161. switch dataKind {
  162. case reflect.Bool:
  163. val.SetBool(dataVal.Bool())
  164. case reflect.Int64:
  165. val.SetBool(dataVal.Int() != 0)
  166. case reflect.Uint64:
  167. val.SetBool(dataVal.Uint() != 0)
  168. case reflect.Float64:
  169. val.SetBool(dataVal.Float() != 0)
  170. case reflect.String:
  171. if dataVal.String() == "" {
  172. val.SetBool(false)
  173. } else {
  174. b, err := strconv.ParseBool(dataVal.String())
  175. if err != nil {
  176. return err
  177. }
  178. val.SetBool(b)
  179. }
  180. default:
  181. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  182. }
  183. return nil
  184. }
  185. // AssignFloatValue 将any类型的值进行转化,赋值给float类型的reflect.Value
  186. func AssignFloatValue(data any, val reflect.Value) error {
  187. if data == nil {
  188. return nil
  189. }
  190. if val.Kind() != reflect.Float32 && val.Kind() != reflect.Float64 {
  191. return errors.New("val应当为float类型的Value")
  192. }
  193. dataVal := reflect.Indirect(reflect.ValueOf(data))
  194. dataKind := GroupValueKind(dataVal)
  195. switch dataKind {
  196. case reflect.Int64:
  197. val.SetFloat(float64(dataVal.Int()))
  198. case reflect.Uint64:
  199. val.SetFloat(float64(dataVal.Uint()))
  200. case reflect.Float64:
  201. val.SetFloat(dataVal.Float())
  202. case reflect.Bool:
  203. if dataVal.Bool() {
  204. val.SetFloat(1)
  205. } else {
  206. val.SetFloat(0)
  207. }
  208. case reflect.String:
  209. str := dataVal.String()
  210. if str == "" {
  211. str = "0"
  212. }
  213. f, err := strconv.ParseFloat(str, val.Type().Bits())
  214. if err == nil {
  215. return err
  216. }
  217. val.SetFloat(f)
  218. default:
  219. return errors.New("不支持的类型: " + dataVal.Type().Elem().String())
  220. }
  221. return nil
  222. }