simple_bind_item.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package binding
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/binding/request"
  5. "git.sxidc.com/go-framework/baize/domain"
  6. "git.sxidc.com/go-framework/baize/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默认为PathParams,GET默认为QueryParams
  35. // 额外还提供了一些转化函数:
  36. // MultipartForm: 绑定multipart form
  37. // FormBody: 绑定form body
  38. // XMLBody: 绑定XML body
  39. RequestParamsBindFunc RequestParamsBindFunc
  40. // 通过请求参数构造使用的领域对象,之后在ServiceFunc中会按照构造实体的顺序进行回调
  41. // 非必传,如果为nil,则说明没有领域对象
  42. // 与Objects字段二选一使用,如果都指定,会按照该字段处理
  43. FormDomainObjectsFunc FormDomainObjectsFunc
  44. // 使用的领域对象,当使用Tag对实体进行标注后,可以直接通过该字段给定实体,之后在ServiceFunc中会按照给定实体的顺序进行回调
  45. // 非必传,如果为nil或长度为0,则说明没有领域对象
  46. // 与FormObjectsFunc字段二选一使用,如果都指定,会按照FormObjectsFunc字段处理
  47. Objects []domain.Object
  48. // 基础设施实例,可以通过Application取到
  49. Infrastructure *infrastructure.Infrastructure
  50. // 应用服务泛型函数
  51. ServiceFunc ServiceFunc[O]
  52. }
  53. func (item *SimpleBindItem[O]) bind(binder *Binder, method string, middlewares ...api.Handler) {
  54. bindingItem := &BindItem[O]{
  55. Method: method,
  56. SimpleBindItem: item,
  57. }
  58. bindingItem.bind(binder, middlewares...)
  59. }