Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
164c462b96
|
|||
5e6cb63f14
|
|||
4832aa9d6c
|
|||
4d606d3131
|
|||
be9b9e8ccf
|
|||
28cdfc5bd2
|
|||
10a6627323
|
@@ -107,6 +107,16 @@ func (b *Builder) WithMessage(msg string) *Builder {
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) WithSeverity(v ErrorSeverity) *Builder {
|
||||
b.errorData.Severity = v
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) WithCategory(v ErrorCategory) *Builder {
|
||||
b.errorData.Category = v
|
||||
return b
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Err changes the Severity to ERROR (default)
|
||||
|
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
38
go.mod
38
go.mod
@@ -1,6 +1,8 @@
|
||||
module gogs.mikescher.com/BlackForestBytes/goext
|
||||
|
||||
go 1.23
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.0
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
@@ -8,35 +10,35 @@ require (
|
||||
github.com/jmoiron/sqlx v1.4.0
|
||||
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
|
||||
go.mongodb.org/mongo-driver v1.17.3
|
||||
golang.org/x/crypto v0.35.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 (
|
||||
github.com/bytedance/sonic v1.12.7 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.2 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/bytedance/sonic v1.12.9 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.3 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.0.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.23.0 // indirect
|
||||
github.com/goccy/go-json v0.10.4 // indirect
|
||||
github.com/go-playground/validator/v10 v10.25.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/uuid v1.5.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@@ -51,11 +53,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/net v0.34.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.36.2 // indirect
|
||||
golang.org/x/arch v0.14.0 // indirect
|
||||
golang.org/x/image v0.24.0 // indirect
|
||||
golang.org/x/net v0.35.0 // 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
|
||||
|
42
go.sum
42
go.sum
@@ -11,6 +11,10 @@ github.com/bytedance/sonic v1.12.6 h1:/isNmCUF2x3Sh8RAp/4mh4ZGkcFAX/hLrzrK3AvpRz
|
||||
github.com/bytedance/sonic v1.12.6/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
|
||||
github.com/bytedance/sonic v1.12.7 h1:CQU8pxOy9HToxhndH0Kx/S1qU/CuS9GnKYrGioDcU1Q=
|
||||
github.com/bytedance/sonic v1.12.7/go.mod h1:tnbal4mxOMju17EGfknm2XyYcpyCnIROYOEYuemj13I=
|
||||
github.com/bytedance/sonic v1.12.8 h1:4xYRVRlXIgvSZ4e8iVTlMF5szgpXd4AfvuWgA8I8lgs=
|
||||
github.com/bytedance/sonic v1.12.8/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
|
||||
github.com/bytedance/sonic v1.12.9 h1:Od1BvK55NnewtGaJsTDeAOSnLVO2BTSLOe0+ooKokmQ=
|
||||
github.com/bytedance/sonic v1.12.9/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM=
|
||||
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
@@ -18,8 +22,12 @@ github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iC
|
||||
github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.2 h1:jxAJuN9fOot/cyz5Q6dUuMJF5OqQ6+5GfA8FjjQ0R4o=
|
||||
github.com/bytedance/sonic/loader v0.2.2/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0=
|
||||
github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
@@ -56,12 +64,18 @@ github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27
|
||||
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
|
||||
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
|
||||
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||
github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8=
|
||||
github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
||||
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
@@ -83,11 +97,15 @@ github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/N
|
||||
github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
@@ -156,12 +174,16 @@ go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHy
|
||||
go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4=
|
||||
go.mongodb.org/mongo-driver v1.17.2 h1:gvZyk8352qSfzyZ2UMWcpDpMSGEr1eqE4T793SqyhzM=
|
||||
go.mongodb.org/mongo-driver v1.17.2/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
|
||||
go.mongodb.org/mongo-driver v1.17.3 h1:TQyXhnsWfWtgAhMtOgtYHMTkZIfBTpMTsMnd9ZBeHxQ=
|
||||
go.mongodb.org/mongo-driver v1.17.3/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
|
||||
golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4=
|
||||
golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
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=
|
||||
@@ -174,6 +196,10 @@ 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/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
|
||||
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
|
||||
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=
|
||||
@@ -182,6 +208,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=
|
||||
@@ -196,6 +224,8 @@ golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
@@ -204,6 +234,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=
|
||||
@@ -221,6 +253,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=
|
||||
@@ -231,6 +265,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=
|
||||
@@ -241,6 +277,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=
|
||||
@@ -255,6 +293,10 @@ google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/g
|
||||
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
|
||||
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.558"
|
||||
const GoextVersion = "0.0.565"
|
||||
|
||||
const GoextVersionTimestamp = "2025-01-10T15:36:23+0100"
|
||||
const GoextVersionTimestamp = "2025-02-28T21:43:36+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 {
|
||||
|
21
langext/iter.go
Normal file
21
langext/iter.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"iter"
|
||||
)
|
||||
|
||||
func IterSingleValueSeq[T any](value T) iter.Seq[T] {
|
||||
return func(yield func(T) bool) {
|
||||
if !yield(value) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IterSingleValueSeq2[T1 any, T2 any](v1 T1, v2 T2) iter.Seq2[T1, T2] {
|
||||
return func(yield func(T1, T2) bool) {
|
||||
if !yield(v1, v2) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,9 +7,10 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"iter"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
|
||||
func (c *Coll[TData]) createFindQuery(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (*mongo.Cursor, error) {
|
||||
|
||||
pipeline := mongo.Pipeline{}
|
||||
pipeline = append(pipeline, bson.D{{Key: "$match", Value: filter}})
|
||||
@@ -64,6 +65,16 @@ func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.
|
||||
return nil, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build()
|
||||
}
|
||||
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
|
||||
|
||||
cursor, err := c.createFindQuery(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
return nil, exerr.Wrap(err, "").Build()
|
||||
}
|
||||
|
||||
defer func() { _ = cursor.Close(ctx) }()
|
||||
|
||||
res, err := c.decodeAll(ctx, cursor)
|
||||
@@ -74,6 +85,57 @@ func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.
|
||||
return res, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
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]) 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](*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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// converts FindOptions to AggregateOptions
|
||||
func convertFindOpt(v *options.FindOptions) (*options.AggregateOptions, error) {
|
||||
if v == nil {
|
||||
|
365
wmo/queryList.go
365
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 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()
|
||||
}
|
||||
|
||||
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, exerr.Wrap(err, "mongo-aggregation failed").Any("pipeline", pipeline).Str("collection", c.Name()).Build()
|
||||
if ctks, ok := inTok.(ct.CTKeySort); ok {
|
||||
_, _, _, _, cursor, err = c.createKSListQuery(ctx, filter, pageSize, ctks)
|
||||
if err != nil {
|
||||
return exerr.Wrap(err, "").Build()
|
||||
}
|
||||
} else if ctpag, ok := inTok.(ct.CTPaginated); ok {
|
||||
_, cursor, err = c.createPaginatedListQuery(ctx, filter, pageSize, ctpag)
|
||||
if err != nil {
|
||||
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 exerr.Wrap(err, "").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)
|
||||
err = fn(v)
|
||||
if err != nil {
|
||||
return nil, nil, exerr.Wrap(err, "failed to decode entity").Build()
|
||||
return exerr.Wrap(err, "").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
|
||||
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
|
||||
if ctks, ok := inTok.(ct.CTKeySort); ok {
|
||||
_, _, _, _, cursor, err = c.createKSListQuery(ctx, filter, pageSize, ctks)
|
||||
if err != nil {
|
||||
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())
|
||||
}
|
||||
} else {
|
||||
return langext.IterSingleValueSeq2[TData, error](*new(TData), exerr.New(exerr.TypeCursorTokenDecode, "unknown ct type").Any("token", inTok).Type("tokenType", inTok).Build())
|
||||
}
|
||||
|
||||
pipelineSort := mongo.Pipeline{}
|
||||
pipelineFilter := mongo.Pipeline{}
|
||||
return func(yield func(TData, error) bool) {
|
||||
defer func() { _ = cursor.Close(ctx) }()
|
||||
|
||||
if filter != nil {
|
||||
pipelineFilter = filter.FilterQuery(ctx)
|
||||
pf1, pd1, pf2, pd2 := filter.Pagination(ctx)
|
||||
for cursor.Next(ctx) {
|
||||
v, err := c.decodeSingle(ctx, cursor)
|
||||
if err != nil {
|
||||
if !yield(*new(TData), err) {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
pipelineSort, err = createSortOnlyPipeline(pf1, pd1, &pf2, &pd2)
|
||||
if err != nil {
|
||||
return nil, nil, exerr.Wrap(err, "failed to create sort pipeline").Build()
|
||||
if !yield(v, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 nil, nil, 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, 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 (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