-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathupdate.go
More file actions
56 lines (43 loc) · 1.19 KB
/
update.go
File metadata and controls
56 lines (43 loc) · 1.19 KB
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
49
50
51
52
53
54
55
56
package mercure
import (
"log/slog"
"github.com/gofrs/uuid/v5"
)
// Update represents an update to send to subscribers.
type Update struct {
// The Server-Sent Event to send.
Event
// The topics' Internationalized Resource Identifier (RFC3987) (will most likely be URLs).
// The first one is the canonical IRI, while next ones are alternate IRIs.
Topics []string
// Private updates can only be dispatched to subscribers authorized to receive them.
Private bool
// To print debug information
Debug bool
}
func (u *Update) LogValue() slog.Value {
attrs := []slog.Attr{
slog.String("id", u.ID),
slog.String("type", u.Type),
slog.Uint64("retry", u.Retry),
slog.Any("topics", u.Topics),
slog.Bool("private", u.Private),
}
if u.Debug {
attrs = append(attrs, slog.String("data", u.Data))
}
return slog.GroupValue(attrs...)
}
type serializedUpdate struct {
*Update
event string
}
// AssignUUID generates a new UUID an assign it to the given update if no ID is already set.
func (u *Update) AssignUUID() {
if u.ID == "" {
u.ID = "urn:uuid:" + uuid.Must(uuid.NewV7()).String()
}
}
func newSerializedUpdate(u *Update) *serializedUpdate {
return &serializedUpdate{u, u.String()}
}