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

     1  // Copyright 2014 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  package binding
     6  
     7  import (
     8  	"github.com/hellobchain/newcryptosm/http"
     9  	"io/ioutil"
    10  
    11  	"github.com/golang/protobuf/proto"
    12  )
    13  
    14  type protobufBinding struct{}
    15  
    16  func (protobufBinding) Name() string {
    17  	return "protobuf"
    18  }
    19  
    20  func (b protobufBinding) Bind(req *http.Request, obj interface{}) error {
    21  	buf, err := ioutil.ReadAll(req.Body)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	return b.BindBody(buf, obj)
    26  }
    27  
    28  func (protobufBinding) BindBody(body []byte, obj interface{}) error {
    29  	if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {
    30  		return err
    31  	}
    32  	// Here it's same to return validate(obj), but util now we can't add
    33  	// `binding:""` to the struct which automatically generate by gen-proto
    34  	return nil
    35  	// return validate(obj)
    36  }