Compare commits

...

7 Commits

Author SHA1 Message Date
b704e2a362 v0.0.414 fix rfctime.Date bson marshalling for zero value
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m28s
2024-03-16 19:42:59 +01:00
6b4bd5a6f8 v0.0.413 fix tests
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m33s
2024-03-11 21:00:30 +01:00
6df4f5f2a1 v0.0.412 fix GenerateIDSpecs accepting nil for opt
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m32s
2024-03-11 20:58:06 +01:00
780905ba35 v0.0.411
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m33s
2024-03-11 20:43:37 +01:00
c679797765 v0.0.410 add ginext.SuppressGinLogs
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
2024-03-11 20:42:12 +01:00
401aad9fa4 v0.0.409
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m41s
2024-03-11 17:05:10 +01:00
645113d553 v0.0.408
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m43s
2024-03-11 16:41:47 +01:00
10 changed files with 103 additions and 22 deletions

View File

@@ -38,7 +38,7 @@ var rexIDChecksumConst = rext.W(regexp.MustCompile(`const ChecksumIDGenerator =
//go:embed id-generate.template //go:embed id-generate.template
var templateIDGenerateText string var templateIDGenerateText string
func GenerateIDSpecs(sourceDir string, destFile string, opt *IDGenOptions) error { func GenerateIDSpecs(sourceDir string, destFile string, opt IDGenOptions) error {
debugOutput := langext.Coalesce(opt.DebugOutput, false) debugOutput := langext.Coalesce(opt.DebugOutput, false)

View File

@@ -34,10 +34,10 @@ func TestGenerateIDSpecs(t *testing.T) {
_, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run() _, err = cmdext.Runner("tar").Arg("-xvzf").Arg(tmpFile).Arg("-C").Arg(tmpDir).FailOnExitCode().FailOnTimeout().Timeout(time.Minute).Run()
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go", &IDGenOptions{DebugOutput: langext.PTrue}) err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go", IDGenOptions{DebugOutput: langext.PTrue})
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go", &IDGenOptions{DebugOutput: langext.PTrue}) err = GenerateIDSpecs(tmpDir, tmpDir+"/id_gen.go", IDGenOptions{DebugOutput: langext.PTrue})
tst.AssertNoErr(t, err) tst.AssertNoErr(t, err)
fmt.Println() fmt.Println()

View File

@@ -39,6 +39,7 @@ type ginRouteSpec struct {
type Options struct { type Options struct {
AllowCors *bool // Add cors handler to allow all CORS requests on the default http methods AllowCors *bool // Add cors handler to allow all CORS requests on the default http methods
GinDebug *bool // Set gin.debug to true (adds more logs) GinDebug *bool // Set gin.debug to true (adds more logs)
SuppressGinLogs *bool // Suppress our custom gin logs (even if GinDebug == true)
BufferBody *bool // Buffers the input body stream, this way the ginext error handler can later include the whole request body BufferBody *bool // Buffers the input body stream, this way the ginext error handler can later include the whole request body
Timeout *time.Duration // The default handler timeout Timeout *time.Duration // The default handler timeout
ListenerBeforeRequest []func(g *gin.Context) // Register listener that are called before the handler method ListenerBeforeRequest []func(g *gin.Context) // Register listener that are called before the handler method
@@ -51,7 +52,7 @@ func NewEngine(opt Options) *GinWrapper {
wrapper := &GinWrapper{ wrapper := &GinWrapper{
engine: engine, engine: engine,
suppressGinLogs: false, suppressGinLogs: langext.Coalesce(opt.SuppressGinLogs, false),
allowCors: langext.Coalesce(opt.AllowCors, false), allowCors: langext.Coalesce(opt.AllowCors, false),
ginDebug: langext.Coalesce(opt.GinDebug, true), ginDebug: langext.Coalesce(opt.GinDebug, true),
bufferBody: langext.Coalesce(opt.BufferBody, false), bufferBody: langext.Coalesce(opt.BufferBody, false),
@@ -73,12 +74,12 @@ func NewEngine(opt Options) *GinWrapper {
if !wrapper.ginDebug { if !wrapper.ginDebug {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
ginlogger := gin.Logger() if !wrapper.suppressGinLogs {
engine.Use(func(context *gin.Context) { ginlogger := gin.Logger()
if !wrapper.suppressGinLogs { engine.Use(func(context *gin.Context) {
ginlogger(context) ginlogger(context)
} })
}) }
} else { } else {
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)
} }

2
go.mod
View File

@@ -15,7 +15,7 @@ require (
) )
require ( require (
github.com/bytedance/sonic v1.11.2 // indirect github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect

2
go.sum
View File

@@ -8,6 +8,8 @@ github.com/bytedance/sonic v1.11.1 h1:JC0+6c9FoWYYxakaoa+c5QTtJeiSZNeByOBhXtAFSn
github.com/bytedance/sonic v1.11.1/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/bytedance/sonic v1.11.1/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A= github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A=
github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA=
github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.407" const GoextVersion = "0.0.414"
const GoextVersionTimestamp = "2024-03-11T16:40:41+0100" const GoextVersionTimestamp = "2024-03-16T19:42:59+0100"

View File

@@ -5,12 +5,76 @@ import (
"time" "time"
) )
func Coalesce[T any](v *T, def T) T { func Coalesce[T any](v1 *T, def T) T {
if v == nil { if v1 != nil {
return def return *v1
} else {
return *v
} }
return def
}
func CoalesceOpt[T any](v1 *T, v2 *T) *T {
if v1 != nil {
return v1
}
return v2
}
func Coalesce3[T any](v1 *T, v2 *T, def T) T {
if v1 != nil {
return *v1
}
if v2 != nil {
return *v2
}
return def
}
func Coalesce3Opt[T any](v1 *T, v2 *T, v3 *T) *T {
if v1 != nil {
return v1
}
if v2 != nil {
return v2
}
return v3
}
func Coalesce4[T any](v1 *T, v2 *T, v3 *T, def T) T {
if v1 != nil {
return *v1
}
if v2 != nil {
return *v2
}
if v3 != nil {
return *v3
}
return def
}
func Coalesce4Opt[T any](v1 *T, v2 *T, v3 *T, v4 *T) *T {
if v1 != nil {
return v1
}
if v2 != nil {
return v2
}
if v3 != nil {
return v3
}
return v4
} }
func CoalesceString(s *string, def string) string { func CoalesceString(s *string, def string) string {

View File

@@ -102,6 +102,13 @@ func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
return err return err
} }
if tt == "" {
t.Year = 0
t.Month = 0
t.Day = 0
return nil
}
v, err := time.Parse(t.FormatStr(), tt) v, err := time.Parse(t.FormatStr(), tt)
if err != nil { if err != nil {
return err return err
@@ -114,7 +121,10 @@ func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
} }
func (t Date) MarshalBSONValue() (bsontype.Type, []byte, error) { func (t Date) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(t.TimeUTC().Format(t.FormatStr())) if t.IsZero() {
return bson.MarshalValue("")
}
return bson.MarshalValue(t.String())
} }
func (t Date) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { func (t Date) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
@@ -242,6 +252,10 @@ func (t *Date) ParseString(v string) error {
return nil return nil
} }
func (t Date) IsZero() bool {
return t.Year == 0 && t.Month == 0 && t.Day == 0
}
func NewDate(t time.Time) Date { func NewDate(t time.Time) Date {
return Date{ return Date{
Year: t.Year(), Year: t.Year(),

View File

@@ -6,7 +6,7 @@ import (
"gogs.mikescher.com/BlackForestBytes/goext/exerr" "gogs.mikescher.com/BlackForestBytes/goext/exerr"
) )
func Iterate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int, consumer func(v TData) error) (int, error) { func Iterate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int, consumer func(ctx context.Context, v TData) error) (int, error) {
if filter == nil { if filter == nil {
filter = NewEmptyPaginateFilter() filter = NewEmptyPaginateFilter()
} }

View File

@@ -334,7 +334,7 @@ func ScanAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode
return res, nil return res, nil
} }
func IterateAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool, consumer func(v TData) error) (int, error) { func IterateAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool, consumer func(ctx context.Context, v TData) error) (int, error) {
var strscan *StructScanner var strscan *StructScanner
if sec == Safe { if sec == Safe {
@@ -370,7 +370,7 @@ func IterateAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mo
return rcount, err return rcount, err
} }
err = consumer(data) err = consumer(ctx, data)
if err != nil { if err != nil {
return rcount, exerr.Wrap(err, "").Build() return rcount, exerr.Wrap(err, "").Build()
} }
@@ -384,7 +384,7 @@ func IterateAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mo
return rcount, err return rcount, err
} }
err = consumer(data) err = consumer(ctx, data)
if err != nil { if err != nil {
return rcount, exerr.Wrap(err, "").Build() return rcount, exerr.Wrap(err, "").Build()
} }