simple_bind_item.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package binding
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding/request"
  4. "git.sxidc.com/go-framework/baize/framework/core/api"
  5. "git.sxidc.com/go-framework/baize/framework/core/domain"
  6. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  7. "net/http"
  8. )
  9. func PostBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
  10. item.bind(binder, http.MethodPost, middlewares...)
  11. }
  12. func DeleteBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
  13. item.bind(binder, http.MethodDelete, middlewares...)
  14. }
  15. func PutBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
  16. item.bind(binder, http.MethodPut, middlewares...)
  17. }
  18. func GetBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
  19. item.bind(binder, http.MethodGet, middlewares...)
  20. }
  21. type RequestParamsBindFunc func(c *api.Context, params request.Params) error
  22. type FormDomainObjectsFunc func(c *api.Context, params request.Params) ([]domain.Object, error)
  23. type ServiceFunc[O any] func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (O, error)
  24. type ResponseFunc[O any] func(c *api.Context, statusCode int, data O, err error)
  25. // SimpleBindItem 简化的BindItem
  26. type SimpleBindItem[O any] struct {
  27. // URL相对路径
  28. Path string
  29. // 响应泛型函数,如果不响应,需要使用NoResponse零值占位
  30. ResponseFunc ResponseFunc[O]
  31. // 使用的请求参数,非必传,当请求参数为nil时,说明该接口没有参数
  32. RequestParams request.Params
  33. // 可选的请求参数绑定函数
  34. // 非必传,POST和PUT请求默认为JsonBody,DELETE和GET默认为QueryParams
  35. // 额外还提供了一些转化函数:
  36. // PathParams: 绑定路径参数
  37. // MultipartForm: 绑定multipart form
  38. // FormBody: 绑定form body
  39. // XMLBody: 绑定XML body
  40. RequestParamsBindFunc RequestParamsBindFunc
  41. // 通过请求参数构造使用的领域对象,之后在ServiceFunc中会按照构造实体的顺序进行回调
  42. // 非必传,如果为nil,则说明没有领域对象
  43. // 与Objects字段二选一使用,如果都指定,会按照该字段处理
  44. FormDomainObjectsFunc FormDomainObjectsFunc
  45. // 使用的领域对象,当使用Tag对实体进行标注后,可以直接通过该字段给定实体,之后在ServiceFunc中会按照给定实体的顺序进行回调
  46. // 非必传,如果为nil或长度为0,则说明没有领域对象
  47. // 与FormObjectsFunc字段二选一使用,如果都指定,会按照FormObjectsFunc字段处理
  48. Objects []domain.Object
  49. // 应用服务泛型函数
  50. ServiceFunc ServiceFunc[O]
  51. }
  52. func (item *SimpleBindItem[O]) bind(binder *Binder, method string, middlewares ...api.Handler) {
  53. bindingItem := &BindItem[O]{
  54. Method: method,
  55. SimpleBindItem: item,
  56. }
  57. bindingItem.bind(binder, middlewares...)
  58. }