gutil_map.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gutil
  7. import (
  8. "github.com/gogf/gf/internal/utils"
  9. "reflect"
  10. )
  11. // MapCopy does a shallow copy from map `data` to `copy` for most commonly used map type
  12. // map[string]interface{}.
  13. func MapCopy(data map[string]interface{}) (copy map[string]interface{}) {
  14. copy = make(map[string]interface{}, len(data))
  15. for k, v := range data {
  16. copy[k] = v
  17. }
  18. return
  19. }
  20. // MapContains checks whether map `data` contains `key`.
  21. func MapContains(data map[string]interface{}, key string) (ok bool) {
  22. if len(data) == 0 {
  23. return
  24. }
  25. _, ok = data[key]
  26. return
  27. }
  28. // MapDelete deletes all `keys` from map `data`.
  29. func MapDelete(data map[string]interface{}, keys ...string) {
  30. if len(data) == 0 {
  31. return
  32. }
  33. for _, key := range keys {
  34. delete(data, key)
  35. }
  36. }
  37. // MapMerge merges all map from `src` to map `dst`.
  38. func MapMerge(dst map[string]interface{}, src ...map[string]interface{}) {
  39. if dst == nil {
  40. return
  41. }
  42. for _, m := range src {
  43. for k, v := range m {
  44. dst[k] = v
  45. }
  46. }
  47. }
  48. // MapMergeCopy creates and returns a new map which merges all map from `src`.
  49. func MapMergeCopy(src ...map[string]interface{}) (copy map[string]interface{}) {
  50. copy = make(map[string]interface{})
  51. for _, m := range src {
  52. for k, v := range m {
  53. copy[k] = v
  54. }
  55. }
  56. return
  57. }
  58. // MapPossibleItemByKey tries to find the possible key-value pair for given key ignoring cases and symbols.
  59. //
  60. // Note that this function might be of low performance.
  61. func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{}) {
  62. if len(data) == 0 {
  63. return
  64. }
  65. if v, ok := data[key]; ok {
  66. return key, v
  67. }
  68. // Loop checking.
  69. for k, v := range data {
  70. if utils.EqualFoldWithoutChars(k, key) {
  71. return k, v
  72. }
  73. }
  74. return "", nil
  75. }
  76. // MapContainsPossibleKey checks if the given `key` is contained in given map `data`.
  77. // It checks the key ignoring cases and symbols.
  78. //
  79. // Note that this function might be of low performance.
  80. func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
  81. if k, _ := MapPossibleItemByKey(data, key); k != "" {
  82. return true
  83. }
  84. return false
  85. }
  86. // MapOmitEmpty deletes all empty values from given map.
  87. func MapOmitEmpty(data map[string]interface{}) {
  88. if len(data) == 0 {
  89. return
  90. }
  91. for k, v := range data {
  92. if IsEmpty(v) {
  93. delete(data, k)
  94. }
  95. }
  96. }
  97. // MapToSlice converts map to slice of which all keys and values are its items.
  98. // Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
  99. func MapToSlice(data interface{}) []interface{} {
  100. var (
  101. reflectValue = reflect.ValueOf(data)
  102. reflectKind = reflectValue.Kind()
  103. )
  104. for reflectKind == reflect.Ptr {
  105. reflectValue = reflectValue.Elem()
  106. reflectKind = reflectValue.Kind()
  107. }
  108. switch reflectKind {
  109. case reflect.Map:
  110. array := make([]interface{}, 0)
  111. for _, key := range reflectValue.MapKeys() {
  112. array = append(array, key.Interface())
  113. array = append(array, reflectValue.MapIndex(key).Interface())
  114. }
  115. return array
  116. }
  117. return nil
  118. }