2019-10-30 23:26:57 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gutil
|
|
|
|
|
2019-11-07 11:32:25 +08:00
|
|
|
import (
|
2020-04-11 09:09:25 +08:00
|
|
|
"github.com/gogf/gf/internal/utils"
|
2019-11-07 11:32:25 +08:00
|
|
|
)
|
|
|
|
|
2020-04-09 15:34:23 +08:00
|
|
|
// MapCopy does a shallow copy from map <data> to <copy> for most commonly used map type
|
2020-03-28 00:37:23 +08:00
|
|
|
// map[string]interface{}.
|
2020-04-09 15:34:23 +08:00
|
|
|
func MapCopy(data map[string]interface{}) (copy map[string]interface{}) {
|
2019-10-30 23:26:57 +08:00
|
|
|
copy = make(map[string]interface{}, len(data))
|
|
|
|
for k, v := range data {
|
|
|
|
copy[k] = v
|
|
|
|
}
|
|
|
|
return
|
2020-04-09 15:34:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MapContains checks whether map <data> contains <key>.
|
|
|
|
func MapContains(data map[string]interface{}, key string) (ok bool) {
|
|
|
|
_, ok = data[key]
|
|
|
|
return
|
2019-10-30 23:26:57 +08:00
|
|
|
}
|
2019-11-07 11:32:25 +08:00
|
|
|
|
2020-06-17 11:37:45 +08:00
|
|
|
// MapDelete deletes all <keys> from map <data>.
|
|
|
|
func MapDelete(data map[string]interface{}, keys ...string) {
|
2020-04-15 12:56:41 +08:00
|
|
|
if data == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-17 11:37:45 +08:00
|
|
|
for _, key := range keys {
|
|
|
|
delete(data, key)
|
2020-04-15 12:56:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:11:12 +08:00
|
|
|
// MapMerge merges all map from <src> to map <dst>.
|
|
|
|
func MapMerge(dst map[string]interface{}, src ...map[string]interface{}) {
|
|
|
|
if dst == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, m := range src {
|
|
|
|
for k, v := range m {
|
|
|
|
dst[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapMergeCopy creates and returns a new map which merges all map from <src>.
|
|
|
|
func MapMergeCopy(src ...map[string]interface{}) (copy map[string]interface{}) {
|
|
|
|
copy = make(map[string]interface{})
|
|
|
|
for _, m := range src {
|
|
|
|
for k, v := range m {
|
|
|
|
copy[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:04:52 +08:00
|
|
|
// MapPossibleItemByKey tries to find the possible key-value pair for given key with or without
|
2019-11-07 11:32:25 +08:00
|
|
|
// cases or chars '-'/'_'/'.'/' '.
|
|
|
|
//
|
|
|
|
// Note that this function might be of low performance.
|
2020-04-08 21:26:14 +08:00
|
|
|
func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{}) {
|
2019-11-07 11:32:25 +08:00
|
|
|
if v, ok := data[key]; ok {
|
2019-12-04 16:04:52 +08:00
|
|
|
return key, v
|
2019-11-07 11:32:25 +08:00
|
|
|
}
|
2020-04-11 09:09:25 +08:00
|
|
|
// Loop checking.
|
2019-11-07 11:32:25 +08:00
|
|
|
for k, v := range data {
|
2020-04-11 09:09:25 +08:00
|
|
|
if utils.EqualFoldWithoutChars(k, key) {
|
2019-12-04 16:04:52 +08:00
|
|
|
return k, v
|
2019-11-07 11:32:25 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:04:52 +08:00
|
|
|
return "", nil
|
2019-11-07 11:32:25 +08:00
|
|
|
}
|
2020-04-08 21:26:14 +08:00
|
|
|
|
|
|
|
// MapContainsPossibleKey checks if the given <key> is contained in given map <data>.
|
|
|
|
// It checks the key with or without cases or chars '-'/'_'/'.'/' '.
|
2020-06-17 11:37:45 +08:00
|
|
|
//
|
|
|
|
// Note that this function might be of low performance.
|
2020-04-08 21:26:14 +08:00
|
|
|
func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
|
|
|
|
if k, _ := MapPossibleItemByKey(data, key); k != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-08-08 09:53:32 +08:00
|
|
|
|
|
|
|
// MapOmitEmpty deletes all empty values from guven map.
|
|
|
|
func MapOmitEmpty(data map[string]interface{}) {
|
|
|
|
for k, v := range data {
|
|
|
|
if IsEmpty(v) {
|
|
|
|
delete(data, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|