gutil_map.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "reflect"
  9. "github.com/gogf/gf/v2/internal/utils"
  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. return utils.MapPossibleItemByKey(data, key)
  63. }
  64. // MapContainsPossibleKey checks if the given `key` is contained in given map `data`.
  65. // It checks the key ignoring cases and symbols.
  66. //
  67. // Note that this function might be of low performance.
  68. func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
  69. return utils.MapContainsPossibleKey(data, key)
  70. }
  71. // MapOmitEmpty deletes all empty values from given map.
  72. func MapOmitEmpty(data map[string]interface{}) {
  73. if len(data) == 0 {
  74. return
  75. }
  76. for k, v := range data {
  77. if IsEmpty(v) {
  78. delete(data, k)
  79. }
  80. }
  81. }
  82. // MapToSlice converts map to slice of which all keys and values are its items.
  83. // Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
  84. func MapToSlice(data interface{}) []interface{} {
  85. var (
  86. reflectValue = reflect.ValueOf(data)
  87. reflectKind = reflectValue.Kind()
  88. )
  89. for reflectKind == reflect.Ptr {
  90. reflectValue = reflectValue.Elem()
  91. reflectKind = reflectValue.Kind()
  92. }
  93. switch reflectKind {
  94. case reflect.Map:
  95. array := make([]interface{}, 0)
  96. for _, key := range reflectValue.MapKeys() {
  97. array = append(array, key.Interface())
  98. array = append(array, reflectValue.MapIndex(key).Interface())
  99. }
  100. return array
  101. }
  102. return nil
  103. }