Skip to content

Commit

Permalink
fix: repository.ForEach 实体对象实例化方式
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyi committed Jan 3, 2025
1 parent 3f245f8 commit 760ce22
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"reflect"

"github.com/doug-martin/goqu/v9"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 760ce22

Please sign in to comment.