-
Notifications
You must be signed in to change notification settings - Fork 15
/
marshaller_custom.go
299 lines (253 loc) · 6.16 KB
/
marshaller_custom.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package dingo
import (
"encoding/json"
"errors"
)
/*CustomMarshallerCodec is used by a marshaller developed to help users to
provide a customized marshaller by providing a "codec" to encode/decode arguments/returns.
*/
type CustomMarshallerCodec interface {
/*
A hook called when CustomMarshaller.Prepare is called.
*/
Prepare(name string, fn interface{}) (err error)
/*
encode arguments.
- fn: function fingerprint
- val: slice of arguments
You can encode each argument one by one, and compose them into one
slice of byte slice. (or anyway you want)
*/
EncodeArgument(fn interface{}, val []interface{}) ([][]byte, error)
/*
decode arguments.
- fn: function fingerprint
- bs: slice of byte slice
*/
DecodeArgument(fn interface{}, bs [][]byte) ([]interface{}, error)
/*
encode returns.
- fn: function fingerprint
- val: slice of returns
You can encode each return one by one, and compose them into one
slice of byte slice. (or anyway you want)
*/
EncodeReturn(fn interface{}, val []interface{}) ([][]byte, error)
/*
decode arguments.
- fn: function fingerprint
- bs: slice of byte slice
*/
DecodeReturn(fn interface{}, bs [][]byte) ([]interface{}, error)
}
/*CustomMarshaller is a helper Marshaller for users to create customized Marshaller(s) by providing
several hooks. Users just need to take care of things they know:
- input arguments
- outpu return values
other payloads of task/report are handled by CustomMarshaller.
Here is a partial demo with json:
// worker function, we are going to provide a custom marshaller
// without any reflect for it.
fn := func(msg string, category int) (done bool) {
...
}
// implement CustomMarshallerCodec interface
type myCodec struct {}
// encoding arguments
func (c *myCodec) EncodeArgument(fn interface{}, val []interface{}) ([][]byte, error) {
bMsg, _ := json.Marshal(val[0])
bCategory, _ := json.Marshal(val[1])
return [][]byte{bMsg, bCategory}, nil
}
// encoding returns
func (c *myCodec) EncodeReturn(fn interface{}, val []interface{}) ([][]byte, error) {
bDone, _ := json.Marshal(val[0])
return [][]byte{bDone}, nil
}
// decoding arguments
func (c *myCodec) DecodeArgument(fn interface{}, bs [][]byte) ([]interface{}, error) {
var (
msg string
category int
)
// unmarshall each argument
json.Unmarshal(bs[0], &msg)
json.Unmarshal(bs[1], &category)
return []interface{}{msg, category}, nil
}
func (c *myCodec) DecodeReturn(fn interface{}, bs [][]byte) ([]interface{}, error) {
var done bool
json.Unmarshal(bs[0], &done)
return []interface{}{done}, nil
}
// register it to dingo.App
app.AddMarshaller(expectedMashId, &struct{
CustomMarshaller,
myCustomInvoker,
}{
CustomMarshaller{Codec: &myCodec{}},
myCustomInvoker{},
})
*/
type CustomMarshaller struct {
Codec CustomMarshallerCodec
}
func (ms *CustomMarshaller) Prepare(name string, fn interface{}) (err error) {
if ms.Codec != nil {
err = ms.Codec.Prepare(name, fn)
}
return
}
func (ms *CustomMarshaller) EncodeTask(fn interface{}, task *Task) (b []byte, err error) {
if task == nil {
err = errors.New("Task(nil) is not acceptable")
return
}
bs, args := [][]byte{}, task.Args()
if len(args) > 0 {
if ms.Codec == nil {
err = errors.New("Encode hook is not available")
return
}
if bs, err = ms.Codec.EncodeArgument(fn, args); err != nil {
return
}
}
var bOpt []byte
if bOpt, err = json.Marshal(task.P.O); err != nil {
return
}
b, err = ComposeBytes(task.H, append(bs, bOpt))
return
}
func (ms *CustomMarshaller) DecodeTask(h *Header, fn interface{}, b []byte) (task *Task, err error) {
// decode header
if h == nil {
if h, err = DecodeHeader(b); err != nil {
return
}
}
// clean registry when leaving
defer func() {
if h != nil {
h.Reset()
}
}()
var (
bs [][]byte
args = []interface{}{}
o *Option
)
if bs, err = DecomposeBytes(h, b); err != nil {
return
}
// option would only occupy 1 slot
if len(bs) > 1 {
if ms.Codec == nil {
err = errors.New("Decode hook is not available")
return
}
if args, err = ms.Codec.DecodeArgument(fn, bs[:len(bs)-1]); err != nil {
return
}
}
// decode option
if err = json.Unmarshal(bs[len(bs)-1], &o); err != nil {
return
}
task = &Task{
H: h,
P: &TaskPayload{
O: o,
A: args,
},
}
return
}
func (ms *CustomMarshaller) EncodeReport(fn interface{}, report *Report) (b []byte, err error) {
if report == nil {
err = errors.New("Report(nil) is not acceptable")
return
}
// reset registry
report.H.Reset()
bs, returns := [][]byte{}, report.Return()
if len(returns) > 0 {
if ms.Codec == nil {
err = errors.New("Encode hook is not available")
return
}
if bs, err = ms.Codec.EncodeReturn(fn, returns); err != nil {
return
}
}
var (
bStatus, bErr, bOpt []byte
)
if bStatus, err = json.Marshal(report.P.S); err != nil {
return
}
if bErr, err = json.Marshal(report.P.E); err != nil {
return
}
if bOpt, err = json.Marshal(report.P.O); err != nil {
return
}
b, err = ComposeBytes(report.H, append(bs, bStatus, bErr, bOpt))
return
}
func (ms *CustomMarshaller) DecodeReport(h *Header, fn interface{}, b []byte) (report *Report, err error) {
// decode header
if h == nil {
if h, err = DecodeHeader(b); err != nil {
return
}
}
// clean registry when leaving
defer func() {
if h != nil {
h.Reset()
}
}()
var (
s int16
e *Error
o *Option
bs [][]byte
)
if bs, err = DecomposeBytes(h, b); err != nil {
return
}
var returns = []interface{}{}
if len(bs) > 3 {
if ms.Codec == nil {
err = errors.New("Decode hook is not available")
return
}
if returns, err = ms.Codec.DecodeReturn(fn, bs[:len(bs)-3]); err != nil {
return
}
}
// decode status
if err = json.Unmarshal(bs[len(bs)-3], &s); err != nil {
return
}
// decode err
if err = json.Unmarshal(bs[len(bs)-2], &e); err != nil {
return
}
// decode option
if err = json.Unmarshal(bs[len(bs)-1], &o); err != nil {
return
}
report = &Report{
H: h,
P: &ReportPayload{
S: s,
E: e,
O: o,
R: returns,
},
}
return
}