-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
42 lines (35 loc) · 901 Bytes
/
utils.go
File metadata and controls
42 lines (35 loc) · 901 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
package anki
import (
"time"
"github.com/google/uuid"
)
// timeZero returns a zero time.
func timeZero() time.Time {
return time.Unix(0, 0)
}
// timeUnix returns the Unix timestamp for a given time.
func timeUnix(t time.Time) int64 {
return max(t.Unix(), 0)
}
// randomGUID generates a random GUID.
func randomGUID() (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
}
return u.String(), nil
}
// scanValue scans a single value from a database row.
func scanValue[T any](_ sqlQueryer, row sqlRow) (T, error) {
var val T
return val, row.Scan(&val)
}
// sliceMap applies a function to each element of a slice and returns a new slice
// with the results. It is a generic map function for slices.
func sliceMap[Slice ~[]E, E, T any](s Slice, f func(E) T) []T {
vals := make([]T, 0, len(s))
for _, e := range s {
vals = append(vals, f(e))
}
return vals
}