bind_proto.go 667 B

12345678910111213141516171819202122232425262728293031323334
  1. package ginutil
  2. import (
  3. "net/http"
  4. "kpt-tmr-group/pkg/jsonpb"
  5. "kpt-tmr-group/pkg/xerr"
  6. "github.com/gin-gonic/gin"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. type ProtoMessageQueryBinding struct{}
  10. func BindQueryProto(c *gin.Context, pb proto.Message) error {
  11. values := c.Request.URL.Query()
  12. if err := jsonpb.UnmarshalQuery(values, pb); err != nil {
  13. return xerr.WithStack(err)
  14. }
  15. return nil
  16. }
  17. func BindProto(c *gin.Context, pb proto.Message) (err error) {
  18. switch c.Request.Method {
  19. case http.MethodGet, http.MethodDelete:
  20. err = BindQueryProto(c, pb)
  21. default:
  22. err = BindProtoMessage(c, pb)
  23. }
  24. if err != nil {
  25. return xerr.WithStack(err)
  26. }
  27. return
  28. }