Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
7e16e799e4
|
|||
890e16241d
|
|||
b9d0348735
|
|||
b9e9575b9b
|
|||
295a098eb4
|
|||
b69a082bb1
|
@@ -9,6 +9,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -667,6 +668,28 @@ func (v MetaValue) rawValueForJson() any {
|
|||||||
}
|
}
|
||||||
return v.Value.(EnumWrap).ValueString
|
return v.Value.(EnumWrap).ValueString
|
||||||
}
|
}
|
||||||
|
if v.DataType == MDTFloat32 {
|
||||||
|
if math.IsNaN(float64(v.Value.(float32))) {
|
||||||
|
return "float64::NaN"
|
||||||
|
} else if math.IsInf(float64(v.Value.(float32)), +1) {
|
||||||
|
return "float64::+inf"
|
||||||
|
} else if math.IsInf(float64(v.Value.(float32)), -1) {
|
||||||
|
return "float64::-inf"
|
||||||
|
} else {
|
||||||
|
return v.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v.DataType == MDTFloat64 {
|
||||||
|
if math.IsNaN(v.Value.(float64)) {
|
||||||
|
return "float64::NaN"
|
||||||
|
} else if math.IsInf(v.Value.(float64), +1) {
|
||||||
|
return "float64::+inf"
|
||||||
|
} else if math.IsInf(v.Value.(float64), -1) {
|
||||||
|
return "float64::-inf"
|
||||||
|
} else {
|
||||||
|
return v.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
return v.Value
|
return v.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -163,16 +163,16 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
|
|
||||||
ictx, cancel := context.WithTimeout(context.Background(), langext.Coalesce(pctx.timeout, pctx.wrapper.requestTimeout))
|
ictx, cancel := context.WithTimeout(context.Background(), langext.Coalesce(pctx.timeout, pctx.wrapper.requestTimeout))
|
||||||
|
|
||||||
|
actx := CreateAppContext(pctx.ginCtx, ictx, cancel)
|
||||||
|
|
||||||
if pctx.persistantData.sessionObj != nil {
|
if pctx.persistantData.sessionObj != nil {
|
||||||
err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, ictx)
|
err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, actx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
actx.Cancel()
|
||||||
return nil, nil, langext.Ptr(Error(exerr.Wrap(err, "Failed to init session").Build()))
|
return nil, nil, langext.Ptr(Error(exerr.Wrap(err, "Failed to init session").Build()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
actx := CreateAppContext(pctx.ginCtx, ictx, cancel)
|
|
||||||
|
|
||||||
return actx, pctx.ginCtx, nil
|
return actx, pctx.ginCtx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,6 +6,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SessionObject interface {
|
type SessionObject interface {
|
||||||
Init(g *gin.Context, ctx context.Context) error
|
Init(g *gin.Context, ctx *AppContext) error
|
||||||
Finish(ctx context.Context, resp HTTPResponse) error
|
Finish(ctx context.Context, resp HTTPResponse) error
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@@ -23,7 +23,7 @@ require (
|
|||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.16.0 // indirect
|
github.com/go-playground/validator/v10 v10.17.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/golang/snappy v0.0.4 // indirect
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@@ -31,6 +31,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
|||||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
|
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
|
||||||
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||||
|
github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74=
|
||||||
|
github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.375"
|
const GoextVersion = "0.0.381"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-01-14T01:50:48+0100"
|
const GoextVersionTimestamp = "2024-01-23T17:51:52+0100"
|
||||||
|
21
langext/must.go
Normal file
21
langext/must.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
// Must returns a value and panics on error
|
||||||
|
//
|
||||||
|
// Usage: Must(methodWithError(...))
|
||||||
|
func Must[T any](v T, err error) T {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustBool returns a value and panics on missing
|
||||||
|
//
|
||||||
|
// Usage: MustBool(methodWithOkayReturn(...))
|
||||||
|
func MustBool[T any](v T, ok bool) T {
|
||||||
|
if !ok {
|
||||||
|
panic("not ok")
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
72
reflectext/convertToMap.go
Normal file
72
reflectext/convertToMap.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package reflectext
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
func ConvertStructToMap(v any) any {
|
||||||
|
return reflectToMap(reflect.ValueOf(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectToMap(fv reflect.Value) any {
|
||||||
|
|
||||||
|
if fv.Kind() == reflect.Ptr {
|
||||||
|
|
||||||
|
if fv.IsNil() {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return reflectToMap(fv.Elem())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if fv.Kind() == reflect.Func {
|
||||||
|
|
||||||
|
// skip
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if fv.Kind() == reflect.Array {
|
||||||
|
|
||||||
|
arrlen := fv.Len()
|
||||||
|
arr := make([]any, arrlen)
|
||||||
|
for i := 0; i < arrlen; i++ {
|
||||||
|
arr[i] = reflectToMap(fv.Index(i))
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if fv.Kind() == reflect.Slice {
|
||||||
|
|
||||||
|
arrlen := fv.Len()
|
||||||
|
arr := make([]any, arrlen)
|
||||||
|
for i := 0; i < arrlen; i++ {
|
||||||
|
arr[i] = reflectToMap(fv.Index(i))
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if fv.Kind() == reflect.Chan {
|
||||||
|
|
||||||
|
// skip
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if fv.Kind() == reflect.Struct {
|
||||||
|
|
||||||
|
res := make(map[string]any)
|
||||||
|
|
||||||
|
for i := 0; i < fv.NumField(); i++ {
|
||||||
|
if fv.Type().Field(i).IsExported() {
|
||||||
|
res[fv.Type().Field(i).Name] = reflectToMap(fv.Field(i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return fv.Interface()
|
||||||
|
}
|
@@ -96,7 +96,7 @@ func BuildInsertStatement(q Queryable, tableName string, obj any) (string, PP, e
|
|||||||
if rsfield.Type.Kind() == reflect.Ptr && rvfield.IsNil() {
|
if rsfield.Type.Kind() == reflect.Ptr && rvfield.IsNil() {
|
||||||
|
|
||||||
fields = append(fields, columnName)
|
fields = append(fields, columnName)
|
||||||
values = append(fields, "NULL")
|
values = append(values, "NULL")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ func BuildInsertStatement(q Queryable, tableName string, obj any) (string, PP, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
fields = append(fields, columnName)
|
fields = append(fields, columnName)
|
||||||
values = append(fields, ":"+params.Add(val))
|
values = append(values, ":"+params.Add(val))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,9 +18,9 @@ type DBTypeConverter interface {
|
|||||||
DBToModel(v any) (any, error)
|
DBToModel(v any) (any, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ConverterBoolToBit = NewDBTypeConverter[bool, int](func(v bool) (int, error) {
|
var ConverterBoolToBit = NewDBTypeConverter[bool, int64](func(v bool) (int64, error) {
|
||||||
return langext.Conditional(v, 1, 0), nil
|
return langext.Conditional(v, int64(1), int64(0)), nil
|
||||||
}, func(v int) (bool, error) {
|
}, func(v int64) (bool, error) {
|
||||||
if v == 0 {
|
if v == 0 {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user