Compare commits

...

9 Commits

Author SHA1 Message Date
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
4a33986b6a v0.0.407 sq.Iterate
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
2024-03-11 16:40:41 +01:00
c1c8c64c76 v0.0.406 bf
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s
2024-03-10 16:44:21 +01:00
0927fdc4d7 v0.0.405
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m36s
2024-03-10 15:28:26 +01:00
10 changed files with 227 additions and 39 deletions

View File

@@ -38,7 +38,7 @@ var rexIDChecksumConst = rext.W(regexp.MustCompile(`const ChecksumIDGenerator =
//go:embed id-generate.template
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)

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()
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)
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)
fmt.Println()

View File

@@ -39,6 +39,7 @@ type ginRouteSpec struct {
type Options struct {
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)
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
Timeout *time.Duration // The default handler timeout
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{
engine: engine,
suppressGinLogs: false,
suppressGinLogs: langext.Coalesce(opt.SuppressGinLogs, false),
allowCors: langext.Coalesce(opt.AllowCors, false),
ginDebug: langext.Coalesce(opt.GinDebug, true),
bufferBody: langext.Coalesce(opt.BufferBody, false),
@@ -73,12 +74,12 @@ func NewEngine(opt Options) *GinWrapper {
if !wrapper.ginDebug {
gin.SetMode(gin.ReleaseMode)
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
if !wrapper.suppressGinLogs {
if !wrapper.suppressGinLogs {
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
ginlogger(context)
}
})
})
}
} else {
gin.SetMode(gin.DebugMode)
}

View File

@@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.404"
const GoextVersion = "0.0.413"
const GoextVersionTimestamp = "2024-03-10T15:25:30+0100"
const GoextVersionTimestamp = "2024-03-11T21:00:30+0100"

View File

@@ -5,12 +5,76 @@ import (
"time"
)
func Coalesce[T any](v *T, def T) T {
if v == nil {
return def
} else {
return *v
func Coalesce[T any](v1 *T, def T) T {
if v1 != nil {
return *v1
}
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 {

View File

@@ -67,36 +67,20 @@ func (t *Date) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &str); err != nil {
return err
}
t0, err := time.Parse(t.FormatStr(), str)
if err != nil {
return err
}
t.Year = t0.Year()
t.Month = int(t0.Month())
t.Day = t0.Day()
return nil
return t.ParseString(str)
}
func (t Date) MarshalJSON() ([]byte, error) {
str := t.TimeUTC().Format(t.FormatStr())
str := t.String()
return json.Marshal(str)
}
func (t Date) MarshalText() ([]byte, error) {
b := make([]byte, 0, len(t.FormatStr()))
return t.TimeUTC().AppendFormat(b, t.FormatStr()), nil
return []byte(t.String()), nil
}
func (t *Date) UnmarshalText(data []byte) error {
var err error
v, err := time.Parse(t.FormatStr(), string(data))
if err != nil {
return err
}
t.Year = v.Year()
t.Month = int(v.Month())
t.Day = v.Day()
return nil
return t.ParseString(string(data))
}
func (t *Date) UnmarshalBSONValue(bt bsontype.Type, data []byte) error {
@@ -166,7 +150,7 @@ func (t Date) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val
}
func (t Date) Serialize() string {
return t.TimeUTC().Format(t.FormatStr())
return t.String()
}
func (t Date) FormatStr() string {
@@ -230,11 +214,11 @@ func (t *Date) ParseString(v string) error {
if err != nil {
return errors.New("invalid date format: " + v + ": " + err.Error())
}
month, err := strconv.ParseInt(split[0], 10, 32)
month, err := strconv.ParseInt(split[1], 10, 32)
if err != nil {
return errors.New("invalid date format: " + v + ": " + err.Error())
}
day, err := strconv.ParseInt(split[0], 10, 32)
day, err := strconv.ParseInt(split[2], 10, 32)
if err != nil {
return errors.New("invalid date format: " + v + ": " + err.Error())
}

View File

@@ -47,3 +47,10 @@ func NewSimplePaginateFilter(filterClause string, filterParams PP, sort []Filter
},
}
}
func NewEmptyPaginateFilter() PaginateFilter {
return genericPaginateFilter{
sql: func(params PP) (string, string, []string) { return "1=1", "", nil },
sort: func() []FilterSort { return make([]FilterSort, 0) },
}
}

48
sq/list.go Normal file
View File

@@ -0,0 +1,48 @@
package sq
import (
"context"
"fmt"
"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(ctx context.Context, v TData) error) (int, error) {
if filter == nil {
filter = NewEmptyPaginateFilter()
}
prepParams := PP{}
sortOrder := filter.Sort()
sortCond := ""
if len(sortOrder) > 0 {
sortCond = "ORDER BY "
for i, v := range sortOrder {
if i > 0 {
sortCond += ", "
}
sortCond += v.Field + " " + string(v.Direction)
}
}
pageCond := ""
if limit != nil {
pageCond += fmt.Sprintf("LIMIT :%s OFFSET :%s", prepParams.Add(*limit+1), prepParams.Add(*limit*(page-1)))
}
filterCond, joinCond, joinTables := filter.SQL(prepParams)
selectCond := table + ".*"
for _, v := range joinTables {
selectCond += ", " + v + ".*"
}
sqlQueryData := "SELECT " + selectCond + " FROM " + table + " " + joinCond + " WHERE ( " + filterCond + " ) " + sortCond + " " + pageCond
rows, err := q.Query(ctx, sqlQueryData, prepParams)
if err != nil {
return 0, exerr.Wrap(err, "failed to list paginated entries from DB").Str("table", table).Any("filter", filter).Int("page", page).Any("limit", limit).Build()
}
return IterateAll[TData](ctx, q, rows, scanMode, scanSec, true, consumer)
}

View File

@@ -9,6 +9,10 @@ import (
)
func Paginate[TData any](ctx context.Context, q Queryable, table string, filter PaginateFilter, scanMode StructScanMode, scanSec StructScanSafety, page int, limit *int) ([]TData, pag.Pagination, error) {
if filter == nil {
filter = NewEmptyPaginateFilter()
}
prepParams := PP{}
sortOrder := filter.Sort()
@@ -90,6 +94,10 @@ func Paginate[TData any](ctx context.Context, q Queryable, table string, filter
}
func Count(ctx context.Context, q Queryable, table string, filter PaginateFilter) (int, error) {
if filter == nil {
filter = NewEmptyPaginateFilter()
}
prepParams := PP{}
filterCond, joinCond, _ := filter.SQL(prepParams)

View File

@@ -333,3 +333,79 @@ func ScanAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode
}
return res, nil
}
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
if sec == Safe {
strscan = NewStructScanner(rows, false)
var data TData
err := strscan.Start(&data)
if err != nil {
return 0, err
}
} else if sec == Unsafe {
strscan = NewStructScanner(rows, true)
var data TData
err := strscan.Start(&data)
if err != nil {
return 0, err
}
} else {
return 0, errors.New("unknown value for <sec>")
}
rcount := 0
for rows.Next() {
if err := ctx.Err(); err != nil {
return rcount, err
}
if mode == SModeFast {
var data TData
err := strscan.StructScanBase(&data)
if err != nil {
return rcount, err
}
err = consumer(ctx, data)
if err != nil {
return rcount, exerr.Wrap(err, "").Build()
}
rcount++
} else if mode == SModeExtended {
var data TData
err := strscan.StructScanExt(q, &data)
if err != nil {
return rcount, err
}
err = consumer(ctx, data)
if err != nil {
return rcount, exerr.Wrap(err, "").Build()
}
rcount++
} else {
return rcount, errors.New("unknown value for <mode>")
}
}
if close {
err := strscan.rows.Close()
if err != nil {
return rcount, err
}
}
if err := rows.Err(); err != nil {
return rcount, err
}
return rcount, nil
}