github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/msgpack.go (about)

     1  // Copyright 2017 Manu Martinez-Almeida.  All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !nomsgpack
     6  // +build !nomsgpack
     7  
     8  package binding
     9  
    10  import (
    11  	"bytes"
    12  	"github.com/hellobchain/newcryptosm/http"
    13  	"io"
    14  
    15  	"github.com/ugorji/go/codec"
    16  )
    17  
    18  type msgpackBinding struct{}
    19  
    20  func (msgpackBinding) Name() string {
    21  	return "msgpack"
    22  }
    23  
    24  func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
    25  	return decodeMsgPack(req.Body, obj)
    26  }
    27  
    28  func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
    29  	return decodeMsgPack(bytes.NewReader(body), obj)
    30  }
    31  
    32  func decodeMsgPack(r io.Reader, obj interface{}) error {
    33  	cdc := new(codec.MsgpackHandle)
    34  	if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
    35  		return err
    36  	}
    37  	return validate(obj)
    38  }