gf/util/gutil/gutil_map.go

50 lines
1.4 KiB
Go
Raw Normal View History

// 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
import (
"regexp"
"strings"
)
var (
// replaceCharReg is the regular expression object for replacing chars in map keys.
replaceCharReg, _ = regexp.Compile(`[\-\.\_\s]+`)
)
2020-03-28 00:37:23 +08:00
// CopyMap does a shallow copy from map <data> to <copy> for most commonly used map type
// map[string]interface{}.
func CopyMap(data map[string]interface{}) (copy map[string]interface{}) {
copy = make(map[string]interface{}, len(data))
for k, v := range data {
copy[k] = v
}
return
}
// MapPossibleItemByKey tries to find the possible key-value pair for given key with or without
// cases or chars '-'/'_'/'.'/' '.
//
// Note that this function might be of low performance.
func MapPossibleItemByKey(data map[string]interface{}, key string) (string, interface{}) {
if v, ok := data[key]; ok {
return key, v
}
replacedKey := replaceCharReg.ReplaceAllString(key, "")
if v, ok := data[replacedKey]; ok {
return replacedKey, v
}
// Loop for check.
for k, v := range data {
2020-03-28 21:32:29 +08:00
// Remove all special chars and compare with case insensitive.
if strings.EqualFold(replaceCharReg.ReplaceAllString(k, ""), replacedKey) {
return k, v
}
}
return "", nil
}