protobuf.go 759 B

1234567891011121314151617181920212223242526272829303132333435
  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. package binding
  5. import (
  6. "io/ioutil"
  7. "net/http"
  8. "github.com/golang/protobuf/proto"
  9. )
  10. type protobufBinding struct{}
  11. func (protobufBinding) Name() string {
  12. return "protobuf"
  13. }
  14. func (protobufBinding) Bind(req *http.Request, obj interface{}) error {
  15. buf, err := ioutil.ReadAll(req.Body)
  16. if err != nil {
  17. return err
  18. }
  19. if err = proto.Unmarshal(buf, obj.(proto.Message)); err != nil {
  20. return err
  21. }
  22. //Here it's same to return validate(obj), but util now we cann't add `binding:""` to the struct
  23. //which automatically generate by gen-proto
  24. //return nil
  25. return validate(obj)
  26. }