Skip to content

Commit 6aadc4d

Browse files
committed
添加func选择器进行字段处理,添加使用案例,更新文档
1 parent 347174e commit 6aadc4d

File tree

4 files changed

+155
-19
lines changed

4 files changed

+155
-19
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ golang的json字段过滤器,随意选择字段,随意输出指定结构体
615615

616616
[omit选择器排除过滤](#omit选择器排除过滤)
617617

618+
[func选择器自定义方法进行字段处理](#func选择器自定义方法进行字段处理)
619+
618620
[$any标识符任意场景解析](#$any标识符任意场景解析)
619621

620622
[过滤后的Filter结构体的方法](#过滤后的Filter结构体的方法)
@@ -764,6 +766,51 @@ Age int `json:"age,omitempty,select(article|profile)"` //为0忽略
764766
```
765767

766768

769+
### func选择器自定义方法进行字段处理
770+
```go
771+
type Image struct {
772+
Url []byte `json:"url,select(img),func(GetUrl)"`
773+
Path string `json:"path,select(img),func(GetImagePath)"`
774+
Name string `json:"name"`
775+
Hot int `json:"hot,select(img),func(GetHot)"` //热度
776+
Like int
777+
Collect int
778+
Forward int
779+
}
780+
781+
func (i Image) GetUrl() string {
782+
return string(i.Url) + ".jpg"
783+
}
784+
785+
// 指针接收器的方法只有在过滤时候传送指针才可以保证此方法被正常调用
786+
func (i *Image) GetImagePath() string {
787+
return i.Path + i.Name + ".png"
788+
}
789+
790+
// 计算热度
791+
func (i Image) GetHot() int {
792+
return i.Like * i.Forward * i.Collect
793+
}
794+
795+
func TestFunc(t *testing.T) {
796+
img := Image{
797+
Url: []byte("url"),
798+
Path: "path",
799+
Name: "_golang",
800+
Collect: 10,
801+
Like: 100,
802+
Forward: 50,
803+
}
804+
fmt.Println(filter.Select("img", img))
805+
//{"hot":50000,"path":"path","url":"url.jpg"}
806+
807+
fmt.Println(filter.Select("img", &img)) //只有传入指针才可以调用绑定指针接收器方法
808+
//{"hot":50000,"path":"path_golang.png","url":"url.jpg"}
809+
}
810+
811+
```
812+
813+
767814

768815
#### $any标识符任意场景解析
769816

example/func_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/liu-cn/json-filter/filter"
6+
"testing"
7+
)
8+
9+
type Image struct {
10+
Url []byte `json:"url,select(img),func(GetUrl)"`
11+
Path string `json:"path,select(img),func(GetImagePath)"`
12+
Name string `json:"name"`
13+
Hot int `json:"hot,select(img),func(GetHot)"` //热度
14+
Like int
15+
Collect int
16+
Forward int
17+
}
18+
19+
func (i Image) GetUrl() string {
20+
return string(i.Url) + ".jpg"
21+
}
22+
23+
// 指针接收器的方法只有在过滤时候传送指针才可以保证此方法被正常调用
24+
func (i *Image) GetImagePath() string {
25+
return i.Path + i.Name + ".png"
26+
}
27+
28+
// 计算热度
29+
func (i Image) GetHot() int {
30+
return i.Like * i.Forward * i.Collect
31+
}
32+
33+
func TestFunc(t *testing.T) {
34+
img := Image{
35+
Url: []byte("url"),
36+
Path: "path",
37+
Name: "_golang",
38+
Collect: 10,
39+
Like: 100,
40+
Forward: 50,
41+
}
42+
fmt.Println(filter.Select("img", img))
43+
//{"hot":50000,"path":"path","url":"url.jpg"}
44+
45+
fmt.Println(filter.Select("img", &img)) //只有传入指针才可以调用绑定指针接收器方法
46+
//{"hot":50000,"path":"path_golang.png","url":"url.jpg"}
47+
}

filter/example_test.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func ExampleSelect() {
2020
)
2121
name := "小北"
2222
user := User{ID: 1, Name: &name, Age: 21, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
23-
article := Select("article", &user) //传指针或值均可
24-
null := Select("null", user)
23+
article := Select("article", &user) //尽量传指针
24+
null := Select("null", &user)
2525
user.Name = nil
26-
profile := Select("profile", user)
26+
profile := Select("profile", &user)
2727
articleJSON, _ := json.Marshal(article)
2828
fmt.Println(string(articleJSON))
2929
fmt.Println(profile) //可以直接打印,打印会直接输出过滤后的json
@@ -35,27 +35,36 @@ func ExampleSelect() {
3535
//{"id":1}
3636
}
3737

38-
func ExampleOmit() {
38+
func (a *Article) GetHot() {
3939

40+
}
41+
func ExampleOmit() {
4042
type (
4143
Tag struct {
4244
Icon string `json:"icon,omit(article)"`
4345
Name string `json:"name,omit(profile)"`
4446
}
45-
User struct {
46-
Age int `json:"age"`
47+
Articles struct {
4748
Password int `json:"password,omit($any)"` //$any表示任何场景都排除该字段
4849
Tags []Tag `json:"tags"`
50+
Hot int `json:"hot,select(img),func(GetHot)"` //热度 过滤时会调用GetHot方法获取该字段的值
51+
Like int `json:"-"`
52+
Collect int `json:"-"`
4953
}
5054
)
51-
user := User{Age: 21, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
52-
article := Omit("article", &user) //传指针或值均可
53-
profile := Omit("profile", user)
55+
56+
//func (a Articles) GetHot() int { //这个方法里可以对字段进行处理,处理后可以返回一个任意值
57+
// return a.Like + a.Collect
58+
//}
59+
60+
articles := Articles{Like: 100, Collect: 20, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
61+
article := Omit("article", &articles) //尽量传指针,不传指针func选择器中的用指针接收的方法无法被调用
62+
profile := Omit("profile", &articles)
5463
articleJSON, _ := json.Marshal(article)
5564
fmt.Println(string(articleJSON))
5665
fmt.Println(profile) //可以直接打印,打印会直接输出过滤后的json
5766

5867
//Output:
59-
//{"age":21,"tags":[{"name":"foo"},{"name":"bar"}]}
60-
//{"age":21,"tags":[{"icon":"icon"},{"icon":"icon"}]}
68+
//{"hot":120,"tags":[{"name":"foo"},{"name":"bar"}]}
69+
//{"hot":120,"tags":[{"icon":"icon"},{"icon":"icon"}]}
6170
}

test/main.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,46 @@ func main() {
7676
Avatar2: []byte("uuid2"),
7777
}
7878

79-
//fmt.Println(mustJson(u))
79+
fmt.Println(mustJson(u))
8080
//fmt.Println(filter.Omit("h", u))
81-
fmt.Println(filter.Select("all", u))
82-
fmt.Println(filter.Omit("all", u))
83-
fmt.Println(filter.Select("all", &u))
84-
fmt.Println(filter.Omit("all", &u))
85-
TestSlice()
86-
TestMap()
87-
TestU()
81+
//fmt.Println(filter.Select("all", u))
82+
//fmt.Println(filter.Omit("all", u))
83+
//fmt.Println(filter.Select("all", &u))
84+
//fmt.Println(filter.Omit("all", &u))
85+
//TestSlice()
86+
//TestMap()
87+
//TestU()
88+
89+
ExampleOmit()
90+
}
91+
92+
func ExampleOmit() {
93+
type (
94+
Tag struct {
95+
Icon string `json:"icon,omit(article)"`
96+
Name string `json:"name,omit(profile)"`
97+
}
98+
Articles struct {
99+
Password int `json:"password,omit($any)"` //$any表示任何场景都排除该字段
100+
Tags []Tag `json:"tags"`
101+
Hot int `json:"hot,select(img),func(GetHot)"` //热度 过滤时会调用GetHot方法获取该字段的值
102+
Like int `json:"-"`
103+
Collect int `json:"-"`
104+
}
105+
)
106+
107+
//func (a Articles) GetHot() int { //这个方法里可以对字段进行处理,处理后可以返回一个任意值
108+
// return a.Like + a.Collect
109+
//}
110+
111+
articles := Articles{Like: 100, Collect: 20, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
112+
article := filter.Omit("article", &articles) //尽量传指针,不传指针func选择器中的用指针接收的方法无法被调用
113+
profile := filter.Omit("profile", &articles)
114+
articleJSON, _ := json.Marshal(article)
115+
fmt.Println(string(articleJSON))
116+
fmt.Println(profile) //可以直接打印,打印会直接输出过滤后的json
117+
118+
//Output:
119+
//{"hot":120,"tags":[{"name":"foo"},{"name":"bar"}]}
120+
//{"hot":120,"tags":[{"icon":"icon"},{"icon":"icon"}]}
88121
}

0 commit comments

Comments
 (0)