Last active
April 8, 2022 05:14
-
-
Save CannibalVox/c7104043b9057d451377a1103d036451 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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