utils_map.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 utils
  7. // MapPossibleItemByKey tries to find the possible key-value pair for given key ignoring cases and symbols.
  8. //
  9. // Note that this function might be of low performance.
  10. func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{}) {
  11. if len(data) == 0 {
  12. return
  13. }
  14. if v, ok := data[key]; ok {
  15. return key, v
  16. }
  17. // Loop checking.
  18. for k, v := range data {
  19. if EqualFoldWithoutChars(k, key) {
  20. return k, v
  21. }
  22. }
  23. return "", nil
  24. }
  25. // MapContainsPossibleKey checks if the given `key` is contained in given map `data`.
  26. // It checks the key ignoring cases and symbols.
  27. //
  28. // Note that this function might be of low performance.
  29. func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
  30. if k, _ := MapPossibleItemByKey(data, key); k != "" {
  31. return true
  32. }
  33. return false
  34. }