forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
46 lines (37 loc) · 865 Bytes
/
encode.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
package msgqueue
import (
"bytes"
"encoding/base64"
"fmt"
"reflect"
"gopkg.in/vmihailenco/msgpack.v2"
)
func encodeArgs(args []interface{}) (string, error) {
b, err := msgpack.Marshal(args...)
if err != nil {
return "", err
}
s := base64.StdEncoding.EncodeToString(b)
return s, nil
}
func decodeArgs(s string, fnType reflect.Type) ([]reflect.Value, error) {
if fnType.NumIn() == 0 {
return nil, nil
}
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(b)
dec := msgpack.NewDecoder(buf)
in := make([]reflect.Value, fnType.NumIn())
for i := 0; i < fnType.NumIn(); i++ {
arg := reflect.New(fnType.In(i)).Elem()
if err := dec.DecodeValue(arg); err != nil {
err = fmt.Errorf("queue: arg=%d decoding failed: %s", i, err)
return nil, err
}
in[i] = arg
}
return in, nil
}