-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.go
48 lines (41 loc) · 885 Bytes
/
common.go
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
44
45
46
47
48
// Package common contains types used by all update code.
package common
import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/jamespfennell/transiter/internal/gen/api"
"github.com/jamespfennell/transiter/internal/gen/db"
"golang.org/x/exp/slog"
)
type UpdateContext struct {
Querier db.Querier
Logger *slog.Logger
SystemPk int64
FeedPk int64
FeedConfig *api.FeedConfig
}
func HashBytes(b []byte) string {
return fmt.Sprintf("%x", md5.Sum(b))
}
func HashValue(a any) (string, error) {
b, err := json.Marshal(a)
if err != nil {
return "", err
}
return HashBytes(b), nil
}
func MapValues[K comparable, V any](in map[K]V) []V {
out := make([]V, 0, len(in))
for _, v := range in {
out = append(out, v)
}
return out
}
func MapKeys[T comparable, V any](in map[T]V) []T {
var out []T
for t := range in {
out = append(out, t)
}
return out
}