-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.go
More file actions
43 lines (33 loc) · 841 Bytes
/
map.go
File metadata and controls
43 lines (33 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package rowmap
import (
"database/sql"
"github.com/Southclaws/fault"
"github.com/Southclaws/fault/floc"
)
func mapRows[E any](mapper MapperFunc[E], rows *sql.Rows, err error) ([]E, error) {
if err != nil {
return nil, fault.Wrap(err, With(mapper), floc.WithDepth(2))
}
defer rows.Close()
var entities []E
for rows.Next() {
e, err := mapper(rows)
if err != nil {
return nil, fault.Wrap(err, With(mapper), floc.WithDepth(2))
}
entities = append(entities, e)
}
return entities, nil
}
func mapSingleRow[E any](mapper MapperFunc[E], rows *sql.Rows, err error) (E, error) {
results, err := mapRows(mapper, rows, err)
if err != nil {
var empty E
return empty, err
}
if len(results) == 0 {
var empty E
return empty, fault.Wrap(sql.ErrNoRows, With(mapper), floc.WithDepth(2))
}
return results[0], nil
}