github.com/gogf/gf@v1.16.9/util/gutil/gutil_map.go (about)

     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  
     7  package gutil
     8  
     9  import (
    10  	"github.com/gogf/gf/internal/utils"
    11  	"reflect"
    12  )
    13  
    14  // MapCopy does a shallow copy from map `data` to `copy` for most commonly used map type
    15  // map[string]interface{}.
    16  func MapCopy(data map[string]interface{}) (copy map[string]interface{}) {
    17  	copy = make(map[string]interface{}, len(data))
    18  	for k, v := range data {
    19  		copy[k] = v
    20  	}
    21  	return
    22  }
    23  
    24  // MapContains checks whether map `data` contains `key`.
    25  func MapContains(data map[string]interface{}, key string) (ok bool) {
    26  	if len(data) == 0 {
    27  		return
    28  	}
    29  	_, ok = data[key]
    30  	return
    31  }
    32  
    33  // MapDelete deletes all `keys` from map `data`.
    34  func MapDelete(data map[string]interface{}, keys ...string) {
    35  	if len(data) == 0 {
    36  		return
    37  	}
    38  	for _, key := range keys {
    39  		delete(data, key)
    40  	}
    41  }
    42  
    43  // MapMerge merges all map from `src` to map `dst`.
    44  func MapMerge(dst map[string]interface{}, src ...map[string]interface{}) {
    45  	if dst == nil {
    46  		return
    47  	}
    48  	for _, m := range src {
    49  		for k, v := range m {
    50  			dst[k] = v
    51  		}
    52  	}
    53  }
    54  
    55  // MapMergeCopy creates and returns a new map which merges all map from `src`.
    56  func MapMergeCopy(src ...map[string]interface{}) (copy map[string]interface{}) {
    57  	copy = make(map[string]interface{})
    58  	for _, m := range src {
    59  		for k, v := range m {
    60  			copy[k] = v
    61  		}
    62  	}
    63  	return
    64  }
    65  
    66  // MapPossibleItemByKey tries to find the possible key-value pair for given key ignoring cases and symbols.
    67  //
    68  // Note that this function might be of low performance.
    69  func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{}) {
    70  	if len(data) == 0 {
    71  		return
    72  	}
    73  	if v, ok := data[key]; ok {
    74  		return key, v
    75  	}
    76  	// Loop checking.
    77  	for k, v := range data {
    78  		if utils.EqualFoldWithoutChars(k, key) {
    79  			return k, v
    80  		}
    81  	}
    82  	return "", nil
    83  }
    84  
    85  // MapContainsPossibleKey checks if the given `key` is contained in given map `data`.
    86  // It checks the key ignoring cases and symbols.
    87  //
    88  // Note that this function might be of low performance.
    89  func MapContainsPossibleKey(data map[string]interface{}, key string) bool {
    90  	if k, _ := MapPossibleItemByKey(data, key); k != "" {
    91  		return true
    92  	}
    93  	return false
    94  }
    95  
    96  // MapOmitEmpty deletes all empty values from given map.
    97  func MapOmitEmpty(data map[string]interface{}) {
    98  	if len(data) == 0 {
    99  		return
   100  	}
   101  	for k, v := range data {
   102  		if IsEmpty(v) {
   103  			delete(data, k)
   104  		}
   105  	}
   106  }
   107  
   108  // MapToSlice converts map to slice of which all keys and values are its items.
   109  // Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
   110  func MapToSlice(data interface{}) []interface{} {
   111  	var (
   112  		reflectValue = reflect.ValueOf(data)
   113  		reflectKind  = reflectValue.Kind()
   114  	)
   115  	for reflectKind == reflect.Ptr {
   116  		reflectValue = reflectValue.Elem()
   117  		reflectKind = reflectValue.Kind()
   118  	}
   119  	switch reflectKind {
   120  	case reflect.Map:
   121  		array := make([]interface{}, 0)
   122  		for _, key := range reflectValue.MapKeys() {
   123  			array = append(array, key.Interface())
   124  			array = append(array, reflectValue.MapIndex(key).Interface())
   125  		}
   126  		return array
   127  	}
   128  	return nil
   129  }