forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
41 lines (31 loc) · 768 Bytes
/
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
package messaging
import (
"encoding/json"
"github.com/lovoo/goka"
)
var (
SentStream goka.Stream = "message_sent"
ReceivedStream goka.Stream = "message_received"
)
type Message struct {
From string
To string
Content string
}
type MessageCodec struct{}
func (c *MessageCodec) Encode(value interface{}) ([]byte, error) {
return json.Marshal(value)
}
func (c *MessageCodec) Decode(data []byte) (interface{}, error) {
var m Message
return &m, json.Unmarshal(data, &m)
}
type MessageListCodec struct{}
func (c *MessageListCodec) Encode(value interface{}) ([]byte, error) {
return json.Marshal(value)
}
func (c *MessageListCodec) Decode(data []byte) (interface{}, error) {
var m []Message
err := json.Unmarshal(data, &m)
return m, err
}