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