Merge "Make SortedStringKeys call SortedKeys"
This commit is contained in:
@@ -65,21 +65,8 @@ func JoinWithPrefixAndSeparator(strs []string, prefix string, sep string) string
|
|||||||
// SortedStringKeys returns the keys of the given map in the ascending order.
|
// SortedStringKeys returns the keys of the given map in the ascending order.
|
||||||
//
|
//
|
||||||
// Deprecated: Use SortedKeys instead.
|
// Deprecated: Use SortedKeys instead.
|
||||||
func SortedStringKeys(m interface{}) []string {
|
func SortedStringKeys[V any](m map[string]V) []string {
|
||||||
v := reflect.ValueOf(m)
|
return SortedKeys(m)
|
||||||
if v.Kind() != reflect.Map {
|
|
||||||
panic(fmt.Sprintf("%#v is not a map", m))
|
|
||||||
}
|
|
||||||
if v.Len() == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
iter := v.MapRange()
|
|
||||||
s := make([]string, 0, v.Len())
|
|
||||||
for iter.Next() {
|
|
||||||
s = append(s, iter.Key().String())
|
|
||||||
}
|
|
||||||
sort.Strings(s)
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ordered interface {
|
type Ordered interface {
|
||||||
|
@@ -671,44 +671,6 @@ func TestSortedKeys(t *testing.T) {
|
|||||||
testSortedKeysHelper(t, "empty", map[string]string{}, nil)
|
testSortedKeysHelper(t, "empty", map[string]string{}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortedStringKeys(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
in interface{}
|
|
||||||
expected []string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "nil",
|
|
||||||
in: map[string]string(nil),
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "empty",
|
|
||||||
in: map[string]string{},
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "simple",
|
|
||||||
in: map[string]string{"a": "foo", "b": "bar"},
|
|
||||||
expected: []string{"a", "b"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "interface values",
|
|
||||||
in: map[string]interface{}{"a": nil, "b": nil},
|
|
||||||
expected: []string{"a", "b"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range testCases {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got := SortedStringKeys(tt.in)
|
|
||||||
if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {
|
|
||||||
t.Errorf("wanted %q, got %q", w, g)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSortedStringValues(t *testing.T) {
|
func TestSortedStringValues(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
Reference in New Issue
Block a user