3
0
corteza/pkg/slice/keys.go
Vivek Patel 3edef9e373 Improves record-value manipulation
This will fix WF issue for accessing record's value via selectors & gval expressions
2021-06-16 12:33:05 +05:30

27 lines
447 B
Go

package slice
import (
"reflect"
"sort"
"github.com/spf13/cast"
)
// Keys returns sorted map keys
//
// If input is not a map it will return an empty slice
func Keys(m interface{}) (kk []string) {
v := reflect.ValueOf(m)
if v.Kind() == reflect.Map {
kk = make([]string, 0, v.Len())
for _, kval := range v.MapKeys() {
if k := cast.ToString(kval.Interface()); k != "" {
kk = append(kk, k)
}
}
}
sort.Strings(kk)
return
}