Newer
Older
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
57
58
59
60
61
62
63
64
65
66
67
68
package emissionqueue
import (
"encoding/json"
"github.com/jackc/pgtype"
"github.com/m4rw3r/uuid"
)
// Message ...
type Message struct {
ID uint `db:"id"`
EnvelopeID uuid.UUID `db:"envelope_id"`
ProcessID uuid.UUID `db:"process_id"`
RelatedTo pgtype.TextArray `db:"related_to"`
MsgType string `db:"msgtype"`
Content []byte `db:"content"`
Chunks pgtype.ByteaArray `db:"chunks"`
}
// NewMessage creates a message.
func NewMessage(relatedTo string, msgType string, content []byte) Message {
m := Message{
MsgType: msgType,
Content: content,
}
if err := m.RelatedTo.Set([]string{relatedTo}); err != nil {
panic(err)
}
if err := m.Chunks.Set(nil); err != nil {
panic(err)
}
return m
}
func NewChunkedMessage(msgType string, chunks [][]byte) Message {
m := Message{
MsgType: msgType,
}
if err := m.RelatedTo.Set([]string{}); err != nil {
panic(err)
}
if err := m.Chunks.Set(chunks); err != nil {
panic(err)
}
return m
}
func NewMessageJSON(relatedTo string, msgType string, payload interface{}) (*Message, error) {
b, err := json.Marshal(payload)
if err != nil {
return nil, err
}
m := NewMessage(relatedTo, msgType, b)
return &m, nil
}
func MustNewMessageJSON(relatedTo string, msgType string, payload interface{}) Message {
m, err := NewMessageJSON(relatedTo, msgType, payload)
if err != nil {
panic(err)
}
return *m
}