forked from asynkron/protoactor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_envelope.go
97 lines (81 loc) · 1.85 KB
/
message_envelope.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package actor
type messageHeader map[string]string
func (m messageHeader) Get(key string) string {
return m[key]
}
func (m messageHeader) Set(key string, value string) {
m[key] = value
}
func (m messageHeader) Keys() []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func (m messageHeader) Length() int {
return len(m)
}
func (m messageHeader) ToMap() map[string]string {
mp := make(map[string]string)
for k, v := range m {
mp[k] = v
}
return mp
}
type ReadonlyMessageHeader interface {
Get(key string) string
Keys() []string
Length() int
ToMap() map[string]string
}
type MessageEnvelope struct {
Header messageHeader
Message interface{}
Sender *PID
}
func (me *MessageEnvelope) GetHeader(key string) string {
if me.Header == nil {
return ""
}
return me.Header.Get(key)
}
func (me *MessageEnvelope) SetHeader(key string, value string) {
if me.Header == nil {
me.Header = make(map[string]string)
}
me.Header.Set(key, value)
}
var (
EmptyMessageHeader = make(messageHeader)
)
func WrapEnvelope(message interface{}) *MessageEnvelope {
if e, ok := message.(*MessageEnvelope); ok {
return e
}
return &MessageEnvelope{nil, message, nil}
}
func UnwrapEnvelope(message interface{}) (ReadonlyMessageHeader, interface{}, *PID) {
if env, ok := message.(*MessageEnvelope); ok {
return env.Header, env.Message, env.Sender
}
return nil, message, nil
}
func UnwrapEnvelopeHeader(message interface{}) ReadonlyMessageHeader {
if env, ok := message.(*MessageEnvelope); ok {
return env.Header
}
return nil
}
func UnwrapEnvelopeMessage(message interface{}) interface{} {
if env, ok := message.(*MessageEnvelope); ok {
return env.Message
}
return message
}
func UnwrapEnvelopeSender(message interface{}) *PID {
if env, ok := message.(*MessageEnvelope); ok {
return env.Sender
}
return nil
}