常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
6.1 KiB

3 years ago
3 years ago
  1. package helper
  2. import (
  3. "errors"
  4. "net/url"
  5. "sort"
  6. "strings"
  7. )
  8. func MergeStringMap(x, y map[string]string) map[string]string {
  9. n := make(map[string]string)
  10. for i, v := range x {
  11. for j, w := range y {
  12. if i == j {
  13. n[i] = w
  14. } else {
  15. if _, ok := n[i]; !ok {
  16. n[i] = v
  17. }
  18. if _, ok := n[j]; !ok {
  19. n[j] = w
  20. }
  21. }
  22. }
  23. }
  24. return n
  25. }
  26. /**
  27. * 转换为map string数组
  28. * 2021/1/5
  29. * gz
  30. */
  31. func ToMapStringArray(x interface{}) ([]map[string]string, error) {
  32. list, ok := x.([]interface{})
  33. if !ok {
  34. return []map[string]string{}, errors.New("type error")
  35. }
  36. if len(list) < 1 {
  37. return []map[string]string{}, nil
  38. }
  39. var ret []map[string]string = make([]map[string]string, len(list))
  40. var tmp map[string]interface{}
  41. var map_tmp map[string]string
  42. var err error
  43. for key, item := range list {
  44. if tmp, ok = item.(map[string]interface{}); ok {
  45. map_tmp = make(map[string]string)
  46. for k, v := range tmp {
  47. map_tmp[k] = ToStr(v)
  48. }
  49. ret[key] = map_tmp
  50. } else {
  51. err = errors.New("data type error")
  52. break
  53. }
  54. }
  55. return ret, err
  56. }
  57. /**
  58. * 将map[string]string数组转成map[string]interface数组
  59. */
  60. func MapStringToInterfaceArr(src []map[string]string) []map[string]interface{} {
  61. var dest []map[string]interface{}
  62. for i := range src {
  63. m := src[i]
  64. n := make(map[string]interface{})
  65. for k, v := range m {
  66. n[k] = interface{}(v)
  67. }
  68. dest = append(dest, n)
  69. }
  70. return dest
  71. }
  72. /**
  73. * 将map[string]string转成map[string]interface
  74. */
  75. func MapStringToInterface(src map[string]string) map[string]interface{} {
  76. target := make(map[string]interface{})
  77. for k, v := range src {
  78. target[k] = interface{}(v)
  79. }
  80. return target
  81. }
  82. //将interface转成map[string]interface
  83. func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) {
  84. dataJson, err := json.Marshal(data)
  85. if err != nil {
  86. return nil, err
  87. }
  88. var returnData map[string]interface{}
  89. err = json.Unmarshal(dataJson, &returnData)
  90. return returnData, nil
  91. }
  92. //将interface转成map[string]string
  93. func InterfaceToMapString(data interface{}) (map[string]string, error) {
  94. dataJson, err := json.Marshal(data)
  95. if err != nil {
  96. return nil, err
  97. }
  98. var returnData map[string]string
  99. err = json.Unmarshal(dataJson, &returnData)
  100. return returnData, nil
  101. }
  102. //将interface转成[]map[string]interface
  103. func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, error) {
  104. dataJson, err := json.Marshal(data)
  105. if err != nil {
  106. return nil, err
  107. }
  108. var returnData []map[string]interface{}
  109. err = json.Unmarshal(dataJson, &returnData)
  110. return returnData, nil
  111. }
  112. //将interface转成[]map[string]string
  113. func InterfaceToMapStringArr(data interface{}) ([]map[string]string, error) {
  114. dataJson, err := json.Marshal(data)
  115. if err != nil {
  116. return nil, err
  117. }
  118. var returnData []map[string]string
  119. err = json.Unmarshal(dataJson, &returnData)
  120. return returnData, nil
  121. }
  122. /**
  123. * 对map进行字典序排序
  124. * @param data map[string]string
  125. * @2020/12/28
  126. * @linsen
  127. */
  128. func HttpBuildQuery(data map[string]string) string {
  129. keySlice := []string{}
  130. for k, _ := range data {
  131. keySlice = append(keySlice, k)
  132. }
  133. sort.Strings(keySlice) //排序字符串
  134. var query string
  135. for _, value := range keySlice {
  136. if value != "" && data[value] != "" {
  137. query += StringJoin(value, "=", data[value], "&")
  138. }
  139. }
  140. return strings.Trim(query, "&")
  141. }
  142. /**
  143. * 对map字典按照顺序进行拼接并且把每个value进行类urlencode
  144. * @param data map[string]string
  145. * @2021/10/14
  146. * @wyy
  147. */
  148. func HttpBuildStringQuery(data map[string]string) string {
  149. keySlice := []string{}
  150. for k, _ := range data {
  151. keySlice = append(keySlice, k)
  152. }
  153. // sort.Strings(keySlice) //排序字符串
  154. var query string
  155. for _, value := range keySlice {
  156. if value != "" && data[value] != "" {
  157. query += StringJoin(value, "=", url.QueryEscape(data[value]), "&")
  158. }
  159. }
  160. return strings.Trim(query, "&")
  161. }
  162. /**
  163. * 对map切片 按值进行顺序
  164. * @param order desc/asc val_type float64/string MapDate []map[string]interface{}
  165. * @2022/10/27
  166. * @lo
  167. */
  168. func MapSort(order, val, val_type string, MapDate []map[string]interface{}) ([]map[string]interface{}, error) {
  169. type KVPair struct {
  170. Key int
  171. Val_float64 float64
  172. val_string string
  173. }
  174. res := []map[string]interface{}{}
  175. if order != "desc" && order != "asc" {
  176. return res, errors.New("order parameter err")
  177. }
  178. if val_type != "float64" && val_type != "string" {
  179. return res, errors.New("val_type parameter err")
  180. }
  181. if val == "" {
  182. return res, errors.New("val parameter err")
  183. }
  184. if len(MapDate) < 1 {
  185. return res, errors.New("MapDate parameter err")
  186. }
  187. tmpList := []KVPair{}
  188. if val_type == "string" {
  189. for k, item := range MapDate {
  190. tmpList = append(tmpList, KVPair{Key: k, val_string: ToStr(item[val])})
  191. }
  192. if order == "desc" {
  193. sort.Slice(tmpList, func(i, j int) bool {
  194. return tmpList[i].val_string > tmpList[j].val_string // 降序
  195. })
  196. } else {
  197. sort.Slice(tmpList, func(i, j int) bool {
  198. return tmpList[i].val_string < tmpList[j].val_string // 升序
  199. })
  200. }
  201. } else {
  202. for k, item := range MapDate {
  203. float64_val, _ := ToFloat64(item[val])
  204. tmpList = append(tmpList, KVPair{Key: k, Val_float64: float64_val})
  205. }
  206. if order == "desc" {
  207. sort.Slice(tmpList, func(i, j int) bool {
  208. return tmpList[i].Val_float64 > tmpList[j].Val_float64 // 降序
  209. })
  210. } else {
  211. sort.Slice(tmpList, func(i, j int) bool {
  212. return tmpList[i].Val_float64 < tmpList[j].Val_float64 // 升序
  213. })
  214. }
  215. }
  216. for _, tmp := range tmpList {
  217. res = append(res, MapDate[tmp.Key])
  218. }
  219. return res, nil
  220. }
  221. func MapPage(pageNum, pageSize int, MapDate []map[string]interface{}) []map[string]interface{} {
  222. stat := (pageNum - 1) * pageSize
  223. end := stat + pageSize
  224. count := len(MapDate)
  225. if stat >= count {
  226. return []map[string]interface{}{}
  227. }
  228. if end >= count {
  229. end = count
  230. }
  231. if stat < 0 || end < 0 {
  232. return []map[string]interface{}{}
  233. }
  234. return MapDate[stat:end]
  235. }
  236. /**
  237. * MAP索引转大驼峰
  238. * gz
  239. */
  240. func SetMapFirstToUpper(info map[string]interface{}) map[string]interface{} {
  241. var ret map[string]interface{}
  242. if len(info) > 0 {
  243. ret = make(map[string]interface{})
  244. for k, v := range info {
  245. ret[StrFirstToUpper(k)] = v
  246. }
  247. }
  248. return ret
  249. }