-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
39 lines (28 loc) · 815 Bytes
/
message.go
File metadata and controls
39 lines (28 loc) · 815 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
package pubsub
import "github.com/vmihailenco/msgpack/v5"
type MessageFilterKind int
type FilterMap = map[MessageFilterKind]interface{}
type Message[T any] struct {
Filters FilterMap `msgpack:"f,omitempty"`
Payload T `msgpack:"p,omitempty"`
}
func (m *Message[T]) WithFilter(kind MessageFilterKind, value T) *Message[T] {
m.Filters[kind] = value
return m
}
type rawMessage[T any] Message[T]
func (m *Message[T]) MarshalBinary() ([]byte, error) {
return msgpack.Marshal((*rawMessage[T])(m))
}
func (m *Message[T]) UnmarshalBinary(b []byte) error {
return msgpack.Unmarshal(b, (*rawMessage[T])(m))
}
func NewMessage[T any](payload T, opts ...FilterMap) *Message[T] {
if len(opts) == 0 {
opts = append(opts, FilterMap{})
}
return &Message[T]{
Payload: payload,
Filters: opts[0],
}
}