-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.go
61 lines (50 loc) · 1.17 KB
/
messages.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
49
50
51
52
53
54
55
56
57
58
59
60
61
package xqueue
import (
"context"
"encoding/json"
"fmt"
"time"
)
type AckFunc func() error
type NackFunc func() error
type Message struct {
Ctx context.Context
ID string
UUID string
Payload []byte
Topic string
Timestamp time.Time
Metadata map[string]interface{}
RedeliveryCount int
ackFn AckFunc
nackFn NackFunc
}
func (m Message) String() string {
return fmt.Sprintf("id[%v] uuid[%v] topic[%v] time[%v] redelivery[%v] data[%s]",
m.ID, m.UUID, m.Topic, m.Timestamp.Format("2006-01-02 15:04:05.999"), m.RedeliveryCount, string(m.Payload))
}
func (m Message) Unmarshal(v interface{}) error {
return json.Unmarshal(m.Payload, v)
}
func (m Message) Marshal(v interface{}) (err error) {
m.Payload, err = json.Marshal(v)
return
}
func (m Message) Ack() error {
if m.ackFn == nil {
return fmt.Errorf("ack function is nil")
}
return m.ackFn()
}
func (m Message) SetAckFunc(f AckFunc) {
m.ackFn = f
}
func (m Message) Nack() error {
if m.nackFn == nil {
return fmt.Errorf("nack function is nil")
}
return m.nackFn()
}
func (m Message) SetNackFunc(f NackFunc) {
m.nackFn = f
}