2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-10-30 23:26:57 +08:00
|
|
|
//
|
|
|
|
// 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-12-16 00:50:42 +08:00
|
|
|
"reflect"
|
2021-11-13 23:30:31 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
2019-11-07 11:32:25 +08:00
|
|
|
)
|
|
|
|
|
2021-03-23 17:53:20 +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
|
|
|
}
|
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// MapContains checks whether map `data` contains `key`.
|
2020-04-09 15:34:23 +08:00
|
|
|
func MapContains(data map[string]interface{}, key string) (ok bool) {
|
2020-11-08 15:44:04 +08:00
|
|
|
if len(data) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-04-09 15:34:23 +08:00
|
|
|
_, ok = data[key]
|
|
|
|
return
|
2019-10-30 23:26:57 +08:00
|
|
|
}
|
2019-11-07 11:32:25 +08:00
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// MapDelete deletes all `keys` from map `data`.
|
2020-06-17 11:37:45 +08:00
|
|
|
func MapDelete(data map[string]interface{}, keys ...string) {
|
2020-11-08 15:44:04 +08:00
|
|
|
if len(data) == 0 {
|
2020-04-15 12:56:41 +08:00
|
|
|
return
|
|
|
|
}
|
2020-06-17 11:37:45 +08:00
|
|
|
for _, key := range keys {
|
|
|
|
delete(data, key)
|
2020-04-15 12:56:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// MapMerge merges all map from `src` to map `dst`.
|
2020-04-14 21:11:12 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// MapMergeCopy creates and returns a new map which merges all map from `src`.
|
2020-04-14 21:11:12 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:44:04 +08:00
|
|
|
// MapPossibleItemByKey tries to find the possible key-value pair for given key ignoring cases and symbols.
|
2019-11-07 11:32:25 +08:00
|
|
|
//
|
|
|
|
// 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{}) {
|
2021-11-07 00:16:14 +08:00
|
|
|
return utils.MapPossibleItemByKey(data, key)
|
2019-11-07 11:32:25 +08:00
|
|
|
}
|
2020-04-08 21:26:14 +08:00
|
|
|
|
2021-03-23 17:53:20 +08:00
|
|
|
// MapContainsPossibleKey checks if the given `key` is contained in given map `data`.
|
2020-11-08 15:44:04 +08:00
|
|
|
// It checks the key ignoring cases and symbols.
|
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 {
|
2022-02-16 00:47:23 +08:00
|
|
|
return utils.MapContainsPossibleKey(data, key)
|
2020-04-08 21:26:14 +08:00
|
|
|
}
|
2020-08-08 09:53:32 +08:00
|
|
|
|
2020-11-08 15:44:04 +08:00
|
|
|
// MapOmitEmpty deletes all empty values from given map.
|
2020-08-08 09:53:32 +08:00
|
|
|
func MapOmitEmpty(data map[string]interface{}) {
|
2020-11-08 15:44:04 +08:00
|
|
|
if len(data) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-08 09:53:32 +08:00
|
|
|
for k, v := range data {
|
|
|
|
if IsEmpty(v) {
|
|
|
|
delete(data, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 00:50:42 +08:00
|
|
|
|
|
|
|
// MapToSlice converts map to slice of which all keys and values are its items.
|
|
|
|
// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
|
|
|
|
func MapToSlice(data interface{}) []interface{} {
|
|
|
|
var (
|
|
|
|
reflectValue = reflect.ValueOf(data)
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
)
|
|
|
|
for reflectKind == reflect.Ptr {
|
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
}
|
|
|
|
switch reflectKind {
|
|
|
|
case reflect.Map:
|
|
|
|
array := make([]interface{}, 0)
|
|
|
|
for _, key := range reflectValue.MapKeys() {
|
|
|
|
array = append(array, key.Interface())
|
|
|
|
array = append(array, reflectValue.MapIndex(key).Interface())
|
|
|
|
}
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|