Compare commits

...

4 Commits

Author SHA1 Message Date
7d64f18f54 v0.0.368
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m9s
2024-01-13 01:29:40 +01:00
d08b2e565a v0.0.367
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m32s
2024-01-12 18:40:29 +01:00
d29e84894d v0.0.366 ginext: set cookies
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m28s
2024-01-12 15:10:48 +01:00
617298c366 v0.0.365
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m24s
2024-01-09 18:23:46 +01:00
6 changed files with 248 additions and 7 deletions

View File

@@ -17,7 +17,7 @@ import (
type GinWrapper struct {
engine *gin.Engine
SuppressGinLogs bool
suppressGinLogs bool
allowCors bool
ginDebug bool
@@ -51,7 +51,7 @@ func NewEngine(opt Options) *GinWrapper {
wrapper := &GinWrapper{
engine: engine,
SuppressGinLogs: false,
suppressGinLogs: false,
allowCors: langext.Coalesce(opt.AllowCors, false),
ginDebug: langext.Coalesce(opt.GinDebug, true),
bufferBody: langext.Coalesce(opt.BufferBody, false),
@@ -75,7 +75,7 @@ func NewEngine(opt Options) *GinWrapper {
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
if !wrapper.SuppressGinLogs {
if !wrapper.suppressGinLogs {
ginlogger(context)
}
})

View File

@@ -9,6 +9,16 @@ import (
"os"
)
type cookieval struct {
name string
value string
maxAge int
path string
domain string
secure bool
httpOnly bool
}
type headerval struct {
Key string
Val string
@@ -17,6 +27,7 @@ type headerval struct {
type HTTPResponse interface {
Write(g *gin.Context)
WithHeader(k string, v string) HTTPResponse
WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse
IsSuccess() bool
}
@@ -33,6 +44,7 @@ type jsonHTTPResponse struct {
statusCode int
data any
headers []headerval
cookies []cookieval
}
func (j jsonHTTPResponse) jsonRenderer(g *gin.Context) json.GoJsonRender {
@@ -47,6 +59,9 @@ func (j jsonHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.Render(j.statusCode, j.jsonRenderer(g))
}
@@ -55,6 +70,11 @@ func (j jsonHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j jsonHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j jsonHTTPResponse) IsSuccess() bool {
return j.statusCode >= 200 && j.statusCode <= 399
}
@@ -82,12 +102,16 @@ func (j jsonHTTPResponse) Headers() []string {
type emptyHTTPResponse struct {
statusCode int
headers []headerval
cookies []cookieval
}
func (j emptyHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.Status(j.statusCode)
}
@@ -96,6 +120,11 @@ func (j emptyHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j emptyHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j emptyHTTPResponse) IsSuccess() bool {
return j.statusCode >= 200 && j.statusCode <= 399
}
@@ -120,12 +149,16 @@ type textHTTPResponse struct {
statusCode int
data string
headers []headerval
cookies []cookieval
}
func (j textHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.String(j.statusCode, "%s", j.data)
}
@@ -134,6 +167,11 @@ func (j textHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j textHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j textHTTPResponse) IsSuccess() bool {
return j.statusCode >= 200 && j.statusCode <= 399
}
@@ -159,12 +197,16 @@ type dataHTTPResponse struct {
data []byte
contentType string
headers []headerval
cookies []cookieval
}
func (j dataHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.Data(j.statusCode, j.contentType, j.data)
}
@@ -173,6 +215,11 @@ func (j dataHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j dataHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j dataHTTPResponse) IsSuccess() bool {
return j.statusCode >= 200 && j.statusCode <= 399
}
@@ -198,6 +245,7 @@ type fileHTTPResponse struct {
filepath string
filename *string
headers []headerval
cookies []cookieval
}
func (j fileHTTPResponse) Write(g *gin.Context) {
@@ -209,6 +257,9 @@ func (j fileHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.File(j.filepath)
}
@@ -217,6 +268,11 @@ func (j fileHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j fileHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j fileHTTPResponse) IsSuccess() bool {
return true
}
@@ -247,17 +303,20 @@ type downloadDataHTTPResponse struct {
data []byte
filename *string
headers []headerval
cookies []cookieval
}
func (j downloadDataHTTPResponse) Write(g *gin.Context) {
g.Header("Content-Type", j.mimetype) // if we don't set it here gin does weird file-sniffing later...
if j.filename != nil {
g.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", *j.filename))
}
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.Data(j.statusCode, j.mimetype, j.data)
}
@@ -266,6 +325,11 @@ func (j downloadDataHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j downloadDataHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j downloadDataHTTPResponse) IsSuccess() bool {
return j.statusCode >= 200 && j.statusCode <= 399
}
@@ -290,9 +354,16 @@ type redirectHTTPResponse struct {
statusCode int
url string
headers []headerval
cookies []cookieval
}
func (j redirectHTTPResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
g.Redirect(j.statusCode, j.url)
}
@@ -301,6 +372,11 @@ func (j redirectHTTPResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j redirectHTTPResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j redirectHTTPResponse) IsSuccess() bool {
return j.statusCode >= 200 && j.statusCode <= 399
}
@@ -324,9 +400,16 @@ func (j redirectHTTPResponse) Headers() []string {
type jsonAPIErrResponse struct {
err *exerr.ExErr
headers []headerval
cookies []cookieval
}
func (j jsonAPIErrResponse) Write(g *gin.Context) {
for _, v := range j.headers {
g.Header(v.Key, v.Val)
}
for _, v := range j.cookies {
g.SetCookie(v.name, v.value, v.maxAge, v.path, v.domain, v.secure, v.httpOnly)
}
j.err.Output(g)
j.err.CallListener(exerr.MethodOutput)
@@ -337,6 +420,11 @@ func (j jsonAPIErrResponse) WithHeader(k string, v string) HTTPResponse {
return j
}
func (j jsonAPIErrResponse) WithCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool) HTTPResponse {
j.cookies = append(j.cookies, cookieval{name, value, maxAge, path, domain, secure, httpOnly})
return j
}
func (j jsonAPIErrResponse) IsSuccess() bool {
return false
}

View File

@@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.364"
const GoextVersion = "0.0.368"
const GoextVersionTimestamp = "2024-01-09T18:17:55+0100"
const GoextVersionTimestamp = "2024-01-13T01:29:40+0100"

98
reflectext/mapAccess.go Normal file
View File

@@ -0,0 +1,98 @@
package reflectext
import (
"reflect"
"strings"
)
// GetMapPath returns the value deep inside a hierahically nested map structure
// eg:
// x := langext.H{"K1": langext.H{"K2": 665}}
// GetMapPath[int](x, "K1.K2") == 665
func GetMapPath[TData any](mapval any, path string) (TData, bool) {
var ok bool
split := strings.Split(path, ".")
for i, key := range split {
if i < len(split)-1 {
mapval, ok = GetMapField[any](mapval, key)
if !ok {
return *new(TData), false
}
} else {
return GetMapField[TData](mapval, key)
}
}
return *new(TData), false
}
// GetMapField gets the value of a map, without knowing the actual types (mapval is any)
// eg:
// x := langext.H{"K1": 665}
// GetMapPath[int](x, "K1") == 665
//
// works with aliased types and autom. dereferences pointes
func GetMapField[TData any, TKey comparable](mapval any, key TKey) (TData, bool) {
rval := reflect.ValueOf(mapval)
for rval.Kind() == reflect.Ptr && !rval.IsNil() {
rval = rval.Elem()
}
if rval.Kind() != reflect.Map {
return *new(TData), false // mapval is not a map
}
kval := reflect.ValueOf(key)
if !kval.Type().AssignableTo(rval.Type().Key()) {
return *new(TData), false // key cannot index mapval
}
eval := rval.MapIndex(kval)
if !eval.IsValid() {
return *new(TData), false // key does not exist in mapval
}
destType := reflect.TypeOf(new(TData)).Elem()
if eval.Type() == destType {
return eval.Interface().(TData), true
}
if eval.CanConvert(destType) && !preventConvert(eval.Type(), destType) {
return eval.Convert(destType).Interface().(TData), true
}
if (eval.Kind() == reflect.Ptr || eval.Kind() == reflect.Interface) && eval.IsNil() && destType.Kind() == reflect.Ptr {
return *new(TData), false // special case: mapval[key] is nil
}
for (eval.Kind() == reflect.Ptr || eval.Kind() == reflect.Interface) && !eval.IsNil() {
eval = eval.Elem()
if eval.Type() == destType {
return eval.Interface().(TData), true
}
if eval.CanConvert(destType) && !preventConvert(eval.Type(), destType) {
return eval.Convert(destType).Interface().(TData), true
}
}
return *new(TData), false // mapval[key] is not of type TData
}
func preventConvert(t1 reflect.Type, t2 reflect.Type) bool {
if t1.Kind() == reflect.String && t1.Kind() != reflect.String {
return true
}
if t2.Kind() == reflect.String && t1.Kind() != reflect.String {
return true
}
return false
}

View File

@@ -0,0 +1,55 @@
package reflectext
import (
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/tst"
"testing"
)
func TestGetMapPath(t *testing.T) {
type PseudoInt = int64
mymap2 := map[string]map[string]any{"Test": {"Second": 3}}
var maany2 any = mymap2
tst.AssertEqual(t, fmt.Sprint(GetMapPath[int](maany2, "Test.Second")), "3 true")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[int](maany2, "Test2.Second")), "0 false")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[int](maany2, "Test.Second2")), "0 false")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[string](maany2, "Test.Second")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[string](maany2, "Test2.Second")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[string](maany2, "Test.Second2")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[PseudoInt](maany2, "Test.Second")), "3 true")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[PseudoInt](maany2, "Test2.Second")), "0 false")
tst.AssertEqual(t, fmt.Sprint(GetMapPath[PseudoInt](maany2, "Test.Second2")), "0 false")
}
func TestGetMapField(t *testing.T) {
type PseudoInt = int64
mymap1 := map[string]any{"Test": 12}
mymap2 := map[string]int{"Test": 12}
var maany1 any = mymap1
var maany2 any = mymap2
tst.AssertEqual(t, fmt.Sprint(GetMapField[int](maany1, "Test")), "12 true")
tst.AssertEqual(t, fmt.Sprint(GetMapField[int](maany1, "Test2")), "0 false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[string](maany1, "Test")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[string](maany1, "Test2")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany1, "Test")), "12 true")
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany1, "Test2")), "0 false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[int](maany2, "Test")), "12 true")
tst.AssertEqual(t, fmt.Sprint(GetMapField[int](maany2, "Test2")), "0 false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[string](maany2, "Test")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[string](maany2, "Test2")), "false")
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test")), "12 true")
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test2")), "0 false")
}
func main2() {
}
func main() {
}

View File

@@ -53,7 +53,7 @@ func BuildUpdateStatement(q Queryable, tableName string, obj any, idColumn strin
return "", nil, err
}
setClauses = append(setClauses, fmt.Sprintf("(%s = :%s)", columnName, params.Add(val)))
setClauses = append(setClauses, fmt.Sprintf("%s = :%s", columnName, params.Add(val)))
}
}