chore: move List util out of db2sdk to avoid circular imports (#21733)

This commit is contained in:
Steven Masley
2026-01-28 13:07:53 -06:00
committed by GitHub
parent 921fad098b
commit dfbd541cee
2 changed files with 23 additions and 13 deletions
+19
View File
@@ -4,6 +4,25 @@ import (
"golang.org/x/exp/constraints"
)
// List is a helper function to reduce boilerplate when converting slices of
// database types to slices of codersdk types.
// Only works if the function takes a single argument.
func List[F any, T any](list []F, convert func(F) T) []T {
return ListLazy(convert)(list)
}
// ListLazy returns the converter function for a list, but does not eval
// the input. Helpful for combining the Map and the List functions.
func ListLazy[F any, T any](convert func(F) T) func(list []F) []T {
return func(list []F) []T {
into := make([]T, 0, len(list))
for _, item := range list {
into = append(into, convert(item))
}
return into
}
}
// ToStrings works for any type where the base type is a string.
func ToStrings[T ~string](a []T) []string {
tmp := make([]string, 0, len(a))