-
Notifications
You must be signed in to change notification settings - Fork 46
/
message.go
54 lines (49 loc) · 1.69 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
package tcpx
// Message contains the necessary parts of tcpx protocol
// MessagID is defining a message routing flag.
// Header is an attachment of a message.
// Body is the message itself, it should be raw message not serialized yet, like "hello", not []byte("hello")
type Message struct {
MessageID int32 `json:"message_id"`
Header map[string]interface{} `json:"header"`
Body interface{} `json:"body"`
}
func NewMessage(messageID int32, src interface{}) Message {
return Message{
MessageID: messageID,
Header: map[string]interface{}{
HEADER_ROUTER_KEY: MESSAGEID,
HEADER_ROUTER_VALUE: messageID,
},
Body: src,
}
}
func NewURLPatternMessage(urlPattern string, src interface{}) Message {
return Message{
MessageID: 0,
Header: map[string]interface{}{
HEADER_ROUTER_KEY: URLPATTERN,
HEADER_ROUTER_VALUE: urlPattern,
},
Body: src,
}
}
func (m Message) Pack(marshaller Marshaller) ([]byte, error) {
return PackWithMarshaller(m, marshaller)
}
// Get value of message's header whose key is 'key'
// Get and Set don't have lock to ensure concurrently safe, which means
// if you should never operate the header in multiple goroutines, it's better to design a context yourself per request
// rather than straightly use message.Header.
func (msg Message) Get(key string) interface{} {
if msg.Header == nil {
return nil
}
return msg.Header[key]
}
// Get and Set don't have lock to ensure concurrently safe, which means
// if you should never operate the header in multiple goroutines, it's better to design a context yourself per request
// rather than straightly use message.Header.
func (msg *Message) Set(k string, v interface{}) {
msg.Header[k] = v
}