github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/message_envelope.go (about) 1 package actor 2 3 type messageHeader map[string]string 4 5 func (header messageHeader) Get(key string) string { 6 return header[key] 7 } 8 9 func (header messageHeader) Set(key string, value string) { 10 header[key] = value 11 } 12 13 func (header messageHeader) Keys() []string { 14 keys := make([]string, 0, len(header)) 15 for k := range header { 16 keys = append(keys, k) 17 } 18 return keys 19 } 20 21 func (header messageHeader) Length() int { 22 return len(header) 23 } 24 25 func (header messageHeader) ToMap() map[string]string { 26 mp := make(map[string]string) 27 for k, v := range header { 28 mp[k] = v 29 } 30 return mp 31 } 32 33 type ReadonlyMessageHeader interface { 34 Get(key string) string 35 Keys() []string 36 Length() int 37 ToMap() map[string]string 38 } 39 40 type MessageEnvelope struct { 41 Header messageHeader 42 Message interface{} 43 Sender *PID 44 } 45 46 func (envelope *MessageEnvelope) GetHeader(key string) string { 47 if envelope.Header == nil { 48 return "" 49 } 50 return envelope.Header.Get(key) 51 } 52 53 func (envelope *MessageEnvelope) SetHeader(key string, value string) { 54 if envelope.Header == nil { 55 envelope.Header = make(map[string]string) 56 } 57 envelope.Header.Set(key, value) 58 } 59 60 var EmptyMessageHeader = make(messageHeader) 61 62 func WrapEnvelope(message interface{}) *MessageEnvelope { 63 if e, ok := message.(*MessageEnvelope); ok { 64 return e 65 } 66 return &MessageEnvelope{nil, message, nil} 67 } 68 69 func UnwrapEnvelope(message interface{}) (ReadonlyMessageHeader, interface{}, *PID) { 70 if env, ok := message.(*MessageEnvelope); ok { 71 return env.Header, env.Message, env.Sender 72 } 73 return nil, message, nil 74 } 75 76 func UnwrapEnvelopeHeader(message interface{}) ReadonlyMessageHeader { 77 if env, ok := message.(*MessageEnvelope); ok { 78 return env.Header 79 } 80 return nil 81 } 82 83 func UnwrapEnvelopeMessage(message interface{}) interface{} { 84 if env, ok := message.(*MessageEnvelope); ok { 85 return env.Message 86 } 87 return message 88 } 89 90 func UnwrapEnvelopeSender(message interface{}) *PID { 91 if env, ok := message.(*MessageEnvelope); ok { 92 return env.Sender 93 } 94 return nil 95 }