Skip to content

Commit c16161e

Browse files
authored
Merge pull request #8 from h3110w0r1d-y/main
修复传入数组指针时引发PANIC的BUG, 修复递归时传值导致指针丢失的BUG, 修正部分测试用例
2 parents 6aadc4d + 25dfb33 commit c16161e

File tree

5 files changed

+38
-53
lines changed

5 files changed

+38
-53
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: 7 additions & 26 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() {
18-
case reflect.Ptr: //如果是指针类型则取地址重新判断类型
17+
case reflect.Ptr: //如果是指针类型则取值重新判断类型
18+
valueOf = valueOf.Elem()
1919
typeOf = typeOf.Elem()
2020
goto TakePointerValue
2121
case reflect.Struct:
@@ -110,12 +110,6 @@ func getSelectTag(scene string, pkgInfo string, i int, typeOf reflect.Type) tagI
110110
}
111111

112112
func parserMap(valueOf reflect.Value, t *fieldNodeTree, scene string, isSelect bool) {
113-
114-
takeVMap:
115-
if valueOf.Kind() == reflect.Ptr {
116-
valueOf = valueOf.Elem()
117-
goto takeVMap
118-
}
119113
keys := valueOf.MapKeys()
120114
if len(keys) == 0 { //空map情况下解析为{}
121115
t.Val = struct{}{}
@@ -143,7 +137,7 @@ takeVMap:
143137
nodeTree.IsNil = true
144138
t.AddChild(nodeTree)
145139
} else {
146-
nodeTree.parseAny(k, scene, val.Interface(), isSelect)
140+
nodeTree.parseAny(k, scene, val, isSelect)
147141
t.AddChild(nodeTree)
148142
}
149143
}
@@ -165,18 +159,6 @@ func parserBaseType(valueOf reflect.Value, t *fieldNodeTree, key string) {
165159
}
166160

167161
func parserStruct(typeOf reflect.Type, valueOf reflect.Value, t *fieldNodeTree, scene string, key string, isSelect bool) {
168-
169-
TakeValueOfPointerValue: //这里主要是考虑到有可能用的不是一级指针,如果是***int 等多级指针就需要不断的取值
170-
if valueOf.Kind() == reflect.Ptr {
171-
if valueOf.IsNil() {
172-
t.IsNil = true
173-
return
174-
} else {
175-
valueOf = valueOf.Elem()
176-
goto TakeValueOfPointerValue
177-
}
178-
}
179-
180162
if valueOf.CanConvert(timeTypes) { //是time.Time类型或者底层是time.Time类型
181163
t.Key = key
182164
t.Val = valueOf.Interface()
@@ -248,7 +230,7 @@ TakeValueOfPointerValue: //这里主要是考虑到有可能用的不是一级
248230
}
249231
}
250232

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

253235
if t.IsAnonymous {
254236
t.AnonymousAddChild(tree)
@@ -263,7 +245,6 @@ TakeValueOfPointerValue: //这里主要是考虑到有可能用的不是一级
263245
}
264246

265247
func parserSliceOrArray(typeOf reflect.Type, valueOf reflect.Value, t *fieldNodeTree, scene string, key string, isSelect bool) {
266-
267248
val1 := valueOf.Interface()
268249
ok := valueOf.CanConvert(byteTypes)
269250
if ok {
@@ -310,7 +291,7 @@ func parserSliceOrArray(typeOf reflect.Type, valueOf reflect.Value, t *fieldNode
310291
node.IsNil = true
311292
t.AddChild(node)
312293
} else {
313-
node.parseAny("", scene, val.Interface(), isSelect)
294+
node.parseAny("", scene, val, isSelect)
314295
t.AddChild(node)
315296
}
316297
}

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)