dto.go 862 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package binding
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/service-supports/fserr"
  5. "github.com/gin-gonic/gin/binding"
  6. )
  7. type DTO interface{}
  8. func ToConcreteDTO[T DTO](object DTO) (T, error) {
  9. concrete, ok := object.(T)
  10. if !ok {
  11. var zero T
  12. return zero, fserr.New("DTO转化失败")
  13. }
  14. return concrete, nil
  15. }
  16. func JsonBody(c *api.Context, dto DTO) error {
  17. return c.ShouldBindJSON(dto)
  18. }
  19. func QueryParams(c *api.Context, dto DTO) error {
  20. return c.ShouldBindQuery(dto)
  21. }
  22. func PathParams(c *api.Context, dto DTO) error {
  23. return c.ShouldBindUri(dto)
  24. }
  25. func MultipartForm(c *api.Context, dto DTO) error {
  26. return c.ShouldBindWith(dto, binding.FormMultipart)
  27. }
  28. func Form(c *api.Context, dto DTO) error {
  29. return c.ShouldBindWith(dto, binding.Form)
  30. }
  31. func XMLBody(c *api.Context, dto DTO) error {
  32. return c.ShouldBindXML(dto)
  33. }