-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
59 lines (50 loc) · 1.14 KB
/
message.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
package tinyq
import (
"bytes"
"encoding/gob"
"time"
)
const (
timeFormat = "02-Jan-2006 15:04:05"
)
// MessageStatus
type MessageStatus string
const (
Pending MessageStatus = "pending"
Success MessageStatus = "success"
Failed MessageStatus = "failed"
)
// Message
type Message struct {
UUID string `json:"uuid"`
Status MessageStatus `json:"status"`
Body []byte `json:"body"`
Detail string `json:"detail"`
Timestamp string `json:"timestamp"`
}
// Messages
type Messages struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
Page uint64 `json:"page"`
Items []Message `json:"items"`
}
// NewMessage
func NewMessage(key string, b interface{}) (Message, error) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(b); err != nil {
return Message{}, err
}
return Message{
UUID: key,
Status: Pending,
Body: buf.Bytes(),
Detail: "",
Timestamp: time.Now().Format(timeFormat),
}, nil
}
// Data
func (msg Message) Value(v interface{}) error {
return gob.NewDecoder(bytes.NewReader(msg.Body)).Decode(v)
}