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

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