-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_codec.go
53 lines (45 loc) · 1.58 KB
/
proto_codec.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
package jsoniterpb
import (
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/modern-go/reflect2"
)
var ProtoCodecs = map[reflect2.Type]*ProtoCodec{}
func (e *ProtoExtension) createProtoEncoder(typ reflect2.Type) (xret jsoniter.ValEncoder) {
codec, ok := ProtoCodecs[typ]
if ok && codec != nil && codec.EncoderCreator != nil {
return codec.EncoderCreator(e, typ)
}
return nil
}
func (e *ProtoExtension) createProtoDecoder(typ reflect2.Type) (xret jsoniter.ValDecoder) {
codec, ok := ProtoCodecs[typ]
if ok && codec != nil && codec.DecoderCreator != nil {
return codec.DecoderCreator(e, typ)
}
return nil
}
type ProtoCodec struct {
EncoderCreator func(e *ProtoExtension, typ reflect2.Type) jsoniter.ValEncoder
DecoderCreator func(e *ProtoExtension, typ reflect2.Type) jsoniter.ValDecoder
}
func (codec *ProtoCodec) SetElemEncodeFunc(encodeFunc func(e *ProtoExtension, ptr unsafe.Pointer, stream *jsoniter.Stream)) *ProtoCodec {
codec.EncoderCreator = func(e *ProtoExtension, typ reflect2.Type) jsoniter.ValEncoder {
return WrapElemEncoder(typ, &funcEncoder{
fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
encodeFunc(e, ptr, stream)
},
}, nil)
}
return codec
}
func (codec *ProtoCodec) SetElemDecodeFunc(decodeFunc func(e *ProtoExtension, ptr unsafe.Pointer, iter *jsoniter.Iterator)) *ProtoCodec {
codec.DecoderCreator = func(e *ProtoExtension, typ reflect2.Type) jsoniter.ValDecoder {
return WrapElemDecoder(typ, &funcDecoder{
fun: func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
decodeFunc(e, ptr, iter)
},
}, nil)
}
return codec
}