mirror of
https://github.com/foomo/gotsrpc.git
synced 2026-04-28 00:24:42 +00:00
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package gotsrpc
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/ugorji/go/codec"
|
|
)
|
|
|
|
var msgpackHandle = &transportHandle{
|
|
contentType: "application/msgpack; charset=utf-8",
|
|
handle: &codec.MsgpackHandle{},
|
|
beforeEncodeReply: newErrorEncodeHook(),
|
|
beforeDecodeReply: newErrorDecodeHook(),
|
|
afterDecodeReply: newErrorAfterDecodeHook(),
|
|
}
|
|
|
|
func init() {
|
|
mh := new(codec.MsgpackHandle)
|
|
// use map[string]interface{} instead of map[interface{}]interface{}
|
|
mh.MapType = reflect.TypeFor[map[string]any]()
|
|
// attempting to set the promoted field in literal will cause a compiler error
|
|
mh.RawToString = true
|
|
mh.ReaderBufferSize = 4096
|
|
mh.WriterBufferSize = 4096
|
|
msgpackHandle.handle = mh
|
|
|
|
registerTransportHandle(EncodingMsgpack, msgpackHandle)
|
|
}
|
|
|
|
func NewMSGPackEncoderBytes(b *[]byte) *codec.Encoder {
|
|
return codec.NewEncoderBytes(b, msgpackHandle.handle)
|
|
}
|
|
|
|
func NewMSGPackDecoderBytes(b []byte) *codec.Decoder {
|
|
return codec.NewDecoderBytes(b, msgpackHandle.handle)
|
|
}
|
|
|
|
func SetMSGPackExt(rt any, tag uint64, ext codec.BytesExt) error {
|
|
if value, ok := msgpackHandle.handle.(*codec.MsgpackHandle); ok {
|
|
return value.SetBytesExt(reflect.TypeOf(rt), tag, ext)
|
|
}
|
|
|
|
return errors.New("invalid handle type")
|
|
}
|