Compare commits
	
		
			5 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5e6cb63f14 | |||
| 4832aa9d6c | |||
| 4d606d3131 | |||
| be9b9e8ccf | |||
| 28cdfc5bd2 | 
| @@ -25,6 +25,15 @@ func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func CreateBackgroundAppContext() *AppContext { | ||||
| 	return &AppContext{ | ||||
| 		inner:      context.Background(), | ||||
| 		cancelFunc: nil, | ||||
| 		cancelled:  false, | ||||
| 		GinContext: nil, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (ac *AppContext) Deadline() (deadline time.Time, ok bool) { | ||||
| 	return ac.inner.Deadline() | ||||
| } | ||||
|   | ||||
| @@ -84,7 +84,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 				WithType(exerr.TypeBindFailURI). | ||||
| 				Str("struct_type", fmt.Sprintf("%T", pctx.uri)). | ||||
| 				Build() | ||||
| 			return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "URI", err)) | ||||
| 			return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "URI", err)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -94,7 +94,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 				WithType(exerr.TypeBindFailQuery). | ||||
| 				Str("struct_type", fmt.Sprintf("%T", pctx.query)). | ||||
| 				Build() | ||||
| 			return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "QUERY", err)) | ||||
| 			return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "QUERY", err)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -108,7 +108,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 						WithType(exerr.TypeBindFailJSON). | ||||
| 						Str("struct_type", fmt.Sprintf("%T", pctx.body)). | ||||
| 						Build() | ||||
| 					return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) | ||||
| 					return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) | ||||
| 				} | ||||
| 			} | ||||
| 			if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil { | ||||
| @@ -116,14 +116,14 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 					WithType(exerr.TypeBindFailJSON). | ||||
| 					Str("struct_type", fmt.Sprintf("%T", pctx.body)). | ||||
| 					Build() | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) | ||||
| 			} | ||||
| 		} else { | ||||
| 			if !pctx.ignoreWrongContentType { | ||||
| 				err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body"). | ||||
| 					Str("struct_type", fmt.Sprintf("%T", pctx.body)). | ||||
| 					Build() | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| @@ -132,14 +132,14 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 		if brc, ok := pctx.ginCtx.Request.Body.(dataext.BufferedReadCloser); ok { | ||||
| 			v, err := brc.BufferedAll() | ||||
| 			if err != nil { | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err)) | ||||
| 			} | ||||
| 			*pctx.rawbody = v | ||||
| 		} else { | ||||
| 			buf := &bytes.Buffer{} | ||||
| 			_, err := io.Copy(buf, pctx.ginCtx.Request.Body) | ||||
| 			if err != nil { | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err)) | ||||
| 			} | ||||
| 			*pctx.rawbody = buf.Bytes() | ||||
| 		} | ||||
| @@ -152,7 +152,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 					WithType(exerr.TypeBindFailFormData). | ||||
| 					Str("struct_type", fmt.Sprintf("%T", pctx.form)). | ||||
| 					Build() | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err)) | ||||
| 			} | ||||
| 		} else if pctx.ginCtx.ContentType() == "application/x-www-form-urlencoded" { | ||||
| 			if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil { | ||||
| @@ -160,14 +160,14 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 					WithType(exerr.TypeBindFailFormData). | ||||
| 					Str("struct_type", fmt.Sprintf("%T", pctx.form)). | ||||
| 					Build() | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err)) | ||||
| 			} | ||||
| 		} else { | ||||
| 			if !pctx.ignoreWrongContentType { | ||||
| 				err := exerr.New(exerr.TypeBindFailFormData, "missing form body"). | ||||
| 					Str("struct_type", fmt.Sprintf("%T", pctx.form)). | ||||
| 					Build() | ||||
| 				return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err)) | ||||
| 				return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err)) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| @@ -178,7 +178,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 				WithType(exerr.TypeBindFailHeader). | ||||
| 				Str("struct_type", fmt.Sprintf("%T", pctx.query)). | ||||
| 				Build() | ||||
| 			return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "HEADER", err)) | ||||
| 			return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "HEADER", err)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -190,7 +190,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { | ||||
| 		err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, actx) | ||||
| 		if err != nil { | ||||
| 			actx.Cancel() | ||||
| 			return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "INIT", err)) | ||||
| 			return CreateBackgroundAppContext(), nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "INIT", err)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
							
								
								
									
										16
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								go.mod
									
									
									
									
									
								
							| @@ -9,15 +9,15 @@ require ( | ||||
| 	github.com/rs/xid v1.6.0 | ||||
| 	github.com/rs/zerolog v1.33.0 | ||||
| 	go.mongodb.org/mongo-driver v1.17.2 | ||||
| 	golang.org/x/crypto v0.32.0 | ||||
| 	golang.org/x/sys v0.29.0 | ||||
| 	golang.org/x/term v0.28.0 | ||||
| 	golang.org/x/crypto v0.33.0 | ||||
| 	golang.org/x/sys v0.30.0 | ||||
| 	golang.org/x/term v0.29.0 | ||||
| ) | ||||
|  | ||||
| require ( | ||||
| 	github.com/disintegration/imaging v1.6.2 | ||||
| 	github.com/jung-kurt/gofpdf v1.16.2 | ||||
| 	golang.org/x/sync v0.10.0 | ||||
| 	golang.org/x/sync v0.11.0 | ||||
| ) | ||||
|  | ||||
| require ( | ||||
| @@ -51,11 +51,11 @@ require ( | ||||
| 	github.com/xdg-go/scram v1.1.2 // indirect | ||||
| 	github.com/xdg-go/stringprep v1.0.4 // indirect | ||||
| 	github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect | ||||
| 	golang.org/x/arch v0.13.0 // indirect | ||||
| 	golang.org/x/image v0.23.0 // indirect | ||||
| 	golang.org/x/arch v0.14.0 // indirect | ||||
| 	golang.org/x/image v0.24.0 // indirect | ||||
| 	golang.org/x/net v0.34.0 // indirect | ||||
| 	golang.org/x/text v0.21.0 // indirect | ||||
| 	google.golang.org/protobuf v1.36.4 // indirect | ||||
| 	golang.org/x/text v0.22.0 // indirect | ||||
| 	google.golang.org/protobuf v1.36.5 // indirect | ||||
| 	gopkg.in/yaml.v3 v3.0.1 // indirect | ||||
| 	modernc.org/libc v1.37.6 // indirect | ||||
| 	modernc.org/mathutil v1.6.0 // indirect | ||||
|   | ||||
							
								
								
									
										16
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								go.sum
									
									
									
									
									
								
							| @@ -172,6 +172,8 @@ golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg= | ||||
| golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= | ||||
| golang.org/x/arch v0.13.0 h1:KCkqVVV1kGg0X87TFysjCJ8MxtZEIU4Ja/yXGeoECdA= | ||||
| golang.org/x/arch v0.13.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= | ||||
| golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4= | ||||
| golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= | ||||
| golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||||
| golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||||
| golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= | ||||
| @@ -184,6 +186,8 @@ golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= | ||||
| golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= | ||||
| golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= | ||||
| golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= | ||||
| golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= | ||||
| golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= | ||||
| golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= | ||||
| golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= | ||||
| golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s= | ||||
| @@ -192,6 +196,8 @@ golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g= | ||||
| golang.org/x/image v0.22.0/go.mod h1:9hPFhljd4zZ1GNSIZJ49sqbp45GKK9t6w+iXvGqZUz4= | ||||
| golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68= | ||||
| golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY= | ||||
| golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ= | ||||
| golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8= | ||||
| golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||||
| golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||||
| golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||||
| @@ -214,6 +220,8 @@ golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= | ||||
| golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||||
| golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= | ||||
| golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||||
| golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= | ||||
| golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||||
| golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||||
| golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||||
| golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||||
| @@ -231,6 +239,8 @@ golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= | ||||
| golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||||
| golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= | ||||
| golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||||
| golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= | ||||
| golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||||
| golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||||
| golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||||
| golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24= | ||||
| @@ -241,6 +251,8 @@ golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q= | ||||
| golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= | ||||
| golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg= | ||||
| golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= | ||||
| golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU= | ||||
| golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s= | ||||
| golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||||
| golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||||
| golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||||
| @@ -251,6 +263,8 @@ golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= | ||||
| golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= | ||||
| golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= | ||||
| golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= | ||||
| golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= | ||||
| golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= | ||||
| golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||||
| golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||||
| golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||||
| @@ -267,6 +281,8 @@ google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTm | ||||
| google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= | ||||
| google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM= | ||||
| google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= | ||||
| google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= | ||||
| google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||||
|   | ||||
| @@ -1,5 +1,5 @@ | ||||
| package goext | ||||
|  | ||||
| const GoextVersion = "0.0.559" | ||||
| const GoextVersion = "0.0.564" | ||||
|  | ||||
| const GoextVersionTimestamp = "2025-01-28T15:55:18+0100" | ||||
| const GoextVersionTimestamp = "2025-02-10T13:04:05+0100" | ||||
|   | ||||
| @@ -24,6 +24,7 @@ func Range[T IntegerConstraint](start T, end T) []T { | ||||
| 	return r | ||||
| } | ||||
|  | ||||
| // ForceArray ensures that the given array is not nil (nil will be converted to empty) | ||||
| func ForceArray[T any](v []T) []T { | ||||
| 	if v == nil { | ||||
| 		return make([]T, 0) | ||||
| @@ -47,6 +48,16 @@ func InArray[T comparable](needle T, haystack []T) bool { | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| // ArrContains checks if the value is contained in the array (same as InArray, but odther name for better findability) | ||||
| func ArrContains[T comparable](haystack []T, needle T) bool { | ||||
| 	for _, v := range haystack { | ||||
| 		if v == needle { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| func ArrUnique[T comparable](array []T) []T { | ||||
| 	m := make(map[T]bool, len(array)) | ||||
| 	for _, v := range array { | ||||
|   | ||||
| @@ -85,7 +85,7 @@ func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options. | ||||
| 	return res, nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) IterateFunc(ctx context.Context, filter bson.M, fn func(v TData) error, opts ...*options.FindOptions) error { | ||||
| func (c *Coll[TData]) FindIterateFunc(ctx context.Context, filter bson.M, fn func(v TData) error, opts ...*options.FindOptions) error { | ||||
|  | ||||
| 	cursor, err := c.createFindQuery(ctx, filter, opts...) | ||||
| 	if err != nil { | ||||
| @@ -111,11 +111,10 @@ func (c *Coll[TData]) IterateFunc(ctx context.Context, filter bson.M, fn func(v | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) Iterate(ctx context.Context, filter bson.M, opts ...*options.FindOptions) iter.Seq2[TData, error] { | ||||
|  | ||||
| func (c *Coll[TData]) FindIterate(ctx context.Context, filter bson.M, opts ...*options.FindOptions) iter.Seq2[TData, error] { | ||||
| 	cursor, err := c.createFindQuery(ctx, filter, opts...) | ||||
| 	if err != nil { | ||||
| 		return langext.IterSingleValueSeq2[TData, error](nil, exerr.Wrap(err, "").Build()) | ||||
| 		return langext.IterSingleValueSeq2[TData, error](*new(TData), exerr.Wrap(err, "").Build()) | ||||
| 	} | ||||
|  | ||||
| 	return func(yield func(TData, error) bool) { | ||||
| @@ -124,7 +123,7 @@ func (c *Coll[TData]) Iterate(ctx context.Context, filter bson.M, opts ...*optio | ||||
| 		for cursor.Next(ctx) { | ||||
| 			v, err := c.decodeSingle(ctx, cursor) | ||||
| 			if err != nil { | ||||
| 				if !yield(nil, err) { | ||||
| 				if !yield(*new(TData), err) { | ||||
| 					return | ||||
| 				} | ||||
| 				continue | ||||
|   | ||||
							
								
								
									
										345
									
								
								wmo/queryList.go
									
									
									
									
									
								
							
							
						
						
									
										345
									
								
								wmo/queryList.go
									
									
									
									
									
								
							| @@ -7,6 +7,7 @@ import ( | ||||
| 	ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken" | ||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/exerr" | ||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | ||||
| 	"iter" | ||||
| ) | ||||
|  | ||||
| func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) ([]TData, ct.CursorToken, error) { | ||||
| @@ -20,8 +21,8 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, | ||||
| 			return nil, ct.End(), err | ||||
| 		} | ||||
| 		return d, tok, nil | ||||
| 	} else if ctks, ok := inTok.(ct.CTPaginated); ok { | ||||
| 		d, tok, err := c.listWithPaginatedToken(ctx, filter, pageSize, ctks) | ||||
| 	} else if ctpag, ok := inTok.(ct.CTPaginated); ok { | ||||
| 		d, tok, err := c.listWithPaginatedToken(ctx, filter, pageSize, ctpag) | ||||
| 		if err != nil { | ||||
| 			return nil, ct.End(), err | ||||
| 		} | ||||
| @@ -31,159 +32,78 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) listWithKSToken(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CTKeySort) ([]TData, ct.CursorToken, error) { | ||||
| 	if inTok.Mode == ct.CTMEnd { | ||||
| 		return make([]TData, 0), ct.End(), nil | ||||
| 	} | ||||
| func (c *Coll[TData]) ListIterateFunc(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken, fn func(v TData) error) error { | ||||
| 	var cursor *mongo.Cursor | ||||
| 	var err error | ||||
|  | ||||
| 	if pageSize != nil && *pageSize == 0 { | ||||
| 		return make([]TData, 0), inTok, nil // fast track, we return an empty list and do not advance the cursor token | ||||
| 	} | ||||
|  | ||||
| 	pipeline := mongo.Pipeline{} | ||||
| 	pf1 := "_id" | ||||
| 	pd1 := ct.SortASC | ||||
| 	pf2 := "_id" | ||||
| 	pd2 := ct.SortASC | ||||
|  | ||||
| 	if filter != nil { | ||||
| 		pipeline = filter.FilterQuery(ctx) | ||||
| 		pf1, pd1, pf2, pd2 = filter.Pagination(ctx) | ||||
| 	} | ||||
|  | ||||
| 	sortPrimary := pf1 | ||||
| 	sortDirPrimary := pd1 | ||||
| 	sortSecondary := &pf2 | ||||
| 	sortDirSecondary := &pd2 | ||||
|  | ||||
| 	if pf1 == pf2 { | ||||
| 		sortSecondary = nil | ||||
| 		sortDirSecondary = nil | ||||
| 	} | ||||
|  | ||||
| 	paginationPipeline, doubleSortPipeline, err := createPaginationPipeline(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize) | ||||
| 	if ctks, ok := inTok.(ct.CTKeySort); ok { | ||||
| 		_, _, _, _, cursor, err = c.createKSListQuery(ctx, filter, pageSize, ctks) | ||||
| 		if err != nil { | ||||
| 		return nil, nil, exerr. | ||||
| 			Wrap(err, "failed to create pagination"). | ||||
| 			WithType(exerr.TypeCursorTokenDecode). | ||||
| 			Str("collection", c.Name()). | ||||
| 			Any("inTok", inTok). | ||||
| 			Any("sortPrimary", sortPrimary). | ||||
| 			Any("sortDirPrimary", sortDirPrimary). | ||||
| 			Any("sortSecondary", sortSecondary). | ||||
| 			Any("sortDirSecondary", sortDirSecondary). | ||||
| 			Any("pageSize", pageSize). | ||||
| 			Build() | ||||
| 			return exerr.Wrap(err, "").Build() | ||||
| 		} | ||||
|  | ||||
| 	pipeline = append(pipeline, paginationPipeline...) | ||||
|  | ||||
| 	for _, ppl := range c.extraModPipeline { | ||||
| 		pipeline = langext.ArrConcat(pipeline, ppl(ctx)) | ||||
| 	} | ||||
|  | ||||
| 	if c.needsDoubleSort(ctx) { | ||||
| 		pipeline = langext.ArrConcat(pipeline, doubleSortPipeline) | ||||
| 	} | ||||
|  | ||||
| 	cursor, err := c.coll.Aggregate(ctx, pipeline) | ||||
| 	} else if ctpag, ok := inTok.(ct.CTPaginated); ok { | ||||
| 		_, cursor, err = c.createPaginatedListQuery(ctx, filter, pageSize, ctpag) | ||||
| 		if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build() | ||||
| 			return exerr.Wrap(err, "").Build() | ||||
| 		} | ||||
| 	} else { | ||||
| 		return exerr.New(exerr.TypeCursorTokenDecode, "unknown ct type").Any("token", inTok).Type("tokenType", inTok).Build() | ||||
| 	} | ||||
|  | ||||
| 	defer func() { _ = cursor.Close(ctx) }() | ||||
|  | ||||
| 	// fast branch | ||||
| 	if pageSize == nil { | ||||
| 		entries, err := c.decodeAll(ctx, cursor) | ||||
| 	for cursor.Next(ctx) { | ||||
|  | ||||
| 		v, err := c.decodeSingle(ctx, cursor) | ||||
| 		if err != nil { | ||||
| 			return nil, nil, exerr.Wrap(err, "failed to all-decode entities").Build() | ||||
| 		} | ||||
| 		return entries, ct.End(), nil | ||||
| 			return exerr.Wrap(err, "").Build() | ||||
| 		} | ||||
|  | ||||
| 	entities := make([]TData, 0, cursor.RemainingBatchLength()) | ||||
| 	for (pageSize == nil || len(entities) != *pageSize) && cursor.Next(ctx) { | ||||
| 		var entry TData | ||||
| 		entry, err = c.decodeSingle(ctx, cursor) | ||||
| 		err = fn(v) | ||||
| 		if err != nil { | ||||
| 			return nil, nil, exerr.Wrap(err, "failed to decode entity").Build() | ||||
| 		} | ||||
| 		entities = append(entities, entry) | ||||
| 			return exerr.Wrap(err, "").Build() | ||||
| 		} | ||||
|  | ||||
| 	if pageSize == nil || len(entities) < *pageSize || !cursor.TryNext(ctx) { | ||||
| 		return entities, ct.End(), nil | ||||
| 	} | ||||
|  | ||||
| 	last := entities[len(entities)-1] | ||||
|  | ||||
| 	c.EnsureInitializedReflection(last) | ||||
|  | ||||
| 	nextToken, err := c.createToken(sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, last, pageSize) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "failed to create (out)-token").Build() | ||||
| 	} | ||||
|  | ||||
| 	return entities, nextToken, nil | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) listWithPaginatedToken(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CTPaginated) ([]TData, ct.CursorToken, error) { | ||||
| func (c *Coll[TData]) ListIterate(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) iter.Seq2[TData, error] { | ||||
| 	var cursor *mongo.Cursor | ||||
| 	var err error | ||||
|  | ||||
| 	page := inTok.Page | ||||
|  | ||||
| 	if page < 0 { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	pipelineSort := mongo.Pipeline{} | ||||
| 	pipelineFilter := mongo.Pipeline{} | ||||
|  | ||||
| 	if filter != nil { | ||||
| 		pipelineFilter = filter.FilterQuery(ctx) | ||||
| 		pf1, pd1, pf2, pd2 := filter.Pagination(ctx) | ||||
|  | ||||
| 		pipelineSort, err = createSortOnlyPipeline(pf1, pd1, &pf2, &pd2) | ||||
| 	if ctks, ok := inTok.(ct.CTKeySort); ok { | ||||
| 		_, _, _, _, cursor, err = c.createKSListQuery(ctx, filter, pageSize, ctks) | ||||
| 		if err != nil { | ||||
| 			return nil, nil, exerr.Wrap(err, "failed to create sort pipeline").Build() | ||||
| 			return langext.IterSingleValueSeq2[TData, error](*new(TData), exerr.Wrap(err, "").Build()) | ||||
| 		} | ||||
| 	} else if ctpag, ok := inTok.(ct.CTPaginated); ok { | ||||
| 		_, cursor, err = c.createPaginatedListQuery(ctx, filter, pageSize, ctpag) | ||||
| 		if err != nil { | ||||
| 			return langext.IterSingleValueSeq2[TData, error](*new(TData), exerr.Wrap(err, "").Build()) | ||||
| 		} | ||||
|  | ||||
| 	pipelinePaginate := mongo.Pipeline{} | ||||
| 	if pageSize != nil { | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$skip", Value: *pageSize * (page - 1)}}) | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$limit", Value: *pageSize}}) | ||||
| 	} else { | ||||
| 		page = 1 | ||||
| 		return langext.IterSingleValueSeq2[TData, error](*new(TData), exerr.New(exerr.TypeCursorTokenDecode, "unknown ct type").Any("token", inTok).Type("tokenType", inTok).Build()) | ||||
| 	} | ||||
|  | ||||
| 	pipelineCount := mongo.Pipeline{} | ||||
| 	pipelineCount = append(pipelineCount, bson.D{{Key: "$count", Value: "count"}}) | ||||
| 	return func(yield func(TData, error) bool) { | ||||
| 		defer func() { _ = cursor.Close(ctx) }() | ||||
|  | ||||
| 	extrModPipelineResolved := mongo.Pipeline{} | ||||
| 	for _, ppl := range c.extraModPipeline { | ||||
| 		extrModPipelineResolved = langext.ArrConcat(extrModPipelineResolved, ppl(ctx)) | ||||
| 	} | ||||
|  | ||||
| 	pipelineList := langext.ArrConcat(pipelineFilter, pipelineSort, pipelinePaginate, extrModPipelineResolved, pipelineSort) | ||||
|  | ||||
| 	cursorList, err := c.coll.Aggregate(ctx, pipelineList) | ||||
| 		for cursor.Next(ctx) { | ||||
| 			v, err := c.decodeSingle(ctx, cursor) | ||||
| 			if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineList).Str("collection", c.Name()).Build() | ||||
| 				if !yield(*new(TData), err) { | ||||
| 					return | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
|  | ||||
| 	entities, err := c.decodeAll(ctx, cursorList) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "failed to all-decode entities").Build() | ||||
| 			if !yield(v, nil) { | ||||
| 				return | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	tokOut := ct.Page(page + 1) | ||||
| 	if pageSize == nil || len(entities) < *pageSize { | ||||
| 		tokOut = ct.PageEnd() | ||||
| 	} | ||||
|  | ||||
| 	return entities, tokOut, nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) Count(ctx context.Context, filter ct.RawFilter) (int64, error) { | ||||
| @@ -291,6 +211,185 @@ func (c *Coll[TData]) ListAllIDs(ctx context.Context, filter ct.RawFilter) ([]st | ||||
| 	return langext.ArrMap(res, func(v idObject) string { return v.ID }), nil | ||||
| } | ||||
|  | ||||
| // ===================================================================================================================== | ||||
|  | ||||
| func (c *Coll[TData]) createKSListQuery(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CTKeySort) (string, ct.SortDirection, *string, *ct.SortDirection, *mongo.Cursor, error) { | ||||
| 	pipeline := mongo.Pipeline{} | ||||
| 	pf1 := "_id" | ||||
| 	pd1 := ct.SortASC | ||||
| 	pf2 := "_id" | ||||
| 	pd2 := ct.SortASC | ||||
|  | ||||
| 	if filter != nil { | ||||
| 		pipeline = filter.FilterQuery(ctx) | ||||
| 		pf1, pd1, pf2, pd2 = filter.Pagination(ctx) | ||||
| 	} | ||||
|  | ||||
| 	sortPrimary := pf1 | ||||
| 	sortDirPrimary := pd1 | ||||
| 	sortSecondary := &pf2 | ||||
| 	sortDirSecondary := &pd2 | ||||
|  | ||||
| 	if pf1 == pf2 { | ||||
| 		sortSecondary = nil | ||||
| 		sortDirSecondary = nil | ||||
| 	} | ||||
|  | ||||
| 	paginationPipeline, doubleSortPipeline, err := createPaginationPipeline(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize) | ||||
| 	if err != nil { | ||||
| 		return "", "", nil, nil, nil, exerr. | ||||
| 			Wrap(err, "failed to create pagination"). | ||||
| 			WithType(exerr.TypeCursorTokenDecode). | ||||
| 			Str("collection", c.Name()). | ||||
| 			Any("inTok", inTok). | ||||
| 			Any("sortPrimary", sortPrimary). | ||||
| 			Any("sortDirPrimary", sortDirPrimary). | ||||
| 			Any("sortSecondary", sortSecondary). | ||||
| 			Any("sortDirSecondary", sortDirSecondary). | ||||
| 			Any("pageSize", pageSize). | ||||
| 			Build() | ||||
| 	} | ||||
|  | ||||
| 	pipeline = append(pipeline, paginationPipeline...) | ||||
|  | ||||
| 	for _, ppl := range c.extraModPipeline { | ||||
| 		pipeline = langext.ArrConcat(pipeline, ppl(ctx)) | ||||
| 	} | ||||
|  | ||||
| 	if c.needsDoubleSort(ctx) { | ||||
| 		pipeline = langext.ArrConcat(pipeline, doubleSortPipeline) | ||||
| 	} | ||||
|  | ||||
| 	cursor, err := c.coll.Aggregate(ctx, pipeline) | ||||
| 	if err != nil { | ||||
| 		return "", "", nil, nil, nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build() | ||||
| 	} | ||||
|  | ||||
| 	return sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, cursor, nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) createPaginatedListQuery(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CTPaginated) (int, *mongo.Cursor, error) { | ||||
| 	var err error | ||||
|  | ||||
| 	page := inTok.Page | ||||
|  | ||||
| 	pipelineSort := mongo.Pipeline{} | ||||
| 	pipelineFilter := mongo.Pipeline{} | ||||
|  | ||||
| 	if filter != nil { | ||||
| 		pipelineFilter = filter.FilterQuery(ctx) | ||||
| 		pf1, pd1, pf2, pd2 := filter.Pagination(ctx) | ||||
|  | ||||
| 		pipelineSort, err = createSortOnlyPipeline(pf1, pd1, &pf2, &pd2) | ||||
| 		if err != nil { | ||||
| 			return 0, nil, exerr.Wrap(err, "failed to create sort pipeline").Build() | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	pipelinePaginate := mongo.Pipeline{} | ||||
| 	if pageSize != nil { | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$skip", Value: *pageSize * (page - 1)}}) | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$limit", Value: *pageSize}}) | ||||
| 	} else { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	pipelineCount := mongo.Pipeline{} | ||||
| 	pipelineCount = append(pipelineCount, bson.D{{Key: "$count", Value: "count"}}) | ||||
|  | ||||
| 	extrModPipelineResolved := mongo.Pipeline{} | ||||
| 	for _, ppl := range c.extraModPipeline { | ||||
| 		extrModPipelineResolved = langext.ArrConcat(extrModPipelineResolved, ppl(ctx)) | ||||
| 	} | ||||
|  | ||||
| 	pipelineList := langext.ArrConcat(pipelineFilter, pipelineSort, pipelinePaginate, extrModPipelineResolved, pipelineSort) | ||||
|  | ||||
| 	cursorList, err := c.coll.Aggregate(ctx, pipelineList) | ||||
| 	if err != nil { | ||||
| 		return 0, nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineList).Str("collection", c.Name()).Build() | ||||
| 	} | ||||
|  | ||||
| 	return page, cursorList, nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) listWithKSToken(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CTKeySort) ([]TData, ct.CursorToken, error) { | ||||
| 	if inTok.Mode == ct.CTMEnd { | ||||
| 		return make([]TData, 0), ct.End(), nil | ||||
| 	} | ||||
|  | ||||
| 	if pageSize != nil && *pageSize == 0 { | ||||
| 		return make([]TData, 0), inTok, nil // fast track, we return an empty list and do not advance the cursor token | ||||
| 	} | ||||
|  | ||||
| 	sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, cursor, err := c.createKSListQuery(ctx, filter, pageSize, inTok) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "").Build() | ||||
| 	} | ||||
|  | ||||
| 	defer func() { _ = cursor.Close(ctx) }() | ||||
|  | ||||
| 	// fast branch | ||||
| 	if pageSize == nil { | ||||
| 		entries, err := c.decodeAll(ctx, cursor) | ||||
| 		if err != nil { | ||||
| 			return nil, nil, exerr.Wrap(err, "failed to all-decode entities").Build() | ||||
| 		} | ||||
| 		return entries, ct.End(), nil | ||||
| 	} | ||||
|  | ||||
| 	entities := make([]TData, 0, cursor.RemainingBatchLength()) | ||||
| 	for (pageSize == nil || len(entities) != *pageSize) && cursor.Next(ctx) { | ||||
| 		var entry TData | ||||
| 		entry, err = c.decodeSingle(ctx, cursor) | ||||
| 		if err != nil { | ||||
| 			return nil, nil, exerr.Wrap(err, "failed to decode entity").Build() | ||||
| 		} | ||||
| 		entities = append(entities, entry) | ||||
| 	} | ||||
|  | ||||
| 	if pageSize == nil || len(entities) < *pageSize || !cursor.TryNext(ctx) { | ||||
| 		return entities, ct.End(), nil | ||||
| 	} | ||||
|  | ||||
| 	last := entities[len(entities)-1] | ||||
|  | ||||
| 	c.EnsureInitializedReflection(last) | ||||
|  | ||||
| 	nextToken, err := c.createToken(sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, last, pageSize) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "failed to create (out)-token").Build() | ||||
| 	} | ||||
|  | ||||
| 	return entities, nextToken, nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) listWithPaginatedToken(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CTPaginated) ([]TData, ct.CursorToken, error) { | ||||
| 	var err error | ||||
|  | ||||
| 	page := inTok.Page | ||||
|  | ||||
| 	if page < 0 { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	page, cursorList, err := c.createPaginatedListQuery(ctx, filter, pageSize, inTok) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "").Build() | ||||
| 	} | ||||
|  | ||||
| 	entities, err := c.decodeAll(ctx, cursorList) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, exerr.Wrap(err, "failed to all-decode entities").Build() | ||||
| 	} | ||||
|  | ||||
| 	tokOut := ct.Page(page + 1) | ||||
| 	if pageSize == nil || len(entities) < *pageSize { | ||||
| 		tokOut = ct.PageEnd() | ||||
| 	} | ||||
|  | ||||
| 	return entities, tokOut, nil | ||||
| } | ||||
|  | ||||
| func createPaginationPipeline[TData any](coll *Coll[TData], token ct.CTKeySort, fieldPrimary string, sortPrimary ct.SortDirection, fieldSecondary *string, sortSecondary *ct.SortDirection, pageSize *int) ([]bson.D, []bson.D, error) { | ||||
|  | ||||
| 	cond := bson.A{} | ||||
|   | ||||
| @@ -7,54 +7,19 @@ import ( | ||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/exerr" | ||||
| 	"gogs.mikescher.com/BlackForestBytes/goext/langext" | ||||
| 	pag "gogs.mikescher.com/BlackForestBytes/goext/pagination" | ||||
| 	"iter" | ||||
| ) | ||||
|  | ||||
| func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page int, limit *int) ([]TData, pag.Pagination, error) { | ||||
| 	page, cursorList, pipelineTotalCount, err := c.createPaginatedQuery(ctx, filter, page, limit) | ||||
| 	if err != nil { | ||||
| 		return nil, pag.Pagination{}, exerr.Wrap(err, "").Build() | ||||
| 	} | ||||
|  | ||||
| 	type totalCountResult struct { | ||||
| 		Count int `bson:"count"` | ||||
| 	} | ||||
|  | ||||
| 	if page < 0 { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	pipelineSort := mongo.Pipeline{} | ||||
| 	pipelineFilter := mongo.Pipeline{} | ||||
| 	sort := bson.D{} | ||||
|  | ||||
| 	if filter != nil { | ||||
| 		pipelineFilter = filter.FilterQuery(ctx) | ||||
| 		sort = filter.Sort(ctx) | ||||
| 	} | ||||
|  | ||||
| 	if len(sort) != 0 { | ||||
| 		pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: sort}}) | ||||
| 	} | ||||
|  | ||||
| 	pipelinePaginate := mongo.Pipeline{} | ||||
| 	if limit != nil { | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$skip", Value: *limit * (page - 1)}}) | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$limit", Value: *limit}}) | ||||
| 	} else { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	pipelineCount := mongo.Pipeline{} | ||||
| 	pipelineCount = append(pipelineCount, bson.D{{Key: "$count", Value: "count"}}) | ||||
|  | ||||
| 	extrModPipelineResolved := mongo.Pipeline{} | ||||
| 	for _, ppl := range c.extraModPipeline { | ||||
| 		extrModPipelineResolved = langext.ArrConcat(extrModPipelineResolved, ppl(ctx)) | ||||
| 	} | ||||
|  | ||||
| 	pipelineList := langext.ArrConcat(pipelineFilter, pipelineSort, pipelinePaginate, extrModPipelineResolved, pipelineSort) | ||||
| 	pipelineTotalCount := langext.ArrConcat(pipelineFilter, pipelineCount) | ||||
|  | ||||
| 	cursorList, err := c.coll.Aggregate(ctx, pipelineList) | ||||
| 	if err != nil { | ||||
| 		return nil, pag.Pagination{}, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineList).Str("collection", c.Name()).Build() | ||||
| 	} | ||||
|  | ||||
| 	entities, err := c.decodeAll(ctx, cursorList) | ||||
| 	if err != nil { | ||||
| 		return nil, pag.Pagination{}, exerr.Wrap(err, "failed to all-decode entities").Build() | ||||
| @@ -93,3 +58,100 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page | ||||
|  | ||||
| 	return entities, paginationObj, nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) PaginateIterateFunc(ctx context.Context, filter pag.MongoFilter, page int, limit *int, fn func(v TData) error) error { | ||||
| 	page, cursor, _, err := c.createPaginatedQuery(ctx, filter, page, limit) | ||||
| 	if err != nil { | ||||
| 		return exerr.Wrap(err, "").Build() | ||||
| 	} | ||||
|  | ||||
| 	defer func() { _ = cursor.Close(ctx) }() | ||||
|  | ||||
| 	for cursor.Next(ctx) { | ||||
|  | ||||
| 		v, err := c.decodeSingle(ctx, cursor) | ||||
| 		if err != nil { | ||||
| 			return exerr.Wrap(err, "").Build() | ||||
| 		} | ||||
|  | ||||
| 		err = fn(v) | ||||
| 		if err != nil { | ||||
| 			return exerr.Wrap(err, "").Build() | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (c *Coll[TData]) PaginateIterate(ctx context.Context, filter pag.MongoFilter, page int, limit *int) iter.Seq2[TData, error] { | ||||
| 	page, cursor, _, err := c.createPaginatedQuery(ctx, filter, page, limit) | ||||
| 	if err != nil { | ||||
| 		return langext.IterSingleValueSeq2[TData, error](*new(TData), exerr.Wrap(err, "").Build()) | ||||
| 	} | ||||
|  | ||||
| 	return func(yield func(TData, error) bool) { | ||||
| 		defer func() { _ = cursor.Close(ctx) }() | ||||
|  | ||||
| 		for cursor.Next(ctx) { | ||||
| 			v, err := c.decodeSingle(ctx, cursor) | ||||
| 			if err != nil { | ||||
| 				if !yield(*new(TData), err) { | ||||
| 					return | ||||
| 				} | ||||
| 				continue | ||||
| 			} | ||||
|  | ||||
| 			if !yield(v, nil) { | ||||
| 				return | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // ===================================================================================================================== | ||||
|  | ||||
| func (c *Coll[TData]) createPaginatedQuery(ctx context.Context, filter pag.MongoFilter, page int, limit *int) (int, *mongo.Cursor, mongo.Pipeline, error) { | ||||
| 	if page < 0 { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	pipelineSort := mongo.Pipeline{} | ||||
| 	pipelineFilter := mongo.Pipeline{} | ||||
| 	sort := bson.D{} | ||||
|  | ||||
| 	if filter != nil { | ||||
| 		pipelineFilter = filter.FilterQuery(ctx) | ||||
| 		sort = filter.Sort(ctx) | ||||
| 	} | ||||
|  | ||||
| 	if len(sort) != 0 { | ||||
| 		pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: sort}}) | ||||
| 	} | ||||
|  | ||||
| 	pipelinePaginate := mongo.Pipeline{} | ||||
| 	if limit != nil { | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$skip", Value: *limit * (page - 1)}}) | ||||
| 		pipelinePaginate = append(pipelinePaginate, bson.D{{Key: "$limit", Value: *limit}}) | ||||
| 	} else { | ||||
| 		page = 1 | ||||
| 	} | ||||
|  | ||||
| 	pipelineCount := mongo.Pipeline{} | ||||
| 	pipelineCount = append(pipelineCount, bson.D{{Key: "$count", Value: "count"}}) | ||||
|  | ||||
| 	extrModPipelineResolved := mongo.Pipeline{} | ||||
| 	for _, ppl := range c.extraModPipeline { | ||||
| 		extrModPipelineResolved = langext.ArrConcat(extrModPipelineResolved, ppl(ctx)) | ||||
| 	} | ||||
|  | ||||
| 	pipelineList := langext.ArrConcat(pipelineFilter, pipelineSort, pipelinePaginate, extrModPipelineResolved, pipelineSort) | ||||
| 	pipelineTotalCount := langext.ArrConcat(pipelineFilter, pipelineCount) | ||||
|  | ||||
| 	cursorList, err := c.coll.Aggregate(ctx, pipelineList) | ||||
| 	if err != nil { | ||||
| 		return 0, nil, nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipelineList).Str("collection", c.Name()).Build() | ||||
| 	} | ||||
|  | ||||
| 	return page, cursorList, pipelineTotalCount, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user