From 561aa2f5ee59e0b598973ddb124085ab1979463b Mon Sep 17 00:00:00 2001 From: v_llxjliu Date: Wed, 5 Jun 2024 15:50:32 +0800 Subject: [PATCH] =?UTF-8?q?add:=20Map=20Slice=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/with_value_test.go | 70 ++++++++++++++++++++++++++++++++++++++ filter/filter.go | 23 ++++++++++--- 2 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 example/with_value_test.go diff --git a/example/with_value_test.go b/example/with_value_test.go new file mode 100644 index 0000000..efe11f4 --- /dev/null +++ b/example/with_value_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/liu-cn/json-filter/filter" + "testing" +) + +func TestMapWithValue(t *testing.T) { + type F struct { + A string `json:"a,select(a)"` + } + //filterMap:=filter.Select("a",&F{A: "a"}).(filter.Filter).Map() + filterMap := filter.SelectMarshal("a", &F{A: "a"}).Map() //跟上面写法等价 + filterMap["b"] = "b" + filterMap["cc"] = struct { + CC string + }{ + CC: "CC", + } + + marshal, err := json.Marshal(filterMap) + if err != nil { + panic(err) + } + + fmt.Println(string(marshal)) + //{ + // "a": "a", + // "b": "b", + // "cc": {"CC": "CC"} + //} + +} + +func TestSliceWithValue(t *testing.T) { + type F struct { + A string `json:"a,select(a)"` + } + + list := []F{ + F{A: "a"}, + } + + //slices:=filter.Select("a",&F{A: "a"}).(filter.Filter).Slice() + slices := filter.SelectMarshal("a", list).Slice() //跟上面写法等价 + + slices = append(slices, F{A: "b"}) + slices = append(slices, F{A: "c"}) + + marshal, err := json.Marshal(slices) + if err != nil { + panic(err) + } + + fmt.Println(string(marshal)) + //[ + // { + // "a": "a" + // }, + // { + // "a": "b" + // }, + // { + // "a": "c" + // } + //] + +} diff --git a/filter/filter.go b/filter/filter.go index 4054d1c..623e5f2 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -36,14 +36,12 @@ func EnableCache(enable bool) { enableCache = enable } -// Deprecated -// SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。 +// SelectMarshal 跟Select方法等价,只是这个会返回具体对象,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。 func SelectMarshal(selectScene string, el interface{}) Filter { return jsonFilter(selectScene, el, true) } -// Deprecated -// OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉 +// OmitMarshal 跟Omit方法等价,只是这个会返回具体对象,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉 func OmitMarshal(omitScene string, el interface{}) Filter { return jsonFilter(omitScene, el, false) } @@ -83,6 +81,23 @@ func (f Filter) String() string { return json } +// Map 过滤后的map结构 +func (f Filter) Map() map[string]interface{} { + return f.node.Map() +} + +// Slice 过滤后的切片结构 +func (f Filter) Slice() []interface{} { + slices := make([]interface{}, 0, len(f.node.Children)) + for i := 0; i < len(f.node.Children); i++ { + v, ok := f.node.Children[i].GetValue() + if ok { + slices = append(slices, v) + } + } + return slices +} + //// SelectCache 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示 //func SelectCache(selectScene string, el interface{}) interface{} { // return jsonFilterCache(selectScene, el, true)