Skip to content

Commit f126229

Browse files
committed
🐛 修复传入数组指针时引发PANIC的BUG, 修复递归时传值导致指针丢失的BUG, 修正部分测试用例
1 parent 6aadc4d commit f126229

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

filter/example_test.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,44 @@ func ExampleSelect() {
2222
user := User{ID: 1, Name: &name, Age: 21, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
2323
article := Select("article", &user) //尽量传指针
2424
null := Select("null", &user)
25-
user.Name = nil
25+
//user.Name = nil
2626
profile := Select("profile", &user)
2727
articleJSON, _ := json.Marshal(article)
2828
fmt.Println(string(articleJSON))
2929
fmt.Println(profile) //可以直接打印,打印会直接输出过滤后的json
3030
fmt.Println(null)
3131

3232
//Output:
33-
//{"id":1,"name":"小北","tags":[{"icon":"icon"},{"icon":"icon"}]}
33+
//{"age":21,"id":1,"name":"小北","tags":[{"icon":"icon"},{"icon":"icon"}]}
3434
//{"age":21,"id":1,"name":"小北","tags":[{"name":"foo"},{"name":"bar"}]}
3535
//{"id":1}
3636
}
3737

3838
func (a *Article) GetHot() {
3939

4040
}
41-
func ExampleOmit() {
42-
type (
43-
Tag struct {
44-
Icon string `json:"icon,omit(article)"`
45-
Name string `json:"name,omit(profile)"`
46-
}
47-
Articles struct {
48-
Password int `json:"password,omit($any)"` //$any表示任何场景都排除该字段
49-
Tags []Tag `json:"tags"`
50-
Hot int `json:"hot,select(img),func(GetHot)"` //热度 过滤时会调用GetHot方法获取该字段的值
51-
Like int `json:"-"`
52-
Collect int `json:"-"`
53-
}
54-
)
5541

56-
//func (a Articles) GetHot() int { //这个方法里可以对字段进行处理,处理后可以返回一个任意值
57-
// return a.Like + a.Collect
58-
//}
42+
type (
43+
ExampleOmitTag struct {
44+
Icon string `json:"icon,omit(article)"`
45+
Name string `json:"name,omit(profile)"`
46+
}
47+
ExampleOmitArticles struct {
48+
Password int `json:"password,omit($any)"` //$any表示任何场景都排除该字段
49+
Tags []ExampleOmitTag `json:"tags"`
50+
Hot int `json:"hot,select(img),func(GetHot)"` //热度 过滤时会调用GetHot方法获取该字段的值
51+
Like int `json:"-"`
52+
Collect int `json:"-"`
53+
}
54+
)
55+
56+
func (a ExampleOmitArticles) GetHot() int { //这个方法里可以对字段进行处理,处理后可以返回一个任意值
57+
return a.Like + a.Collect
58+
}
59+
60+
func ExampleOmit() {
5961

60-
articles := Articles{Like: 100, Collect: 20, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
62+
articles := ExampleOmitArticles{Like: 100, Collect: 20, Tags: []ExampleOmitTag{{"icon", "foo"}, {"icon", "bar"}}}
6163
article := Omit("article", &articles) //尽量传指针,不传指针func选择器中的用指针接收的方法无法被调用
6264
profile := Omit("profile", &articles)
6365
articleJSON, _ := json.Marshal(article)

filter/filter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package filter
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
47

58
type Filter struct {
69
node *fieldNodeTree
@@ -16,7 +19,7 @@ func jsonFilter(selectScene string, el interface{}, isSelect bool) Filter {
1619
Key: "",
1720
ParentNode: nil,
1821
}
19-
tree.parseAny("", selectScene, el, isSelect)
22+
tree.parseAny("", selectScene, reflect.ValueOf(el), isSelect)
2023
return Filter{
2124
node: tree,
2225
}

filter/parser.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ type tagInfo struct {
1010
omit bool //表示这个字段忽略
1111
}
1212

13-
func (t *fieldNodeTree) parseAny(key, scene string, el interface{}, isSelect bool) {
14-
typeOf := reflect.TypeOf(el)
15-
valueOf := reflect.ValueOf(el)
13+
func (t *fieldNodeTree) parseAny(key, scene string, valueOf reflect.Value, isSelect bool) {
14+
typeOf := valueOf.Type()
1615
TakePointerValue: //取指针的值
1716
switch typeOf.Kind() {
1817
case reflect.Ptr: //如果是指针类型则取地址重新判断类型
18+
valueOf = valueOf.Elem()
1919
typeOf = typeOf.Elem()
2020
goto TakePointerValue
2121
case reflect.Struct:
@@ -143,7 +143,7 @@ takeVMap:
143143
nodeTree.IsNil = true
144144
t.AddChild(nodeTree)
145145
} else {
146-
nodeTree.parseAny(k, scene, val.Interface(), isSelect)
146+
nodeTree.parseAny(k, scene, val, isSelect)
147147
t.AddChild(nodeTree)
148148
}
149149
}
@@ -248,7 +248,7 @@ TakeValueOfPointerValue: //这里主要是考虑到有可能用的不是一级
248248
}
249249
}
250250

251-
tree.parseAny(tag.UseFieldName, scene, value.Interface(), isSelect)
251+
tree.parseAny(tag.UseFieldName, scene, value, isSelect)
252252

253253
if t.IsAnonymous {
254254
t.AnonymousAddChild(tree)
@@ -310,7 +310,7 @@ func parserSliceOrArray(typeOf reflect.Type, valueOf reflect.Value, t *fieldNode
310310
node.IsNil = true
311311
t.AddChild(node)
312312
} else {
313-
node.parseAny("", scene, val.Interface(), isSelect)
313+
node.parseAny("", scene, val, isSelect)
314314
t.AddChild(node)
315315
}
316316
}

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ module github.com/liu-cn/json-filter
22

33
go 1.17
44

5-
require (
6-
github.com/liu-cn/pkg v0.0.0-20221218135636-865c46b102ea
7-
)
5+
require github.com/liu-cn/pkg v0.0.0-20221218135636-865c46b102ea

test/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func newUs() Us {
6464
}
6565

6666
func main() {
67-
6867
//var bb = []byte(`{"a":"1"}`)
6968
u := Us{
7069
BB: [3]byte{1, 2, 4},
@@ -75,7 +74,9 @@ func main() {
7574
Avatar: []byte("uuid"),
7675
Avatar2: []byte("uuid2"),
7776
}
78-
77+
list := []Us{u, u, u}
78+
fmt.Println(filter.Omit("1", &list))
79+
//return
7980
fmt.Println(mustJson(u))
8081
//fmt.Println(filter.Omit("h", u))
8182
//fmt.Println(filter.Select("all", u))

0 commit comments

Comments
 (0)