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

     1  package coredynamic
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"gitlab.com/evatix-go/core/errcore"
     7  )
     8  
     9  // MapAsKeyValSlice
    10  //
    11  //  expectation : map[key:interface{}]interface{}
    12  func MapAsKeyValSlice(reflectVal reflect.Value) (*KeyValCollection, error) {
    13  	if reflectVal.Kind() == reflect.Ptr {
    14  		return MapAsKeyValSlice(
    15  			reflect.Indirect(reflect.ValueOf(reflectVal)))
    16  	}
    17  
    18  	if reflectVal.Kind() != reflect.Map {
    19  		return EmptyKeyValCollection(),
    20  			errcore.TypeMismatchType.Error("Reflection is not Map", reflectVal)
    21  	}
    22  
    23  	mapKeys := reflectVal.MapKeys()
    24  	keyValCollection := NewKeyValCollection(len(mapKeys))
    25  
    26  	for _, key := range reflectVal.MapKeys() {
    27  		value := reflectVal.MapIndex(key)
    28  
    29  		dynamicKV := &KeyVal{
    30  			Key:   key.Interface(),
    31  			Value: value.Interface(),
    32  		}
    33  
    34  		keyValCollection.AddPtr(dynamicKV)
    35  	}
    36  
    37  	return keyValCollection, nil
    38  }