github.com/songzhibin97/gkit@v1.2.13/tools/bind/msgpack.go (about)

     1  //go:build !nomsgpack
     2  // +build !nomsgpack
     3  
     4  package bind
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"net/http"
    10  
    11  	"github.com/ugorji/go/codec"
    12  )
    13  
    14  type msgpackBinding struct{}
    15  
    16  func (msgpackBinding) Name() string {
    17  	return "msgpack"
    18  }
    19  
    20  func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
    21  	return decodeMsgPack(req.Body, obj)
    22  }
    23  
    24  func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
    25  	return decodeMsgPack(bytes.NewReader(body), obj)
    26  }
    27  
    28  func decodeMsgPack(r io.Reader, obj interface{}) error {
    29  	cdc := new(codec.MsgpackHandle)
    30  	return codec.NewDecoder(r, cdc).Decode(&obj)
    31  }