You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
727 B
34 lines
727 B
package mq
|
|
|
|
type Message struct {
|
|
value []byte
|
|
}
|
|
|
|
type encodeMessageFunc func(source interface{}) ([]byte, error)
|
|
type decodeMessageFunc func(target interface{}, message *Message) error
|
|
|
|
type MessageParser struct {
|
|
encode encodeMessageFunc
|
|
decode decodeMessageFunc
|
|
}
|
|
|
|
func NewMessageParser(encode encodeMessageFunc, decode decodeMessageFunc) *MessageParser {
|
|
return &MessageParser{
|
|
encode: encode,
|
|
decode: decode,
|
|
}
|
|
}
|
|
|
|
func (p *MessageParser) Encode(source interface{}) (*Message, error) {
|
|
value, err := p.encode(source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Message{
|
|
value: value,
|
|
}, nil
|
|
}
|
|
|
|
func (p *MessageParser) Decode(target interface{}, message *Message) error {
|
|
return p.decode(target, message)
|
|
}
|
|
|