@@ -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
3838func (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 )
0 commit comments