From 99b1fac6dff3568355cf6bb3b5e077ab1950ee3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20C=2E=20For=C3=A9s?= Date: Mon, 21 Apr 2025 01:33:29 +0200 Subject: [PATCH 1/3] feat: better collect rows generic parameters --- rows.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rows.go b/rows.go index f6f26f479..be4a3e77d 100644 --- a/rows.go +++ b/rows.go @@ -447,8 +447,8 @@ func AppendRows[T any, S ~[]T](slice S, rows Rows, fn RowToFunc[T]) (S, error) { // CollectRows iterates through rows, calling fn for each row, and collecting the results into a slice of T. // // This function closes the rows automatically on return. -func CollectRows[T any](rows Rows, fn RowToFunc[T]) ([]T, error) { - return AppendRows([]T{}, rows, fn) +func CollectRows[T any, S ~[]T](rows Rows, fn RowToFunc[T]) (S, error) { + return AppendRows(S{}, rows, fn) } // CollectOneRow calls fn for the first row in rows and returns the result. If no rows are found returns an error where errors.Is(ErrNoRows) is true. From aa8483f04586cdb9aa9feb32c65579af09986b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20C=2E=20For=C3=A9s?= Date: Mon, 21 Apr 2025 01:50:08 +0200 Subject: [PATCH 2/3] fix: revert original signature + add CollectRowsInto --- rows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rows.go b/rows.go index be4a3e77d..dc2e4e5a5 100644 --- a/rows.go +++ b/rows.go @@ -447,7 +447,7 @@ func AppendRows[T any, S ~[]T](slice S, rows Rows, fn RowToFunc[T]) (S, error) { // CollectRows iterates through rows, calling fn for each row, and collecting the results into a slice of T. // // This function closes the rows automatically on return. -func CollectRows[T any, S ~[]T](rows Rows, fn RowToFunc[T]) (S, error) { +func CollectRows[S ~[]T, T any](rows Rows, fn RowToFunc[T]) (S, error) { return AppendRows(S{}, rows, fn) } From ddf22502bf5d3f80bb67d6cd7c5bfe8ba029152c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20C=2E=20For=C3=A9s?= Date: Mon, 21 Apr 2025 01:53:35 +0200 Subject: [PATCH 3/3] fix: upload change --- rows.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rows.go b/rows.go index dc2e4e5a5..0d1d3e9c6 100644 --- a/rows.go +++ b/rows.go @@ -447,8 +447,17 @@ func AppendRows[T any, S ~[]T](slice S, rows Rows, fn RowToFunc[T]) (S, error) { // CollectRows iterates through rows, calling fn for each row, and collecting the results into a slice of T. // // This function closes the rows automatically on return. -func CollectRows[S ~[]T, T any](rows Rows, fn RowToFunc[T]) (S, error) { - return AppendRows(S{}, rows, fn) +func CollectRows[T any](rows Rows, fn RowToFunc[T]) ([]T, error) { + return AppendRows([]T{}, rows, fn) +} + +// CollectRowsInto is the same as [CollectRows] but allows +// defining a custom slice type. Useful when you have custom +// types to denote slices. +// +// This function closes the rows automatically on return. +func CollectRowsInto[S ~[]T, T any](rows Rows, fn RowToFunc[T]) (S, error) { + return CollectRows(rows, fn) } // CollectOneRow calls fn for the first row in rows and returns the result. If no rows are found returns an error where errors.Is(ErrNoRows) is true.