gitlab.com/evatix-go/core@v1.3.55/coredata/coredynamic/MapKeysStringSlice.go (about)

     1  package coredynamic
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"gitlab.com/evatix-go/core/errcore"
     7  )
     8  
     9  // MapKeysStringSlice
    10  //
    11  //  expectation : map[key:string]...value don't care.
    12  func MapKeysStringSlice(reflectVal reflect.Value) ([]string, error) {
    13  	if reflectVal.Kind() == reflect.Ptr {
    14  		return MapKeysStringSlice(
    15  			reflect.Indirect(reflect.ValueOf(reflectVal)))
    16  	}
    17  
    18  	if reflectVal.Kind() != reflect.Map {
    19  		return []string{},
    20  			errcore.TypeMismatchType.Error("Reflection is not Map", reflectVal)
    21  	}
    22  
    23  	mapKeys := reflectVal.MapKeys()
    24  	length := len(mapKeys)
    25  	keys := make([]string, length)
    26  
    27  	for i, key := range reflectVal.MapKeys() {
    28  		keyAny := key.Interface()
    29  		keyAsString, isString := keyAny.(string)
    30  
    31  		if !isString {
    32  			return keys, errcore.TypeMismatchType.Error("Not string type", keyAny)
    33  		}
    34  
    35  		keys[i] = keyAsString
    36  	}
    37  
    38  	return keys, nil
    39  }