github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/mapp/map.go (about)

     1  package mapp
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  
     7  	"github.com/bingoohuang/gg/pkg/sortx"
     8  )
     9  
    10  // Keys 返回Map的key切片
    11  func Keys(m interface{}) []string {
    12  	return KeysX(m).([]string)
    13  }
    14  
    15  // KeysSorted 返回Map排序后的key切片
    16  func KeysSorted(m interface{}) []string {
    17  	keys := Keys(m)
    18  	sort.Strings(keys)
    19  	return keys
    20  }
    21  
    22  // KeysX 返回Map的key切片
    23  func KeysX(m interface{}) interface{} {
    24  	v := reflect.ValueOf(m)
    25  	if v.Kind() != reflect.Map {
    26  		return nil
    27  	}
    28  
    29  	keyType := v.Type().Key()
    30  	ks := reflect.MakeSlice(reflect.SliceOf(keyType), v.Len(), v.Len())
    31  
    32  	for i, key := range v.MapKeys() {
    33  		ks.Index(i).Set(key)
    34  	}
    35  
    36  	return ks.Interface()
    37  }
    38  
    39  // KeysSortedX 返回Map排序后的key切片
    40  func KeysSortedX(m interface{}) interface{} {
    41  	mv := reflect.ValueOf(m)
    42  	if mv.Kind() != reflect.Map {
    43  		return nil
    44  	}
    45  
    46  	mapLen := mv.Len()
    47  	ks := reflect.MakeSlice(reflect.SliceOf(mv.Type().Key()), mapLen, mapLen)
    48  	for i, k := range mv.MapKeys() {
    49  		ks.Index(i).Set(k)
    50  	}
    51  
    52  	ksi := ks.Interface()
    53  	sortx.Sort(ksi)
    54  	return ksi
    55  }
    56  
    57  // Values 返回Map的value切片
    58  func Values(m interface{}) []string {
    59  	return MapValuesX(m).([]string)
    60  }
    61  
    62  // MapValuesX 返回Map的value切片
    63  func MapValuesX(m interface{}) interface{} {
    64  	v := reflect.ValueOf(m)
    65  	if v.Kind() != reflect.Map {
    66  		return nil
    67  	}
    68  
    69  	sl := reflect.MakeSlice(reflect.SliceOf(v.Type().Elem()), v.Len(), v.Len())
    70  	for i, key := range v.MapKeys() {
    71  		sl.Index(i).Set(v.MapIndex(key))
    72  	}
    73  
    74  	return sl.Interface()
    75  }
    76  
    77  // GetOr get value from m by key or returns the defaultValue
    78  func GetOr(m, k, defaultValue interface{}) interface{} {
    79  	mv := reflect.ValueOf(m)
    80  	if mv.Kind() != reflect.Map {
    81  		return nil
    82  	}
    83  
    84  	v := mv.MapIndex(reflect.ValueOf(k))
    85  	if v.IsValid() {
    86  		return v.Interface()
    87  	}
    88  
    89  	return defaultValue
    90  }
    91  
    92  // WalkMap iterates the map by iterFunc.
    93  func WalkMap(m interface{}, iterFunc interface{}) {
    94  	mv := reflect.ValueOf(m)
    95  	if mv.Kind() != reflect.Map {
    96  		return
    97  	}
    98  
    99  	mapLen := mv.Len()
   100  	ks := reflect.MakeSlice(reflect.SliceOf(mv.Type().Key()), mapLen, mapLen)
   101  
   102  	for i, k := range mv.MapKeys() {
   103  		ks.Index(i).Set(k)
   104  	}
   105  
   106  	ksi := ks.Interface()
   107  	sortx.Sort(ksi)
   108  
   109  	funcValue := reflect.ValueOf(iterFunc)
   110  
   111  	for j := 0; j < mapLen; j++ {
   112  		k := ks.Index(j)
   113  		v := mv.MapIndex(k)
   114  		funcValue.Call([]reflect.Value{k, v})
   115  	}
   116  }
   117  
   118  // Clone clones a map[string]string.
   119  func Clone(m map[string]string) map[string]string {
   120  	c := make(map[string]string)
   121  	for k, v := range m {
   122  		c[k] = v
   123  	}
   124  	return c
   125  }