forked from centrifugal/centrifuge-go
/
marshal.go
252 lines (208 loc) · 5.25 KB
/
marshal.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package proto
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/gogo/protobuf/proto"
)
// CommandEncoder ...
type CommandEncoder interface {
Encode(cmd *Command) ([]byte, error)
}
// JSONCommandEncoder ...
type JSONCommandEncoder struct {
}
// NewJSONCommandEncoder ...
func NewJSONCommandEncoder() *JSONCommandEncoder {
return &JSONCommandEncoder{}
}
// Encode ...
func (e *JSONCommandEncoder) Encode(cmd *Command) ([]byte, error) {
return json.Marshal(cmd)
}
// ProtobufCommandEncoder ...
type ProtobufCommandEncoder struct {
}
// NewProtobufCommandEncoder ...
func NewProtobufCommandEncoder() *ProtobufCommandEncoder {
return &ProtobufCommandEncoder{}
}
// Encode ...
func (e *ProtobufCommandEncoder) Encode(cmd *Command) ([]byte, error) {
commandBytes, err := cmd.Marshal()
if err != nil {
return nil, err
}
bs := make([]byte, 8)
n := binary.PutUvarint(bs, uint64(len(commandBytes)))
var buf bytes.Buffer
buf.Write(bs[:n])
buf.Write(commandBytes)
return buf.Bytes(), nil
}
// PushEncoder ...
type PushEncoder interface {
Encode(*Push) ([]byte, error)
EncodeMessage(*Message) ([]byte, error)
EncodePublication(*Publication) ([]byte, error)
EncodeJoin(*Join) ([]byte, error)
EncodeLeave(*Leave) ([]byte, error)
EncodeUnsub(*Unsub) ([]byte, error)
}
// JSONPushEncoder ...
type JSONPushEncoder struct {
}
// NewJSONPushEncoder ...
func NewJSONPushEncoder() *JSONPushEncoder {
return &JSONPushEncoder{}
}
// Encode ...
func (e *JSONPushEncoder) Encode(message *Push) ([]byte, error) {
return json.Marshal(message)
}
// EncodePublication ...
func (e *JSONPushEncoder) EncodePublication(message *Publication) ([]byte, error) {
return json.Marshal(message)
}
// EncodeMessage ...
func (e *JSONPushEncoder) EncodeMessage(message *Message) ([]byte, error) {
return json.Marshal(message)
}
// EncodeJoin ...
func (e *JSONPushEncoder) EncodeJoin(message *Join) ([]byte, error) {
return json.Marshal(message)
}
// EncodeLeave ...
func (e *JSONPushEncoder) EncodeLeave(message *Leave) ([]byte, error) {
return json.Marshal(message)
}
// EncodeUnsub ...
func (e *JSONPushEncoder) EncodeUnsub(message *Unsub) ([]byte, error) {
return json.Marshal(message)
}
// ProtobufPushEncoder ...
type ProtobufPushEncoder struct {
}
// NewProtobufPushEncoder ...
func NewProtobufPushEncoder() *ProtobufPushEncoder {
return &ProtobufPushEncoder{}
}
// Encode ...
func (e *ProtobufPushEncoder) Encode(message *Push) ([]byte, error) {
return message.Marshal()
}
// EncodePublication ...
func (e *ProtobufPushEncoder) EncodePublication(message *Publication) ([]byte, error) {
return message.Marshal()
}
// EncodePush ...
func (e *ProtobufPushEncoder) EncodeMessage(message *Message) ([]byte, error) {
return message.Marshal()
}
// EncodeJoin ...
func (e *ProtobufPushEncoder) EncodeJoin(message *Join) ([]byte, error) {
return message.Marshal()
}
// EncodeLeave ...
func (e *ProtobufPushEncoder) EncodeLeave(message *Leave) ([]byte, error) {
return message.Marshal()
}
// EncodeUnsub ...
func (e *ProtobufPushEncoder) EncodeUnsub(message *Unsub) ([]byte, error) {
return message.Marshal()
}
// ReplyEncoder ...
type ReplyEncoder interface {
Reset()
Encode(*Reply) error
Finish() []byte
}
// JSONReplyEncoder ...
type JSONReplyEncoder struct {
buffer bytes.Buffer
}
// NewJSONReplyEncoder ...
func NewJSONReplyEncoder() *JSONReplyEncoder {
return &JSONReplyEncoder{}
}
// Reset ...
func (e *JSONReplyEncoder) Reset() {
e.buffer.Reset()
}
// Encode ...
func (e *JSONReplyEncoder) Encode(r *Reply) error {
data, err := json.Marshal(r)
if err != nil {
return err
}
e.buffer.Write(data)
e.buffer.WriteString("\n")
return nil
}
// Finish ...
func (e *JSONReplyEncoder) Finish() []byte {
data := e.buffer.Bytes()
dataCopy := make([]byte, len(data))
copy(dataCopy, data)
return dataCopy
}
// ProtobufReplyEncoder ...
type ProtobufReplyEncoder struct {
buffer bytes.Buffer
}
// NewProtobufReplyEncoder ...
func NewProtobufReplyEncoder() *ProtobufReplyEncoder {
return &ProtobufReplyEncoder{}
}
// Encode ...
func (e *ProtobufReplyEncoder) Encode(r *Reply) error {
replyBytes, err := r.Marshal()
if err != nil {
return err
}
bs := make([]byte, 8)
n := binary.PutUvarint(bs, uint64(len(replyBytes)))
e.buffer.Write(bs[:n])
e.buffer.Write(replyBytes)
return nil
}
// Reset ...
func (e *ProtobufReplyEncoder) Reset() {
e.buffer.Reset()
}
// Finish ...
func (e *ProtobufReplyEncoder) Finish() []byte {
data := e.buffer.Bytes()
dataCopy := make([]byte, len(data))
copy(dataCopy, data)
return dataCopy
}
// ParamsEncoder ...
type ParamsEncoder interface {
Encode(request interface{}) ([]byte, error)
}
// JSONParamsEncoder ...
type JSONParamsEncoder struct{}
// NewJSONParamsEncoder ...
func NewJSONParamsEncoder() *JSONParamsEncoder {
return &JSONParamsEncoder{}
}
// Encode ...
func (d *JSONParamsEncoder) Encode(r interface{}) ([]byte, error) {
return json.Marshal(r)
}
// ProtobufParamsEncoder ...
type ProtobufParamsEncoder struct{}
// NewProtobufParamsEncoder ...
func NewProtobufParamsEncoder() *ProtobufParamsEncoder {
return &ProtobufParamsEncoder{}
}
// Encode ...
func (d *ProtobufParamsEncoder) Encode(r interface{}) ([]byte, error) {
m, ok := r.(proto.Marshaler)
if !ok {
return nil, fmt.Errorf("can not marshal type %T to Protobuf", r)
}
return m.Marshal()
}