github.com/sandwich-go/boost@v1.3.29/xencoding/msgpack/msgpack.go (about)

     1  package msgpack
     2  
     3  import (
     4  	"context"
     5  	"github.com/sandwich-go/boost/xencoding"
     6  	msgpack "github.com/vmihailenco/msgpack/v5"
     7  )
     8  
     9  var Codec = &codec{}
    10  
    11  const (
    12  	// CodecName msgpack 加解码名称,可以通过 encoding2.GetCodec(CodecName) 获取对应的 Codec
    13  	CodecName = "msgpack"
    14  )
    15  
    16  func init() {
    17  	xencoding.RegisterCodec(Codec)
    18  }
    19  
    20  // codec is a Codec implementation with json
    21  type codec struct{}
    22  
    23  // Name 返回 Codec 名
    24  func (codec) Name() string { return CodecName }
    25  
    26  // Marshal 编码
    27  func (codec) Marshal(_ context.Context, v interface{}) ([]byte, error) { return msgpack.Marshal(v) }
    28  
    29  // Unmarshal 解码
    30  func (codec) Unmarshal(_ context.Context, data []byte, v interface{}) error {
    31  	return msgpack.Unmarshal(data, v)
    32  }