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

204 lines
3.8 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. }