msgpack.go
947 字节
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
package orm
import (
"reflect"
"github.com/vmihailenco/msgpack/v5"
"github.com/go-pg/pg/v10/types"
)
func msgpackAppender(_ reflect.Type) types.AppenderFunc {
return func(b []byte, v reflect.Value, flags int) []byte {
hexEnc := types.NewHexEncoder(b, flags)
enc := msgpack.GetEncoder()
defer msgpack.PutEncoder(enc)
enc.Reset(hexEnc)
if err := enc.EncodeValue(v); err != nil {
return types.AppendError(b, err)
}
if err := hexEnc.Close(); err != nil {
return types.AppendError(b, err)
}
return hexEnc.Bytes()
}
}
func msgpackScanner(_ reflect.Type) types.ScannerFunc {
return func(v reflect.Value, rd types.Reader, n int) error {
if n <= 0 {
return nil
}
hexDec, err := types.NewHexDecoder(rd, n)
if err != nil {
return err
}
dec := msgpack.GetDecoder()
defer msgpack.PutDecoder(dec)
dec.Reset(hexDec)
if err := dec.DecodeValue(v); err != nil {
return err
}
return nil
}
}