From 760ce22cd4edae685dc168607bb6071d6abdc762 Mon Sep 17 00:00:00 2001 From: yangyi Date: Fri, 3 Jan 2025 12:56:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20repository.ForEach=20=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=AE=9E=E4=BE=8B=E5=8C=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- repository.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/repository.go b/repository.go index 64637a1..094cb3d 100644 --- a/repository.go +++ b/repository.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "reflect" "github.com/doug-martin/goqu/v9" ) @@ -85,8 +86,14 @@ func (repo *Repository[ID, E]) ForEach(ctx context.Context, stmt *goqu.SelectDat } defer rows.Close() + var v E + vt := reflect.TypeOf(v) + if vt.Kind() == reflect.Ptr { + vt = vt.Elem() + } + for rows.Next() { - var row E + row := reflect.New(vt).Interface().(E) if err := rows.StructScan(row); err != nil { return fmt.Errorf("scan row, %w", err)