simple_bind_item.go 2.8 KB

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