github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/codec/msgpack.go (about)

     1  package codec
     2  
     3  import (
     4  	"github.com/vmihailenco/msgpack/v5"
     5  )
     6  
     7  // FIXME 引用包存在不稳定转换
     8  // 该包某些情况下会崩溃
     9  
    10  // msgpackCodec uses messagepack marshaler and unmarshaler.
    11  type msgpackCodec struct{}
    12  
    13  var MsgPack SerializeType = RegisterCodec("MsgPack", &msgpackCodec{})
    14  
    15  // Encode encodes an object into slice of bytes.
    16  func (c msgpackCodec) Encode(i interface{}) ([]byte, error) {
    17  	return msgpack.Marshal(i)
    18  }
    19  
    20  // Decode decodes an object from slice of bytes.
    21  func (c msgpackCodec) Decode(data []byte, i interface{}) error {
    22  	return msgpack.Unmarshal(data, i)
    23  }
    24  
    25  func (c msgpackCodec) String() string {
    26  	return "MsgPack"
    27  }