request.go 983 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package request
  2. import (
  3. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  4. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  5. "github.com/gin-gonic/gin/binding"
  6. "net/http"
  7. )
  8. func BindingJson[O any](c *binding_context.Context, request any, sendFunc response.SendFunc[O]) bool {
  9. err := c.ShouldBindJSON(request)
  10. if err != nil {
  11. var zero O
  12. sendFunc(c, http.StatusBadRequest, zero, err)
  13. return false
  14. }
  15. return true
  16. }
  17. func BindingQuery[O any](c *binding_context.Context, query any, sendFunc response.SendFunc[O]) bool {
  18. err := c.ShouldBindQuery(query)
  19. if err != nil {
  20. var zero O
  21. sendFunc(c, http.StatusBadRequest, zero, err)
  22. return false
  23. }
  24. return true
  25. }
  26. func BindingMultipartForm[O any](c *binding_context.Context, request any, sendFunc response.SendFunc[O]) bool {
  27. err := c.ShouldBindWith(request, binding.FormMultipart)
  28. if err != nil {
  29. var zero O
  30. sendFunc(c, http.StatusBadRequest, zero, err)
  31. return false
  32. }
  33. return true
  34. }