v0.0.492
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 5m32s

This commit is contained in:
Mike Schwörer 2024-08-02 16:19:21 +02:00
parent 413bf3c848
commit ffffe4bf24
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 35 additions and 6 deletions

9
ginext/jsonFilter.go Normal file
View File

@ -0,0 +1,9 @@
package ginext
import "github.com/gin-gonic/gin"
var jsonFilterKey = "goext.jsonfilter"
func SetJSONFilter(g *gin.Context, filter string) {
g.Set(jsonFilterKey, filter)
}

View File

@ -15,7 +15,7 @@ type jsonHTTPResponse struct {
func (j jsonHTTPResponse) jsonRenderer(g *gin.Context) json.GoJsonRender {
var f *string
if jsonfilter := g.GetString("goext.jsonfilter"); jsonfilter != "" {
if jsonfilter := g.GetString(jsonFilterKey); jsonfilter != "" {
f = &jsonfilter
}
return json.GoJsonRender{Data: j.data, NilSafeSlices: true, NilSafeMaps: true, Filter: f}

View File

@ -57,7 +57,7 @@ func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper
}
func (w *GinRoutesWrapper) WithJSONFilter(filter string) *GinRoutesWrapper {
return w.Use(func(g *gin.Context) { g.Set("goext.jsonfilter", filter) })
return w.Use(func(g *gin.Context) { g.Set(jsonFilterKey, filter) })
}
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
@ -112,7 +112,7 @@ func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder {
}
func (w *GinRouteBuilder) WithJSONFilter(filter string) *GinRouteBuilder {
return w.Use(func(g *gin.Context) { g.Set("goext.jsonfilter", filter) })
return w.Use(func(g *gin.Context) { g.Set(jsonFilterKey, filter) })
}
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {

View File

@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.491"
const GoextVersion = "0.0.492"
const GoextVersionTimestamp = "2024-07-31T00:15:09+0200"
const GoextVersionTimestamp = "2024-08-02T16:19:21+0200"

View File

@ -788,7 +788,7 @@ FieldLoop:
if f.omitEmpty && isEmptyValue(fv) {
continue
} else if opts.filter != nil && len(f.jsonfilter) > 0 && !f.jsonfilter.Contains(*opts.filter) {
} else if opts.filter != nil && !matchesJSONFilter(f.jsonfilter, *opts.filter) {
continue
}
e.WriteByte(next)
@ -808,6 +808,26 @@ FieldLoop:
}
}
func matchesJSONFilter(filter jsonfilter, value string) bool {
if len(filter) == 0 {
return true
}
if len(filter) == 1 && filter[0] == "-" {
return false
}
if filter.Contains(value) {
return true
}
if filter.Contains("*") {
return true
}
return false
}
func newStructEncoder(t reflect.Type, tagkey string) encoderFunc {
se := structEncoder{fields: cachedTypeFields(t, tagkey)}
return se.encode