mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// 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 provides utility functions.
|
|
package gutil
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// replaceCharReg is the regular expression object for replacing chars in map keys.
|
|
replaceCharReg, _ = regexp.Compile(`[\-\.\_\s]+`)
|
|
)
|
|
|
|
// MapCopy does memory from map <data> to <copy>.
|
|
func MapCopy(data map[string]interface{}) (copy map[string]interface{}) {
|
|
copy = make(map[string]interface{}, len(data))
|
|
for k, v := range data {
|
|
copy[k] = v
|
|
}
|
|
return
|
|
}
|
|
|
|
// MapValueForPossibleKey tries to find the possible value for given key with or without
|
|
// cases or chars '-'/'_'/'.'/' '.
|
|
//
|
|
// Note that this function might be of low performance.
|
|
func MapPossibleValueForKey(data map[string]interface{}, key string) interface{} {
|
|
if v, ok := data[key]; ok {
|
|
return v
|
|
}
|
|
replacedKey := replaceCharReg.ReplaceAllString(key, "")
|
|
if v, ok := data[replacedKey]; ok {
|
|
return v
|
|
}
|
|
// Loop for check.
|
|
for k, v := range data {
|
|
if strings.EqualFold(replaceCharReg.ReplaceAllString(k, ""), replacedKey) {
|
|
return v
|
|
}
|
|
}
|
|
return nil
|
|
}
|