-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
66 lines (57 loc) · 1.68 KB
/
msg.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
62
63
64
65
66
package mqlib
import (
"strings"
json "github.com/json-iterator/go"
"github.com/wgdzlh/mqlib/rk/primitive"
)
var (
emptyBody = []byte("-")
)
type Message struct {
Id string `json:"id,omitempty"` // 消息ID
RemoteApp string `json:"remote_app,omitempty"` // RPC调用服务端应用名称
Topic string `json:"topic,omitempty"` // 消息主题(可选)
Tag string `json:"tag"` // RPC调用接口名称
Keys []string `json:"keys"` // 携带的业务KEY(可选)
Body []byte `json:"-"` // 业务消息体
rawMsg *primitive.MessageExt `json:"-"` // 原始MQ消息,自动处理,勿动
}
func (m *Message) ToString() string {
out, _ := json.MarshalToString(m)
return out
}
func (m *Message) ToRkMessage() *primitive.Message {
if len(m.Body) == 0 {
m.Body = emptyBody
}
return primitive.NewMessage(m.Topic, m.Body).WithTag(m.Tag).WithKeys(m.Keys)
}
func msgFromRkMsg(m *primitive.Message) *Message {
return &Message{
Tag: m.GetTags(),
Keys: getKeysFromMsg(m),
Body: m.Body,
}
}
func msgFromRkMsgExt(me *primitive.MessageExt) *Message {
return &Message{
Id: me.MsgId,
Topic: me.Topic,
Tag: me.GetTags(),
Keys: getKeysFromMsg(&me.Message),
Body: me.Body,
rawMsg: me,
}
}
func getKeysFromMsg(m *primitive.Message) []string {
var keys []string
rawKey := m.GetKeys()
if rawKey != "" {
keys = strings.Split(rawKey, primitive.PropertyKeySeparator)
last := len(keys) - 1
if last > 0 && keys[last] == "" {
keys = keys[:last]
}
}
return keys
}