protobuf.go 862 B

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