From ffffe4bf24a47f74d075ef3cd90828dbcc260786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 2 Aug 2024 16:19:21 +0200 Subject: [PATCH] v0.0.492 --- ginext/jsonFilter.go | 9 +++++++++ ginext/responseJson.go | 2 +- ginext/routes.go | 4 ++-- goextVersion.go | 4 ++-- gojson/encode.go | 22 +++++++++++++++++++++- 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 ginext/jsonFilter.go diff --git a/ginext/jsonFilter.go b/ginext/jsonFilter.go new file mode 100644 index 0000000..b1868a2 --- /dev/null +++ b/ginext/jsonFilter.go @@ -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) +} diff --git a/ginext/responseJson.go b/ginext/responseJson.go index 5832df9..9b070c3 100644 --- a/ginext/responseJson.go +++ b/ginext/responseJson.go @@ -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} diff --git a/ginext/routes.go b/ginext/routes.go index acc6365..bac2558 100644 --- a/ginext/routes.go +++ b/ginext/routes.go @@ -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) { diff --git a/goextVersion.go b/goextVersion.go index deae0ed..ef76882 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -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" diff --git a/gojson/encode.go b/gojson/encode.go index aca278f..8b0778d 100644 --- a/gojson/encode.go +++ b/gojson/encode.go @@ -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