github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/sliceutils/sliceutils.go (about)

     1  // Copyright (c) 2023 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package sliceutils
     6  
     7  import (
     8  	"golang.org/x/exp/slices"
     9  )
    10  
    11  // ToAnySlice takes a []T, and converts it into a []any.
    12  // This is a common conversion when a function expects a []any but the calling code has a []T, with
    13  // T not being any.
    14  func ToAnySlice[T any](in []T) []any {
    15  	l := len(in)
    16  	out := make([]any, l)
    17  	for i := 0; i < l; i++ {
    18  		out[i] = any(in[i])
    19  	}
    20  	return out
    21  }
    22  
    23  // SortedStringKeys returns the keys of a string to anything map
    24  // in a sorted slice.
    25  func SortedStringKeys[T any](m map[string]T) []string {
    26  	keys := make([]string, 0, len(m))
    27  	for key := range m {
    28  		keys = append(keys, key)
    29  	}
    30  	slices.Sort(keys)
    31  	return keys
    32  }