diff --git a/go.mod b/go.mod index af6a635..766dd27 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,14 @@ go 1.21 require ( github.com/gin-gonic/gin v1.9.1 + github.com/glebarez/go-sqlite v1.22.0 // only needed for tests -.- github.com/jmoiron/sqlx v1.3.5 github.com/rs/xid v1.5.0 github.com/rs/zerolog v1.31.0 go.mongodb.org/mongo-driver v1.13.1 - golang.org/x/crypto v0.17.0 + golang.org/x/crypto v0.18.0 golang.org/x/sys v0.16.0 golang.org/x/term v0.16.0 - github.com/glebarez/go-sqlite v1.22.0 // only needed for tests -.- ) require ( @@ -45,7 +45,7 @@ require ( github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect golang.org/x/arch v0.7.0 // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/net v0.20.0 // indirect golang.org/x/sync v0.6.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.32.0 // indirect diff --git a/go.sum b/go.sum index 6c26999..6017011 100644 --- a/go.sum +++ b/go.sum @@ -126,6 +126,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -134,6 +136,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= 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.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= diff --git a/goextVersion.go b/goextVersion.go index 9bc989b..d0a5304 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.362" +const GoextVersion = "0.0.363" -const GoextVersionTimestamp = "2024-01-07T04:18:03+0100" +const GoextVersionTimestamp = "2024-01-09T08:51:46+0100" diff --git a/wmo/collection.go b/wmo/collection.go index 1cea7ab..b69f715 100644 --- a/wmo/collection.go +++ b/wmo/collection.go @@ -45,7 +45,7 @@ type Coll[TData any] struct { customDecoder *func(ctx context.Context, dec Decodable) (TData, error) // custom decoding function (useful if TData is an interface) isInterfaceDataType bool // true if TData is an interface (not a struct) unmarshalHooks []func(d TData) TData // called for every object after unmarshalling - extraModPipeline mongo.Pipeline // appended to pipelines after filter/limit/skip/sort, used for $lookup, $set, $unset, $project, etc + extraModPipeline []func(ctx context.Context) mongo.Pipeline // appended to pipelines after filter/limit/skip/sort, used for $lookup, $set, $unset, $project, etc } func (c *Coll[TData]) Collection() *mongo.Collection { @@ -83,7 +83,13 @@ func (c *Coll[TData]) WithUnmarshalHook(fn func(d TData) TData) *Coll[TData] { } func (c *Coll[TData]) WithModifyingPipeline(p mongo.Pipeline) *Coll[TData] { - c.extraModPipeline = append(c.extraModPipeline, p...) + c.extraModPipeline = append(c.extraModPipeline, func(ctx context.Context) mongo.Pipeline { return p }) + + return c +} + +func (c *Coll[TData]) WithModifyingPipelineFunc(fn func(ctx context.Context) mongo.Pipeline) *Coll[TData] { + c.extraModPipeline = append(c.extraModPipeline, fn) return c } diff --git a/wmo/queryAggregate.go b/wmo/queryAggregate.go index f2b6a47..6dc0ab7 100644 --- a/wmo/queryAggregate.go +++ b/wmo/queryAggregate.go @@ -10,7 +10,9 @@ import ( func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) ([]TData, error) { - pipeline = langext.ArrConcat(pipeline, c.extraModPipeline) + for _, ppl := range c.extraModPipeline { + pipeline = langext.ArrConcat(pipeline, ppl(ctx)) + } cursor, err := c.coll.Aggregate(ctx, pipeline, opts...) if err != nil { @@ -27,7 +29,9 @@ func (c *Coll[TData]) Aggregate(ctx context.Context, pipeline mongo.Pipeline, op func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (*TData, error) { - pipeline = langext.ArrConcat(pipeline, c.extraModPipeline) + for _, ppl := range c.extraModPipeline { + pipeline = langext.ArrConcat(pipeline, ppl(ctx)) + } cursor, err := c.coll.Aggregate(ctx, pipeline, opts...) if err != nil { @@ -47,7 +51,9 @@ func (c *Coll[TData]) AggregateOneOpt(ctx context.Context, pipeline mongo.Pipeli func (c *Coll[TData]) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, opts ...*options.AggregateOptions) (TData, error) { - pipeline = langext.ArrConcat(pipeline, c.extraModPipeline) + for _, ppl := range c.extraModPipeline { + pipeline = langext.ArrConcat(pipeline, ppl(ctx)) + } cursor, err := c.coll.Aggregate(ctx, pipeline, opts...) if err != nil { diff --git a/wmo/queryFind.go b/wmo/queryFind.go index 6cb12e5..a6b5b30 100644 --- a/wmo/queryFind.go +++ b/wmo/queryFind.go @@ -32,7 +32,9 @@ func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options. } } - pipeline = langext.ArrConcat(pipeline, c.extraModPipeline) + for _, ppl := range c.extraModPipeline { + pipeline = langext.ArrConcat(pipeline, ppl(ctx)) + } for _, opt := range opts { if opt != nil && opt.Projection != nil { diff --git a/wmo/queryFindOne.go b/wmo/queryFindOne.go index be5d7ec..53e00ca 100644 --- a/wmo/queryFindOne.go +++ b/wmo/queryFindOne.go @@ -71,7 +71,9 @@ func (c *Coll[TData]) findOneInternal(ctx context.Context, filter bson.M, allowN pipeline = append(pipeline, bson.D{{Key: "$match", Value: filter}}) pipeline = append(pipeline, bson.D{{Key: "$limit", Value: 1}}) - pipeline = langext.ArrConcat(pipeline, c.extraModPipeline) + for _, ppl := range c.extraModPipeline { + pipeline = langext.ArrConcat(pipeline, ppl(ctx)) + } cursor, err := c.coll.Aggregate(ctx, pipeline) if err != nil { diff --git a/wmo/queryList.go b/wmo/queryList.go index 24cb034..eeba6d8 100644 --- a/wmo/queryList.go +++ b/wmo/queryList.go @@ -6,6 +6,7 @@ import ( "go.mongodb.org/mongo-driver/mongo" ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken" "gogs.mikescher.com/BlackForestBytes/goext/exerr" + "gogs.mikescher.com/BlackForestBytes/goext/langext" ) func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, inTok ct.CursorToken) ([]TData, ct.CursorToken, error) { @@ -50,7 +51,10 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int, } pipeline = append(pipeline, paginationPipeline...) - pipeline = append(pipeline, c.extraModPipeline...) + + for _, ppl := range c.extraModPipeline { + pipeline = langext.ArrConcat(pipeline, ppl(ctx)) + } cursor, err := c.coll.Aggregate(ctx, pipeline) if err != nil { diff --git a/wmo/queryPaginate.go b/wmo/queryPaginate.go index 0a0ea0b..bff51ad 100644 --- a/wmo/queryPaginate.go +++ b/wmo/queryPaginate.go @@ -42,7 +42,12 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.Filter, page int, pipelineCount := mongo.Pipeline{} pipelineCount = append(pipelineCount, bson.D{{Key: "$count", Value: "count"}}) - pipelineList := langext.ArrConcat(pipelineFilter, pipelineSort, pipelinePaginate, c.extraModPipeline, pipelineSort) + 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)