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
+4 -13
View File
@@ -31,23 +31,14 @@ import (
previewtypes "github.com/coder/preview/types"
)
// 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.
// Deprecated: use slice.List
func List[F any, T any](list []F, convert func(F) T) []T {
return ListLazy(convert)(list)
return slice.List[F, T](list, convert)
}
// ListLazy returns the converter function for a list, but does not eval
// the input. Helpful for combining the Map and the List functions.
// Deprecated: use slice.ListLazy
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
}
return slice.ListLazy[F, T](convert)
}
func APIAllowListTarget(entry rbac.AllowListElement) codersdk.APIAllowListTarget {
+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))