DYN-166 add jsonfilter to json library
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 47s

This commit is contained in:
Timo Vetter 2023-10-05 10:45:09 +02:00
parent 98486842ae
commit b5cd116219
3 changed files with 48 additions and 15 deletions

View File

@ -174,9 +174,9 @@ type IndentOpt struct {
// MarshalSafeCollections is like Marshal except it will marshal nil maps and // MarshalSafeCollections is like Marshal except it will marshal nil maps and
// slices as '{}' and '[]' respectfully instead of 'null' // slices as '{}' and '[]' respectfully instead of 'null'
func MarshalSafeCollections(v interface{}, nilSafeSlices bool, nilSafeMaps bool, indent *IndentOpt) ([]byte, error) { func MarshalSafeCollections(v interface{}, nilSafeSlices bool, nilSafeMaps bool, indent *IndentOpt, filter *string) ([]byte, error) {
e := &encodeState{} e := &encodeState{}
err := e.marshal(v, encOpts{escapeHTML: true, nilSafeSlices: nilSafeSlices, nilSafeMaps: nilSafeMaps}) err := e.marshal(v, encOpts{escapeHTML: true, nilSafeSlices: nilSafeSlices, nilSafeMaps: nilSafeMaps, filter: filter})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -393,6 +393,9 @@ type encOpts struct {
nilSafeSlices bool nilSafeSlices bool
// nilSafeMaps marshals a nil maps '{}' instead of 'null' // nilSafeMaps marshals a nil maps '{}' instead of 'null'
nilSafeMaps bool nilSafeMaps bool
// filter matches jsonfilter tag of struct
// marshals if no jsonfilter is set or otherwise if jsonfilter has the filter value
filter *string
} }
type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts) type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
@ -777,6 +780,8 @@ FieldLoop:
if f.omitEmpty && isEmptyValue(fv) { if f.omitEmpty && isEmptyValue(fv) {
continue continue
} else if opts.filter != nil && len(f.jsonfilter) > 0 && !f.jsonfilter.Contains(*opts.filter) {
continue
} }
e.WriteByte(next) e.WriteByte(next)
next = ',' next = ','
@ -1220,15 +1225,28 @@ type field struct {
nameNonEsc string // `"` + name + `":` nameNonEsc string // `"` + name + `":`
nameEscHTML string // `"` + HTMLEscape(name) + `":` nameEscHTML string // `"` + HTMLEscape(name) + `":`
tag bool tag bool
index []int index []int
typ reflect.Type typ reflect.Type
omitEmpty bool omitEmpty bool
quoted bool jsonfilter jsonfilter
quoted bool
encoder encoderFunc encoder encoderFunc
} }
// jsonfilter stores the value of the jsonfilter struct tag
type jsonfilter []string
func (j jsonfilter) Contains(t string) bool {
for _, tag := range j {
if t == tag {
return true
}
}
return false
}
// byIndex sorts field by index sequence. // byIndex sorts field by index sequence.
type byIndex []field type byIndex []field
@ -1304,6 +1322,13 @@ func typeFields(t reflect.Type) structFields {
if !isValidTag(name) { if !isValidTag(name) {
name = "" name = ""
} }
var jsonfilter []string
jsonfilterTag := sf.Tag.Get("jsonfilter")
if isValidTag(jsonfilterTag) {
jsonfilter = strings.Split(jsonfilterTag, ",")
}
index := make([]int, len(f.index)+1) index := make([]int, len(f.index)+1)
copy(index, f.index) copy(index, f.index)
index[len(f.index)] = i index[len(f.index)] = i
@ -1334,12 +1359,13 @@ func typeFields(t reflect.Type) structFields {
name = sf.Name name = sf.Name
} }
field := field{ field := field{
name: name, name: name,
tag: tagged, tag: tagged,
index: index, index: index,
typ: ft, typ: ft,
omitEmpty: opts.Contains("omitempty"), omitEmpty: opts.Contains("omitempty"),
quoted: quoted, jsonfilter: jsonfilter,
quoted: quoted,
} }
field.nameBytes = []byte(field.name) field.nameBytes = []byte(field.name)
field.equalFold = foldFunc(field.nameBytes) field.equalFold = foldFunc(field.nameBytes)

View File

@ -1253,6 +1253,10 @@ func TestMarshalSafeCollections(t *testing.T) {
nilMapStruct struct { nilMapStruct struct {
NilMap map[string]interface{} `json:"nil_map"` NilMap map[string]interface{} `json:"nil_map"`
} }
testWithFilter struct {
Test1 string `json:"test1" jsonfilter:"FILTERONE"`
Test2 string `json:"test2" jsonfilter:"FILTERTWO"`
}
) )
tests := []struct { tests := []struct {
@ -1271,10 +1275,12 @@ func TestMarshalSafeCollections(t *testing.T) {
{map[string]interface{}{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"}, {map[string]interface{}{"1": 1, "2": 2, "3": 3}, "{\"1\":1,\"2\":2,\"3\":3}"},
{pNilMap, "null"}, {pNilMap, "null"},
{nilMapStruct{}, "{\"nil_map\":{}}"}, {nilMapStruct{}, "{\"nil_map\":{}}"},
{testWithFilter{}, "{\"test1\":\"\"}"},
} }
filter := "FILTERONE"
for i, tt := range tests { for i, tt := range tests {
b, err := MarshalSafeCollections(tt.in, true, true, nil) b, err := MarshalSafeCollections(tt.in, true, true, nil, &filter)
if err != nil { if err != nil {
t.Errorf("test %d, unexpected failure: %v", i, err) t.Errorf("test %d, unexpected failure: %v", i, err)
} }

View File

@ -17,6 +17,7 @@ type GoJsonRender struct {
NilSafeSlices bool NilSafeSlices bool
NilSafeMaps bool NilSafeMaps bool
Indent *IndentOpt Indent *IndentOpt
Filter *string
} }
func (r GoJsonRender) Render(w http.ResponseWriter) error { func (r GoJsonRender) Render(w http.ResponseWriter) error {
@ -25,7 +26,7 @@ func (r GoJsonRender) Render(w http.ResponseWriter) error {
header["Content-Type"] = []string{"application/json; charset=utf-8"} header["Content-Type"] = []string{"application/json; charset=utf-8"}
} }
jsonBytes, err := MarshalSafeCollections(r.Data, r.NilSafeSlices, r.NilSafeMaps, r.Indent) jsonBytes, err := MarshalSafeCollections(r.Data, r.NilSafeSlices, r.NilSafeMaps, r.Indent, r.Filter)
if err != nil { if err != nil {
panic(err) panic(err)
} }