Skip to content

Commit 561aa2f

Browse files
author
v_llxjliu
committed
add: Map Slice 方法
1 parent 8e460b1 commit 561aa2f

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

example/with_value_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/liu-cn/json-filter/filter"
7+
"testing"
8+
)
9+
10+
func TestMapWithValue(t *testing.T) {
11+
type F struct {
12+
A string `json:"a,select(a)"`
13+
}
14+
//filterMap:=filter.Select("a",&F{A: "a"}).(filter.Filter).Map()
15+
filterMap := filter.SelectMarshal("a", &F{A: "a"}).Map() //跟上面写法等价
16+
filterMap["b"] = "b"
17+
filterMap["cc"] = struct {
18+
CC string
19+
}{
20+
CC: "CC",
21+
}
22+
23+
marshal, err := json.Marshal(filterMap)
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
fmt.Println(string(marshal))
29+
//{
30+
// "a": "a",
31+
// "b": "b",
32+
// "cc": {"CC": "CC"}
33+
//}
34+
35+
}
36+
37+
func TestSliceWithValue(t *testing.T) {
38+
type F struct {
39+
A string `json:"a,select(a)"`
40+
}
41+
42+
list := []F{
43+
F{A: "a"},
44+
}
45+
46+
//slices:=filter.Select("a",&F{A: "a"}).(filter.Filter).Slice()
47+
slices := filter.SelectMarshal("a", list).Slice() //跟上面写法等价
48+
49+
slices = append(slices, F{A: "b"})
50+
slices = append(slices, F{A: "c"})
51+
52+
marshal, err := json.Marshal(slices)
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
fmt.Println(string(marshal))
58+
//[
59+
// {
60+
// "a": "a"
61+
// },
62+
// {
63+
// "a": "b"
64+
// },
65+
// {
66+
// "a": "c"
67+
// }
68+
//]
69+
70+
}

filter/filter.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ func EnableCache(enable bool) {
3636
enableCache = enable
3737
}
3838

39-
// Deprecated
40-
// SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
39+
// SelectMarshal 跟Select方法等价,只是这个会返回具体对象,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
4140
func SelectMarshal(selectScene string, el interface{}) Filter {
4241
return jsonFilter(selectScene, el, true)
4342
}
4443

45-
// Deprecated
46-
// OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
44+
// OmitMarshal 跟Omit方法等价,只是这个会返回具体对象,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
4745
func OmitMarshal(omitScene string, el interface{}) Filter {
4846
return jsonFilter(omitScene, el, false)
4947
}
@@ -83,6 +81,23 @@ func (f Filter) String() string {
8381
return json
8482
}
8583

84+
// Map 过滤后的map结构
85+
func (f Filter) Map() map[string]interface{} {
86+
return f.node.Map()
87+
}
88+
89+
// Slice 过滤后的切片结构
90+
func (f Filter) Slice() []interface{} {
91+
slices := make([]interface{}, 0, len(f.node.Children))
92+
for i := 0; i < len(f.node.Children); i++ {
93+
v, ok := f.node.Children[i].GetValue()
94+
if ok {
95+
slices = append(slices, v)
96+
}
97+
}
98+
return slices
99+
}
100+
86101
//// SelectCache 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示
87102
//func SelectCache(selectScene string, el interface{}) interface{} {
88103
// return jsonFilterCache(selectScene, el, true)

0 commit comments

Comments
 (0)