map.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package util
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "strconv"
  6. "fmt"
  7. )
  8. // Map
  9. type Map map[string]interface{}
  10. // Exist judge if the key exist in map
  11. func (m Map) Exist(key string) bool {
  12. if _, ok := m[key]; !ok {
  13. return false
  14. }
  15. return true
  16. }
  17. // String return the value type string by key
  18. func (m Map) String(key string) string {
  19. var val string
  20. var ok bool
  21. if val, ok = m[key].(string); ok {
  22. return val
  23. }
  24. res, _ := TransType(m[key], reflect.String)
  25. if val, ok = res.(string); !ok {
  26. return ""
  27. }
  28. return val
  29. }
  30. // Int return the value type int by key
  31. func (m Map) Int(key string) int {
  32. var val int
  33. var ok bool
  34. if val, ok = m[key].(int); ok {
  35. return val
  36. }
  37. res, _ := TransType(m[key], reflect.Int)
  38. if val, ok = res.(int); !ok {
  39. return 0
  40. }
  41. return val
  42. }
  43. // Int return the value type int32 by key
  44. func (m Map) Int32(key string) int32 {
  45. var val int32
  46. var ok bool
  47. if val, ok = m[key].(int32); ok {
  48. return val
  49. }
  50. res, _ := TransType(m[key], reflect.Int32)
  51. if val, ok = res.(int32); !ok {
  52. return 0
  53. }
  54. return val
  55. }
  56. // Int return the value type int64 by key
  57. func (m Map) Int64(key string) int64 {
  58. var val int64
  59. var ok bool
  60. if val, ok = m[key].(int64); ok {
  61. return val
  62. }
  63. res, _ := TransType(m[key], reflect.Int64)
  64. if val, ok = res.(int64); !ok {
  65. return 0
  66. }
  67. return val
  68. }
  69. // Int return the value type float32 by key
  70. func (m Map) Float32(key string) float32 {
  71. var val float32
  72. var ok bool
  73. if val, ok = m[key].(float32); ok {
  74. return val
  75. }
  76. res, _ := TransType(m[key], reflect.Float32)
  77. if val, ok = res.(float32); !ok {
  78. return 0
  79. }
  80. return val
  81. }
  82. // Int return the value type float32 by key
  83. func (m Map) Float64(key string) float64 {
  84. var val float64
  85. var ok bool
  86. if val, ok = m[key].(float64); ok {
  87. return val
  88. }
  89. res, _ := TransType(m[key], reflect.Float64)
  90. if val, ok = res.(float64); !ok {
  91. return 0
  92. }
  93. return val
  94. }
  95. // Int return the value type Map by key
  96. func (m Map) Map(key string) Map {
  97. var val Map
  98. var ok bool
  99. if val, ok = m[key].(Map); !ok {
  100. return nil
  101. }
  102. return val
  103. }
  104. // S2Json trans data to json, e.g struct, map and so on.
  105. func S2Json(data interface{}) string {
  106. bys, _ := json.Marshal(data)
  107. return string(bys)
  108. }
  109. // Json2S trans json to object
  110. func Json2S(src string, dest interface{}) error {
  111. return json.Unmarshal([]byte(src), dest)
  112. }
  113. func TransType(val interface{}, descType reflect.Kind) (interface{}, error) {
  114. if val == nil {
  115. return nil, Error("val is nil")
  116. }
  117. typ := reflect.TypeOf(val).Kind()
  118. switch typ {
  119. case reflect.Int:
  120. res, _ := val.(int)
  121. switch descType {
  122. case reflect.Int:
  123. return int(res), nil
  124. case reflect.Int32:
  125. return int32(res), nil
  126. case reflect.Int64:
  127. return int64(res), nil
  128. case reflect.Float32:
  129. return float32(res), nil
  130. case reflect.Float64:
  131. return float64(res), nil
  132. case reflect.String:
  133. return fmt.Sprintf("%v", res), nil
  134. }
  135. case reflect.Int32:
  136. res, _ := val.(int32)
  137. switch descType {
  138. case reflect.Int:
  139. return int(res), nil
  140. case reflect.Int32:
  141. return int32(res), nil
  142. case reflect.Int64:
  143. return int64(res), nil
  144. case reflect.Float32:
  145. return float32(res), nil
  146. case reflect.Float64:
  147. return float64(res), nil
  148. case reflect.String:
  149. return fmt.Sprintf("%v", res), nil
  150. }
  151. case reflect.Int64:
  152. res, _ := val.(int64)
  153. switch descType {
  154. case reflect.Int:
  155. return int(res), nil
  156. case reflect.Int32:
  157. return int32(res), nil
  158. case reflect.Int64:
  159. return int64(res), nil
  160. case reflect.Float32:
  161. return float32(res), nil
  162. case reflect.Float64:
  163. return float64(res), nil
  164. case reflect.String:
  165. return fmt.Sprintf("%v", res), nil
  166. }
  167. case reflect.Float32:
  168. res, _ := val.(float32)
  169. switch descType {
  170. case reflect.Int:
  171. return int(res), nil
  172. case reflect.Int32:
  173. return int32(res), nil
  174. case reflect.Int64:
  175. return int64(res), nil
  176. case reflect.Float32:
  177. return float32(res), nil
  178. case reflect.Float64:
  179. return float64(res), nil
  180. case reflect.String:
  181. return fmt.Sprintf("%v", res), nil
  182. }
  183. case reflect.Float64:
  184. res, _ := val.(float64)
  185. switch descType {
  186. case reflect.Int:
  187. return int(res), nil
  188. case reflect.Int32:
  189. return int32(res), nil
  190. case reflect.Int64:
  191. return int64(res), nil
  192. case reflect.Float32:
  193. return float32(res), nil
  194. case reflect.Float64:
  195. return float64(res), nil
  196. case reflect.String:
  197. return fmt.Sprintf("%v", res), nil
  198. }
  199. case reflect.String:
  200. res, _ := val.(string)
  201. switch descType {
  202. case reflect.Int:
  203. s_res, _ := strconv.Atoi(res)
  204. return s_res, nil
  205. case reflect.Int32:
  206. s_res, _ := strconv.Atoi(res)
  207. return int32(s_res), nil
  208. case reflect.Int64:
  209. s_res, _ := strconv.Atoi(res)
  210. return int64(s_res), nil
  211. case reflect.Float32:
  212. s_res, _ := strconv.ParseFloat(res, 32)
  213. return float32(s_res), nil
  214. case reflect.Float64:
  215. s_res, _ := strconv.ParseFloat(res, 64)
  216. return float64(s_res), nil
  217. case reflect.String:
  218. return fmt.Sprintf("%v", res), nil
  219. }
  220. }
  221. return nil, Error("invalid value type(%v)", typ)
  222. }