Compare commits

...

3 Commits

Author SHA1 Message Date
78e1c33e30 v0.0.475 ArrGroupBy
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m36s
2024-06-16 17:14:21 +02:00
d2f2a0558a v0.0.474 Add ZeroLogger config field to exerr.Init to override used zerolog instance
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m27s
2024-06-14 23:18:58 +02:00
fc4bed4b9f v0.0.473 add ctx to wmo.FilterQuery|Sort|Pagination
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m23s
2024-06-14 17:24:59 +02:00
10 changed files with 65 additions and 35 deletions

View File

@@ -1,14 +1,15 @@
package cursortoken package cursortoken
import ( import (
"context"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
type RawFilter interface { type RawFilter interface {
FilterQuery() mongo.Pipeline FilterQuery(ctx context.Context) mongo.Pipeline
} }
type Filter interface { type Filter interface {
FilterQuery() mongo.Pipeline FilterQuery(ctx context.Context) mongo.Pipeline
Pagination() (string, SortDirection, string, SortDirection) Pagination(ctx context.Context) (string, SortDirection, string, SortDirection)
} }

View File

@@ -55,18 +55,6 @@ import (
// => Wrap/New + Fatal // => Wrap/New + Fatal
// //
var stackSkipLogger zerolog.Logger
func init() {
cw := zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05 Z07:00",
}
multi := zerolog.MultiLevelWriter(cw)
stackSkipLogger = zerolog.New(multi).With().Timestamp().CallerWithSkipFrameCount(4).Logger()
}
type Builder struct { type Builder struct {
wrappedErr error wrappedErr error
errorData *ExErr errorData *ExErr
@@ -435,9 +423,9 @@ func (b *Builder) Build(ctxs ...context.Context) error {
} }
if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) { if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) {
b.errorData.ShortLog(stackSkipLogger.Error()) b.errorData.ShortLog(pkgconfig.ZeroLogger.Error())
} else if pkgconfig.ZeroLogAllTraces && !b.noLog { } else if pkgconfig.ZeroLogAllTraces && !b.noLog {
b.errorData.ShortLog(stackSkipLogger.Error()) b.errorData.ShortLog(pkgconfig.ZeroLogger.Error())
} }
b.errorData.CallListener(MethodBuild) b.errorData.CallListener(MethodBuild)
@@ -459,9 +447,9 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
b.errorData.Output(g) b.errorData.Output(g)
if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) { if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) {
b.errorData.Log(stackSkipLogger.Error()) b.errorData.Log(pkgconfig.ZeroLogger.Error())
} else if (b.errorData.Severity == SevWarn) && (pkgconfig.ZeroLogAllGinOutput) { } else if (b.errorData.Severity == SevWarn) && (pkgconfig.ZeroLogAllGinOutput) {
b.errorData.Log(stackSkipLogger.Warn()) b.errorData.Log(pkgconfig.ZeroLogger.Warn())
} }
b.errorData.CallListener(MethodOutput) b.errorData.CallListener(MethodOutput)
@@ -477,9 +465,9 @@ func (b *Builder) Print(ctxs ...context.Context) {
} }
if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
b.errorData.Log(stackSkipLogger.Error()) b.errorData.Log(pkgconfig.ZeroLogger.Error())
} else if b.errorData.Severity == SevWarn { } else if b.errorData.Severity == SevWarn {
b.errorData.ShortLog(stackSkipLogger.Warn()) b.errorData.ShortLog(pkgconfig.ZeroLogger.Warn())
} }
b.errorData.CallListener(MethodPrint) b.errorData.CallListener(MethodPrint)
@@ -499,7 +487,7 @@ func (b *Builder) Fatal(ctxs ...context.Context) {
b.CtxData(MethodFatal, dctx) b.CtxData(MethodFatal, dctx)
} }
b.errorData.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel)) b.errorData.Log(pkgconfig.ZeroLogger.WithLevel(zerolog.FatalLevel))
b.errorData.CallListener(MethodFatal) b.errorData.CallListener(MethodFatal)

View File

@@ -4,8 +4,10 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http" "net/http"
"os"
) )
type ErrorPackageConfig struct { type ErrorPackageConfig struct {
@@ -21,6 +23,7 @@ type ErrorPackageConfig struct {
ZeroLogAllGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for all Severities) ZeroLogAllGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for all Severities)
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) // (Optionally) extend the final error meta values with additional data from the gin context (a few are automatically added, here more can be included) ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) // (Optionally) extend the final error meta values with additional data from the gin context (a few are automatically added, here more can be included)
ExtendContextMeta func(b *Builder, method Method, dctx context.Context) // (Optionally) extend the final error meta values with additional data from the context (a few are automatically added, here more can be included) ExtendContextMeta func(b *Builder, method Method, dctx context.Context) // (Optionally) extend the final error meta values with additional data from the context (a few are automatically added, here more can be included)
ZeroLogger zerolog.Logger // The logger used to print exerr log messages
} }
type ErrorPackageConfigInit struct { type ErrorPackageConfigInit struct {
@@ -36,6 +39,7 @@ type ErrorPackageConfigInit struct {
ZeroLogAllGinOutput *bool ZeroLogAllGinOutput *bool
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request)
ExtendContextMeta func(b *Builder, method Method, dctx context.Context) ExtendContextMeta func(b *Builder, method Method, dctx context.Context)
ZeroLogger *zerolog.Logger
} }
var initialized = false var initialized = false
@@ -81,6 +85,13 @@ func Init(cfg ErrorPackageConfigInit) {
egcm = cfg.ExtendContextMeta egcm = cfg.ExtendContextMeta
} }
var logger zerolog.Logger
if cfg.ZeroLogger != nil {
logger = *cfg.ZeroLogger
} else {
logger = newDefaultLogger()
}
pkgconfig = ErrorPackageConfig{ pkgconfig = ErrorPackageConfig{
ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces), ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces),
ZeroLogAllTraces: langext.Coalesce(cfg.ZeroLogAllTraces, pkgconfig.ZeroLogAllTraces), ZeroLogAllTraces: langext.Coalesce(cfg.ZeroLogAllTraces, pkgconfig.ZeroLogAllTraces),
@@ -94,11 +105,23 @@ func Init(cfg ErrorPackageConfigInit) {
ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput), ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput),
ExtendGinMeta: egm, ExtendGinMeta: egm,
ExtendContextMeta: egcm, ExtendContextMeta: egcm,
ZeroLogger: logger,
} }
initialized = true initialized = true
} }
func newDefaultLogger() zerolog.Logger {
cw := zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05 Z07:00",
}
multi := zerolog.MultiLevelWriter(cw)
return zerolog.New(multi).With().Timestamp().CallerWithSkipFrameCount(4).Logger()
}
func Initialized() bool { func Initialized() bool {
return initialized return initialized
} }

2
go.mod
View File

@@ -8,7 +8,7 @@ require (
github.com/jmoiron/sqlx v1.4.0 github.com/jmoiron/sqlx v1.4.0
github.com/rs/xid v1.5.0 github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.33.0 github.com/rs/zerolog v1.33.0
go.mongodb.org/mongo-driver v1.15.0 go.mongodb.org/mongo-driver v1.15.1
golang.org/x/crypto v0.24.0 golang.org/x/crypto v0.24.0
golang.org/x/sys v0.21.0 golang.org/x/sys v0.21.0
golang.org/x/term v0.21.0 golang.org/x/term v0.21.0

2
go.sum
View File

@@ -218,6 +218,8 @@ go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
go.mongodb.org/mongo-driver v1.15.0 h1:rJCKC8eEliewXjZGf0ddURtl7tTVy1TK3bfl0gkUSLc= go.mongodb.org/mongo-driver v1.15.0 h1:rJCKC8eEliewXjZGf0ddURtl7tTVy1TK3bfl0gkUSLc=
go.mongodb.org/mongo-driver v1.15.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= go.mongodb.org/mongo-driver v1.15.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
go.mongodb.org/mongo-driver v1.15.1 h1:l+RvoUOoMXFmADTLfYDm7On9dRm7p4T80/lEQM+r7HU=
go.mongodb.org/mongo-driver v1.15.1/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.472" const GoextVersion = "0.0.475"
const GoextVersionTimestamp = "2024-06-14T14:56:41+0200" const GoextVersionTimestamp = "2024-06-16T17:14:21+0200"

View File

@@ -564,3 +564,18 @@ func ArrChunk[T any](arr []T, chunkSize int) [][]T {
return res return res
} }
func ArrGroupBy[T1 any, T2 comparable](arr []T1, groupfunc func(v T1) T2) map[T2][]T1 {
r := make(map[T2][]T1)
for _, v := range arr {
key := groupfunc(v)
if _, ok := r[key]; ok {
r[key] = append(r[key], v)
} else {
r[key] = []T1{v}
}
}
return r
}

View File

@@ -1,13 +1,14 @@
package pagination package pagination
import ( import (
"context"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
type MongoFilter interface { type MongoFilter interface {
FilterQuery() mongo.Pipeline FilterQuery(ctx context.Context) mongo.Pipeline
Sort() bson.D Sort(ctx context.Context) bson.D
} }
type dynamicFilter struct { type dynamicFilter struct {
@@ -15,11 +16,11 @@ type dynamicFilter struct {
sort bson.D sort bson.D
} }
func (d dynamicFilter) FilterQuery() mongo.Pipeline { func (d dynamicFilter) FilterQuery(ctx context.Context) mongo.Pipeline {
return d.pipeline return d.pipeline
} }
func (d dynamicFilter) Sort() bson.D { func (d dynamicFilter) Sort(ctx context.Context) bson.D {
return d.sort return d.sort
} }

View File

@@ -25,8 +25,8 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int,
pd2 := ct.SortASC pd2 := ct.SortASC
if filter != nil { if filter != nil {
pipeline = filter.FilterQuery() pipeline = filter.FilterQuery(ctx)
pf1, pd1, pf2, pd2 = filter.Pagination() pf1, pd1, pf2, pd2 = filter.Pagination(ctx)
} }
sortPrimary := pf1 sortPrimary := pf1
@@ -109,7 +109,7 @@ func (c *Coll[TData]) Count(ctx context.Context, filter ct.RawFilter) (int64, er
Count int64 `bson:"c"` Count int64 `bson:"c"`
} }
pipeline := filter.FilterQuery() pipeline := filter.FilterQuery(ctx)
pipeline = append(pipeline, bson.D{{Key: "$count", Value: "c"}}) pipeline = append(pipeline, bson.D{{Key: "$count", Value: "c"}})
@@ -152,7 +152,7 @@ func (c *Coll[TData]) ListAllIDs(ctx context.Context, filter ct.RawFilter) ([]st
pipelineFilter := mongo.Pipeline{} pipelineFilter := mongo.Pipeline{}
if filter != nil { if filter != nil {
pipelineFilter = filter.FilterQuery() pipelineFilter = filter.FilterQuery(ctx)
} }
extrModPipelineResolved := mongo.Pipeline{} extrModPipelineResolved := mongo.Pipeline{}

View File

@@ -23,8 +23,8 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.MongoFilter, page
sort := bson.D{} sort := bson.D{}
if filter != nil { if filter != nil {
pipelineFilter = filter.FilterQuery() pipelineFilter = filter.FilterQuery(ctx)
sort = filter.Sort() sort = filter.Sort(ctx)
} }
if len(sort) != 0 { if len(sort) != 0 {