常用类型及数据操作方法
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.

205 lines
3.9 KiB

3 years ago
3 years ago
3 years ago
  1. package helper
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "sort"
  7. "strings"
  8. )
  9. func MergeStringMap(x, y map[string]string) map[string]string {
  10. n := make(map[string]string)
  11. for i, v := range x {
  12. for j, w := range y {
  13. if i == j {
  14. n[i] = w
  15. } else {
  16. if _, ok := n[i]; !ok {
  17. n[i] = v
  18. }
  19. if _, ok := n[j]; !ok {
  20. n[j] = w
  21. }
  22. }
  23. }
  24. }
  25. return n
  26. }
  27. /**
  28. * 转换为map string数组
  29. * 2021/1/5
  30. * gz
  31. */
  32. func ToMapStringArray(x interface{}) ([]map[string]string, error) {
  33. list, ok := x.([]interface{})
  34. if !ok {
  35. return []map[string]string{}, errors.New("type error")
  36. }
  37. if len(list) < 1 {
  38. return []map[string]string{}, nil
  39. }
  40. var ret []map[string]string = make([]map[string]string, len(list))
  41. var tmp map[string]interface{}
  42. var map_tmp map[string]string
  43. var err error
  44. for key, item := range list {
  45. if tmp, ok = item.(map[string]interface{}); ok {
  46. map_tmp = make(map[string]string)
  47. for k, v := range tmp {
  48. map_tmp[k] = ToStr(v)
  49. }
  50. ret[key] = map_tmp
  51. } else {
  52. err = errors.New("data type error")
  53. break
  54. }
  55. }
  56. return ret, err
  57. }
  58. /**
  59. * 将map[string]string数组转成map[string]interface数组
  60. */
  61. func MapStringToInterfaceArr(src []map[string]string) []map[string]interface{} {
  62. var dest []map[string]interface{}
  63. for i := range src {
  64. m := src[i]
  65. n := make(map[string]interface{})
  66. for k, v := range m {
  67. n[k] = interface{}(v)
  68. }
  69. dest = append(dest, n)
  70. }
  71. return dest
  72. }
  73. /**
  74. * 将map[string]string转成map[string]interface
  75. */
  76. func MapStringToInterface(src map[string]string) map[string]interface{} {
  77. target := make(map[string]interface{})
  78. for k, v := range src {
  79. target[k] = interface{}(v)
  80. }
  81. return target
  82. }
  83. //将interface转成map[string]interface
  84. func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) {
  85. dataJson, err := json.Marshal(data)
  86. if err != nil {
  87. return nil, err
  88. }
  89. var returnData map[string]interface{}
  90. err = json.Unmarshal(dataJson, &returnData)
  91. return returnData, nil
  92. }
  93. //将interface转成map[string]string
  94. func InterfaceToMapString(data interface{}) (map[string]string, error) {
  95. dataJson, err := json.Marshal(data)
  96. if err != nil {
  97. return nil, err
  98. }
  99. var returnData map[string]string
  100. err = json.Unmarshal(dataJson, &returnData)
  101. return returnData, nil
  102. }
  103. //将interface转成[]map[string]interface
  104. func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, error) {
  105. dataJson, err := json.Marshal(data)
  106. if err != nil {
  107. return nil, err
  108. }
  109. var returnData []map[string]interface{}
  110. err = json.Unmarshal(dataJson, &returnData)
  111. return returnData, nil
  112. }
  113. //将interface转成[]map[string]string
  114. func InterfaceToMapStringArr(data interface{}) ([]map[string]string, error) {
  115. dataJson, err := json.Marshal(data)
  116. if err != nil {
  117. return nil, err
  118. }
  119. var returnData []map[string]string
  120. err = json.Unmarshal(dataJson, &returnData)
  121. return returnData, nil
  122. }
  123. /**
  124. * 对map进行字典序排序
  125. * @param data map[string]string
  126. * @2020/12/28
  127. * @linsen
  128. */
  129. func HttpBuildQuery(data map[string]string) string {
  130. keySlice := []string{}
  131. for k, _ := range data {
  132. keySlice = append(keySlice, k)
  133. }
  134. sort.Strings(keySlice) //排序字符串
  135. var query string
  136. for _, value := range keySlice {
  137. if value != "" && data[value] != "" {
  138. query += StringJoin(value, "=", data[value], "&")
  139. }
  140. }
  141. return strings.Trim(query, "&")
  142. }
  143. /**
  144. * 对map字典按照顺序进行拼接并且把每个value进行类urlencode
  145. * @param data map[string]string
  146. * @2021/10/14
  147. * @wyy
  148. */
  149. func HttpBuildStringQuery(data map[string]string) string {
  150. keySlice := []string{}
  151. for k, _ := range data {
  152. keySlice = append(keySlice, k)
  153. }
  154. // sort.Strings(keySlice) //排序字符串
  155. var query string
  156. for _, value := range keySlice {
  157. if value != "" && data[value] != "" {
  158. query += StringJoin(value, "=", url.QueryEscape(data[value]), "&")
  159. }
  160. }
  161. return strings.Trim(query, "&")
  162. }