소스 검색

调整中间件传参

yjp 1 년 전
부모
커밋
9c049b2174

+ 23 - 6
convenient/entity_crud/simple.go

@@ -33,6 +33,7 @@ type Simple[I any] struct {
 	GetByIDQueryParams request.IDRequestParam
 
 	// 可选配置项,通过WithXXX配置
+	globalOptions  *GlobalOptions
 	createOptions  *CreateOptions
 	deleteOptions  *DeleteOptions
 	updateOptions  *UpdateOptions
@@ -40,7 +41,8 @@ type Simple[I any] struct {
 	getByIDOptions *GetByIDOptions[I]
 }
 
-func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
+func (simple *Simple[I]) bind(binder *binding.Binder) {
+	globalOptions := simple.globalOptions
 	createOptions := simple.createOptions
 	deleteOptions := simple.deleteOptions
 	updateOptions := simple.updateOptions
@@ -52,7 +54,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 创建
 	if !createOptions.disable {
-		createMiddlewares := append(middlewares, createOptions.middlewares...)
+		createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
 
 		if !createOptions.needTx {
 			binding.PostBind[string](binder, &binding.SimpleBindItem[string]{
@@ -75,7 +77,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 删除
 	if !deleteOptions.disable {
-		deleteMiddlewares := append(middlewares, deleteOptions.middlewares...)
+		deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
 
 		if !deleteOptions.needTx {
 			binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
@@ -98,7 +100,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 修改
 	if !updateOptions.disable {
-		updateMiddlewares := append(middlewares, updateOptions.middlewares...)
+		updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
 
 		if !updateOptions.needTx {
 			binding.PutBind(binder, &binding.SimpleBindItem[any]{
@@ -121,7 +123,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 查询
 	if !queryOptions.disable {
-		queryMiddlewares := append(middlewares, queryOptions.middlewares...)
+		queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
 
 		binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
 			Path:             domainPath + "/query",
@@ -134,7 +136,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 通过ID获取
 	if !getByIDOptions.disable {
-		getByIDMiddlewares := append(middlewares, getByIDOptions.middlewares...)
+		getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
 
 		binding.GetBind(binder, &binding.SimpleBindItem[I]{
 			Path:             domainPath + "/get",
@@ -147,6 +149,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 }
 
 func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
+	globalOptions := new(GlobalOptions)
 	createOptions := new(CreateOptions)
 	deleteOptions := new(DeleteOptions)
 	updateOptions := new(UpdateOptions)
@@ -155,6 +158,8 @@ func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
 
 	for _, opt := range opts {
 		switch o := opt.(type) {
+		case GlobalOption:
+			o(globalOptions)
 		case CreateOption:
 			o(createOptions)
 		case DeleteOption:
@@ -170,6 +175,7 @@ func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
 		}
 	}
 
+	simple.globalOptions = globalOptions
 	simple.createOptions = createOptions
 	simple.deleteOptions = deleteOptions
 	simple.updateOptions = updateOptions
@@ -179,12 +185,17 @@ func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
 	simple.bind(binder)
 }
 
+type GlobalOption func(options *GlobalOptions)
 type CreateOption func(options *CreateOptions)
 type DeleteOption func(options *DeleteOptions)
 type UpdateOption func(options *UpdateOptions)
 type QueryOption[I any] func(options *QueryOptions[I])
 type GetByIDOption[I any] func(options *GetByIDOptions[I])
 
+type GlobalOptions struct {
+	middlewares []binding.Middleware
+}
+
 type CreateOptions struct {
 	// 关闭创建
 	disable bool
@@ -252,6 +263,12 @@ type GetByIDOptions[I any] struct {
 	middlewares []binding.Middleware
 }
 
+func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption {
+	return func(options *GlobalOptions) {
+		options.middlewares = middlewares
+	}
+}
+
 func WithDisableCreate() CreateOption {
 	return func(options *CreateOptions) {
 		options.disable = true

+ 14 - 5
convenient/relation/many2many/simple.go

@@ -38,7 +38,7 @@ type Simple[LI any, RI any] struct {
 	options *Options
 }
 
-func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
+func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 	options := simple.options
 
 	leftDomainPath := domain.RelativeDomainPath(simple.Left)
@@ -55,7 +55,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableLeft {
 		if !options.disableLeftUpdate {
-			leftUpdateMiddlewares := append(middlewares, options.leftUpdateMiddlewares...)
+			leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
 
 			// 左到右更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -70,7 +70,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableLeftQuery {
-			leftQueryMiddlewares := append(middlewares, options.leftQueryMiddlewares...)
+			leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
 
 			// 左到右查询
 			binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
@@ -87,7 +87,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableRight {
 		if !options.disableRightUpdate {
-			rightUpdateMiddlewares := append(middlewares, options.rightUpdateMiddlewares...)
+			rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
 
 			// 右到左更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -102,7 +102,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableRightQuery {
-			rightQueryMiddlewares := append(middlewares, options.rightQueryMiddlewares...)
+			rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
 
 			// 右到左查询
 			binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[LI]]{
@@ -151,6 +151,9 @@ type Options struct {
 	// 关闭右侧查询
 	disableRightQuery bool
 
+	// 全局中间件
+	globalMiddlewares []binding.Middleware
+
 	// 左侧更新中间件
 	leftUpdateMiddlewares []binding.Middleware
 
@@ -164,6 +167,12 @@ type Options struct {
 	rightQueryMiddlewares []binding.Middleware
 }
 
+func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
+	return func(options *Options) {
+		options.globalMiddlewares = middlewares
+	}
+}
+
 func WithDisableLeft() Option {
 	return func(options *Options) {
 		options.disableLeft = true

+ 15 - 6
convenient/relation/one2many/simple.go

@@ -41,7 +41,7 @@ type Simple[LI any, RI any] struct {
 	options *Options
 }
 
-func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
+func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 	options := simple.options
 
 	leftDomainPath := domain.RelativeDomainPath(simple.Left)
@@ -56,7 +56,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableLeft {
 		if !options.disableLeftUpdate {
-			leftUpdateMiddlewares := append(middlewares, options.leftUpdateMiddlewares...)
+			leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
 
 			// 左到右更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -70,7 +70,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableLeftQuery {
-			leftQueryMiddlewares := append(middlewares, options.leftQueryMiddlewares...)
+			leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
 
 			// 左到右查询
 			binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
@@ -85,7 +85,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableRight {
 		if !options.disableRightUpdate {
-			rightUpdateMiddlewares := append(middlewares, options.rightUpdateMiddlewares...)
+			rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
 
 			// 右到左更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -99,7 +99,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableRightQuery {
-			rightQueryMiddlewares := append(middlewares, options.rightQueryMiddlewares...)
+			rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
 
 			// 右到左查询
 			binding.GetBind(binder, &binding.SimpleBindItem[LI]{
@@ -112,7 +112,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableRightWithLeftQuery {
-			rightWithLeftQueryMiddlewares := append(middlewares, options.rightWithLeftQueryMiddlewares...)
+			rightWithLeftQueryMiddlewares := append(options.globalMiddlewares, options.rightWithLeftQueryMiddlewares...)
 
 			// 右到左查询,携带左方信息
 			binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
@@ -165,6 +165,9 @@ type Options struct {
 	// 右查询条件构造回调
 	rightQueryWithConditionFieldCallback ConditionFieldCallback
 
+	// 全局中间件
+	globalMiddlewares []binding.Middleware
+
 	// 左侧更新中间件
 	leftUpdateMiddlewares []binding.Middleware
 
@@ -181,6 +184,12 @@ type Options struct {
 	rightWithLeftQueryMiddlewares []binding.Middleware
 }
 
+func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
+	return func(options *Options) {
+		options.globalMiddlewares = middlewares
+	}
+}
+
 func WithDisableLeft() Option {
 	return func(options *Options) {
 		options.disableLeft = true

+ 16 - 7
convenient/relation/one2one/simple.go

@@ -44,7 +44,7 @@ type Simple[LI any, RI any] struct {
 	options *Options
 }
 
-func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
+func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 	options := simple.options
 
 	leftDomainPath := domain.RelativeDomainPath(simple.Left)
@@ -60,7 +60,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableLeft {
 		if !options.disableLeftUpdate {
-			leftUpdateMiddlewares := append(middlewares, options.leftUpdateMiddlewares...)
+			leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
 
 			// 左到右更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -74,7 +74,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableLeftQuery {
-			leftQueryMiddlewares := append(middlewares, options.leftQueryMiddlewares...)
+			leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
 
 			// 左到右查询
 			binding.GetBind(binder, &binding.SimpleBindItem[RI]{
@@ -87,7 +87,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableLeftWithRightQuery {
-			leftWithRightQueryMiddlewares := append(middlewares, options.leftWithRightQueryMiddlewares...)
+			leftWithRightQueryMiddlewares := append(options.globalMiddlewares, options.leftWithRightQueryMiddlewares...)
 
 			// 左到右查询,携带右方信息
 			binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
@@ -102,7 +102,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableRight {
 		if !options.disableRightUpdate {
-			rightUpdateMiddlewares := append(middlewares, options.rightUpdateMiddlewares...)
+			rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
 
 			// 右到左更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -116,7 +116,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableRightQuery {
-			rightQueryMiddlewares := append(middlewares, options.rightQueryMiddlewares...)
+			rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
 
 			// 右到左查询
 			binding.GetBind(binder, &binding.SimpleBindItem[LI]{
@@ -129,7 +129,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableRightWithLeftQuery {
-			rightWithLeftQueryMiddlewares := append(middlewares, options.rightWithLeftQueryMiddlewares...)
+			rightWithLeftQueryMiddlewares := append(options.globalMiddlewares, options.rightWithLeftQueryMiddlewares...)
 
 			// 右到左查询,携带左方信息
 			binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
@@ -188,6 +188,9 @@ type Options struct {
 	// 右查询条件构造回调
 	rightQueryWithConditionFieldCallback ConditionFieldCallback
 
+	// 全局中间件
+	globalMiddlewares []binding.Middleware
+
 	// 左侧更新中间件
 	leftUpdateMiddlewares []binding.Middleware
 
@@ -207,6 +210,12 @@ type Options struct {
 	rightWithLeftQueryMiddlewares []binding.Middleware
 }
 
+func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
+	return func(options *Options) {
+		options.globalMiddlewares = middlewares
+	}
+}
+
 func WithDisableLeft() Option {
 	return func(options *Options) {
 		options.disableLeft = true

+ 14 - 5
convenient/relation/remote/simple.go

@@ -38,7 +38,7 @@ type Simple[LI any, RI any] struct {
 	options *Options
 }
 
-func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
+func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
 	options := simple.options
 
 	leftDomainPath := domain.RelativeDomainPath(simple.Left)
@@ -65,7 +65,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableLeft {
 		if !options.disableLeftUpdate {
-			leftUpdateMiddlewares := append(middlewares, options.leftUpdateMiddlewares...)
+			leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
 
 			// 左到右更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -80,7 +80,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableLeftQuery {
-			leftQueryMiddlewares := append(middlewares, options.leftQueryMiddlewares...)
+			leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
 
 			// 左到右查询
 			if rightRemote {
@@ -107,7 +107,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 
 	if !options.disableRight {
 		if !options.disableRightUpdate {
-			rightUpdateMiddlewares := append(middlewares, options.rightUpdateMiddlewares...)
+			rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
 
 			// 右到左更新
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -122,7 +122,7 @@ func (simple *Simple[LI, RI]) bind(binder *binding.Binder, middlewares ...bindin
 		}
 
 		if !options.disableRightQuery {
-			rightQueryMiddlewares := append(middlewares, options.rightQueryMiddlewares...)
+			rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
 
 			// 右到左查询
 			if leftRemote {
@@ -181,6 +181,9 @@ type Options struct {
 	// 关闭右侧查询
 	disableRightQuery bool
 
+	// 全局中间件
+	globalMiddlewares []binding.Middleware
+
 	// 左侧更新中间件
 	leftUpdateMiddlewares []binding.Middleware
 
@@ -194,6 +197,12 @@ type Options struct {
 	rightQueryMiddlewares []binding.Middleware
 }
 
+func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
+	return func(options *Options) {
+		options.globalMiddlewares = middlewares
+	}
+}
+
 func WithDisableLeft() Option {
 	return func(options *Options) {
 		options.disableLeft = true

+ 21 - 4
convenient/value_object_crud/simple.go

@@ -27,12 +27,14 @@ type Simple[I any] struct {
 	QueryQueryParams request.QueryRequestParams
 
 	// 可选配置项,通过WithXXX配置
+	globalOptions *GlobalOptions
 	creatOptions  *CreateOptions
 	deleteOptions *DeleteOptions
 	queryOptions  *QueryOptions[I]
 }
 
-func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
+func (simple *Simple[I]) bind(binder *binding.Binder) {
+	globalOptions := simple.globalOptions
 	createOptions := simple.creatOptions
 	deleteOptions := simple.deleteOptions
 	queryOptions := simple.queryOptions
@@ -42,7 +44,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 创建
 	if !createOptions.disable {
-		createMiddlewares := append(middlewares, createOptions.middlewares...)
+		createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
 
 		if !createOptions.needTx {
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -65,7 +67,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 删除
 	if !deleteOptions.disable {
-		deleteMiddlewares := append(middlewares, deleteOptions.middlewares...)
+		deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
 
 		if !deleteOptions.needTx {
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -90,7 +92,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 
 	// 查询
 	if !queryOptions.disable {
-		queryMiddlewares := append(middlewares, queryOptions.middlewares...)
+		queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
 
 		binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
 			Path:             domainPath + "/query",
@@ -103,12 +105,15 @@ func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Mid
 }
 
 func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
+	globalOptions := new(GlobalOptions)
 	createOptions := new(CreateOptions)
 	deleteOptions := new(DeleteOptions)
 	queryOptions := new(QueryOptions[I])
 
 	for _, opt := range opts {
 		switch o := opt.(type) {
+		case GlobalOption:
+			o(globalOptions)
 		case CreateOption:
 			o(createOptions)
 		case DeleteOption:
@@ -120,6 +125,7 @@ func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
 		}
 	}
 
+	simple.globalOptions = globalOptions
 	simple.creatOptions = createOptions
 	simple.deleteOptions = deleteOptions
 	simple.queryOptions = queryOptions
@@ -127,10 +133,15 @@ func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
 	simple.bind(binder)
 }
 
+type GlobalOption func(options *GlobalOptions)
 type CreateOption func(options *CreateOptions)
 type DeleteOption func(options *DeleteOptions)
 type QueryOption[I any] func(options *QueryOptions[I])
 
+type GlobalOptions struct {
+	middlewares []binding.Middleware
+}
+
 type CreateOptions struct {
 	// 关闭创建
 	disable bool
@@ -173,6 +184,12 @@ type QueryOptions[I any] struct {
 	middlewares []binding.Middleware
 }
 
+func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption {
+	return func(options *GlobalOptions) {
+		options.middlewares = middlewares
+	}
+}
+
 func WithDisableCreate() CreateOption {
 	return func(options *CreateOptions) {
 		options.disable = true