github.com/aaabigfish/gopkg@v1.1.0/mq/nsq/message.go (about) 1 package nsq 2 3 import ( 4 "encoding/json" 5 "time" 6 ) 7 8 type Header struct { 9 Key string 10 Value []byte 11 } 12 13 type Message struct { 14 Topic string 15 Headers []Header 16 Key []byte 17 Body []byte 18 19 // If not set at the creation, Time will be automatically set when 20 // writing the message. 21 Time int64 22 } 23 24 func NewMessage() *Message { 25 return &Message{ 26 Time: time.Now().UnixNano(), 27 } 28 } 29 30 func (m *Message) GetKey() string { 31 return string(m.Key) 32 } 33 34 func (m *Message) GetBody() []byte { 35 return m.Body 36 } 37 38 func (m *Message) SetKey(key []byte) *Message { 39 m.Key = key 40 return m 41 } 42 43 func (m *Message) SetHeader(key string, val []byte) *Message { 44 m.Headers = append(m.Headers, Header{ 45 Key: key, 46 Value: val, 47 }) 48 return m 49 } 50 51 func (m *Message) SetTopic(topic string) *Message { 52 m.Topic = topic 53 return m 54 } 55 56 func (m *Message) SetBody(body []byte) *Message { 57 m.Body = body 58 return m 59 } 60 61 func (m *Message) SetBodyJSON(body any) *Message { 62 data, _ := json.Marshal(body) 63 64 return m.SetBody(data) 65 }