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

178 lines
3.3 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]interface
  94. func InterfaceToMapInterfaceArr(data interface{})([]map[string]interface{},error){
  95. dataJson,err := json.Marshal(data)
  96. if err != nil {
  97. return nil,err
  98. }
  99. var returnData []map[string]interface{}
  100. err = json.Unmarshal(dataJson,&returnData)
  101. return returnData,nil
  102. }
  103. /**
  104. * 对map进行字典序排序
  105. * @param data map[string]string
  106. * @2020/12/28
  107. * @linsen
  108. */
  109. func HttpBuildQuery(data map[string]string) string {
  110. keySlice := []string{}
  111. for k, _ := range data {
  112. keySlice = append(keySlice, k)
  113. }
  114. sort.Strings(keySlice) //排序字符串
  115. var query string
  116. for _, value := range keySlice {
  117. if value != "" && data[value] != "" {
  118. query += StringJoin(value, "=", data[value], "&")
  119. }
  120. }
  121. return strings.Trim(query, "&")
  122. }
  123. /**
  124. * 对map字典按照顺序进行拼接并且把每个value进行类urlencode
  125. * @param data map[string]string
  126. * @2021/10/14
  127. * @wyy
  128. */
  129. func HttpBuildStringQuery(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, "=", url.QueryEscape(data[value]), "&")
  139. }
  140. }
  141. return strings.Trim(query, "&")
  142. }