Skip to content

Instantly share code, notes, and snippets.

@CannibalVox
Last active April 8, 2022 05:14
Show Gist options
  • Save CannibalVox/c7104043b9057d451377a1103d036451 to your computer and use it in GitHub Desktop.
Save CannibalVox/c7104043b9057d451377a1103d036451 to your computer and use it in GitHub Desktop.
// Scannable represents the shared surface area between sql.Row and sql.Rows -
// both types implement this interface, and methods which accept Scannable
// can accept both types.
type Scannable interface {
Err() error
Scan(dest ...any) error
}
type Model interface {
Scan(scannable Scannable, additional ...interface{}) error
}
func ScanRows[M any, P interface {
*M
Model
}](rows *sql.Rows) ([]M, error) {
models := make([]M, 0)
for rows.Next() {
var model M
err := P(&model).Scan(rows)
if err != nil {
return nil, errorutils.TransformPostgresError(err)
}
models = append(models, model)
}
return models, nil
}
/// Calling
return data.ScanRows[models.Timezone](rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment