Skip to content

Commit 49aaacf

Browse files
committed
更新文档,默认启用缓存,
1 parent cb98af7 commit 49aaacf

14 files changed

+372
-386
lines changed

README.md

+36-119
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ func main() {
101101
//{"uid":1,"nickname":"boyan","avatar":"avatar","sex":1,"vip_end_time":"2023-03-06T23:11:22.622693+08:00","price":"999.9"}
102102

103103

104-
//usage:filter.SelectMarshal("select case",This can be:slice/array/struct/pointer/map)
105-
fmt.Println(filter.SelectMarshal("article", user).MustJSON()) //The following is the JSON filtered by JSON filter. This output is the JSON under the article interface
104+
//usage:filter.Select("select case",This can be:slice/array/struct/pointer/map)
105+
fmt.Println(filter.Select("article", user)) //The following is the JSON filtered by JSON filter. This output is the JSON under the article interface
106106
//{"avatar":"avatar","nickname":"boyan","uid":1}
107107

108-
fmt.Println(filter.SelectMarshal("profile", user).MustJSON()) //profile result
108+
fmt.Println(filter.Select("profile", user)) //profile result
109109
//{"nickname":"boyan","price":"999.9","sex":1,"vip_end_time":"2023-03-06T23:31:28.636529+08:00"}
110110
}
111111

@@ -146,7 +146,7 @@ At this time, you need to add omit (excluded scene | scene 0 | Scene 1 | scene n
146146
At this time, you need to call
147147

148148
```go
149-
f:=filter.OmitMarshal("chat",el) //The nickname field is then excluded.
149+
f:=filter.Omit("chat",el) //The nickname field is then excluded.
150150
```
151151

152152
##### Omitempty zero value ignored
@@ -174,9 +174,9 @@ type User struct {
174174
}
175175

176176
//In this way, no matter any case
177-
SelectMarshal("Whatever case you choose here", user)//No matter what kind of case, the field of uid will be output.
177+
Select("Whatever case you choose here", user)//No matter what kind of case, the field of uid will be output.
178178

179-
OmitMarshal("Whatever case you choose here",user)//The password field is excluded in any scenario.
179+
Omit("Whatever case you choose here",user)//The password field is excluded in any scenario.
180180

181181

182182
```
@@ -185,32 +185,7 @@ OmitMarshal("Whatever case you choose here",user)//The password field is exclude
185185

186186
#### Method of filtering filter structure
187187

188-
##### Filter.Interface
189-
190-
Filtered JSON data structure, (which can be encoded by JSON)
191-
192-
```go
193-
usage method
194-
195-
f:=filter.SelectMarshal("case",el) //el:array/slice/struct/map
196-
197-
A filtered filter structure will be returned. You can call the specified method to obtain the specified data as needed.
198-
199-
f.Interface()---->Return a data structure that has not been parsed. At this time, you can directly use the official JSON Marshal () to serialize into filtered JSON strings.
200-
ps:
201-
f:=filter.SelectMarshal("场景",el)
202-
json.Marshal(f.Interface()) //Equivalent to json.Marshal(filter.SelectMarshal("case",el).Interface())
203-
```
204-
205-
206-
207-
##### Filter.MustJSON
208-
209-
Directly encoded JSON string after filtering
210-
The method of must prefix will not return err. If err is encountered in the process of use, it doesn't matter if it is used in the test. It must be used in the project to ensure that the structure is correct.
211-
212-
```go
213-
fmt.Println(f.MustJSON()) //---> You don't need to go to JSON like that Marshall, because this is the returned JSON string directly
188+
fmt.Println(f) //---> You don't need to go to JSON like that Marshall, because this is the returned JSON string directly
214189

215190
//If you want to use this method safely, use F. JSON () to return a JSON string and err
216191
j, err := f.JSON()
@@ -239,7 +214,7 @@ m := map[string]interface{}{
239214
},
240215
}
241216
242-
fmt.Println(filter.SelectMarshal("article", m).MustJSON())
217+
fmt.Println(filter.Select("article", m))
243218
//{"name":"哈哈","struct":{"avatar":"avatar","nickname":"boyan","uid":1}}
244219
//You can see that the map can also be filtered directly.
245220
@@ -277,13 +252,13 @@ func main() {
277252
},
278253
}
279254

280-
fmt.Println(filter.SelectMarshal("justName", tags).MustJSON())
255+
fmt.Println(filter.Select("justName", tags))
281256
//--->output: [{"name":"c"},{"name":"c++"},{"name":"go"}]
282257

283-
fmt.Println(filter.SelectMarshal("all", tags).MustJSON())
258+
fmt.Println(filter.Select("all", tags))
284259
//--->output: [{"icon":"icon-c","id":1,"name":"c"},{"icon":"icon-c++","id":1,"name":"c++"},{"icon":"icon-go","id":1,"name":"go"}]
285260

286-
fmt.Println(filter.SelectMarshal("chat", tags).MustJSON())
261+
fmt.Println(filter.Select("chat", tags))
287262
//--->output: [{"icon":"icon-c"},{"icon":"icon-c++"},{"icon":"icon-go"}]
288263

289264
}
@@ -321,7 +296,7 @@ func main() {
321296
},
322297
}
323298

324-
articleJson := filter.SelectMarshal("article", article).MustJSON()
299+
articleJson := filter.Select("article", article)
325300
fmt.Println(articleJson)
326301
//output---> {"pageInfo":999,"pageNum":1,"title":"c++从研发到脱发"}
327302
}
@@ -433,8 +408,8 @@ func main() {
433408

434409

435410
// If I only want to add some user information related to the programming language
436-
lang := filter.SelectMarshal("lang", user)
437-
fmt.Println(lang.MustJSON())
411+
lang := filter.Select("lang", user)
412+
fmt.Println(lang)
438413
//{"langAge":[{"name":"c"},{"name":"c++"},{"name":"Go"}],"uid":1}
439414

440415
//format
@@ -454,8 +429,8 @@ func main() {
454429
}
455430

456431
//If I just want to get some field information of uid and all arts under langage, you can do this
457-
lookup := filter.SelectMarshal("lookup", user)
458-
fmt.Println(lookup.MustJSON())
432+
lookup := filter.Select("lookup", user)
433+
fmt.Println(lookup)
459434
//{"langAge":[{"arts":[{"profile":{"c":"clang"},"values":["1","2"]}]},{"arts":[{"profile":{"c++":"cpp"},"values":["cpp1","cpp2"]}]},{"arts":[{"profile":{"Golang":"go"},"values":["Golang","Golang1"]}]}],"uid":1}
460435

461436

@@ -522,12 +497,12 @@ type User struct {
522497

523498
func (u User) ArticleResp() interface{} {
524499
//In this way, when you want to optimize the performance later, you can optimize it here,
525-
return filter.SelectMarshal("article",u).Interface()
500+
return filter.Select("article",u)
526501
}
527502

528503
func (u User) ProfileResp() interface{} {
529504
//In this way, when you want to optimize the performance later, you can optimize it here,
530-
return filter.SelectMarshal("profile",u).Interface()
505+
return filter.Select("profile",u)
531506
}
532507

533508
func (u User) ChatResp() interface{} {
@@ -557,7 +532,7 @@ type User struct {
557532
}
558533

559534
func (u User) FilterProfile() interface{} {
560-
return filter.SelectMarshal("profile", u).Interface()
535+
return filter.Select("profile", u)
561536
}
562537

563538
func main() {
@@ -616,7 +591,7 @@ func UserRes(c *gin.Context) {
616591
Avatar: "avatar",
617592
}
618593

619-
OkWithData(filter.SelectMarshal("profile", user).Interface(), c)
594+
OkWithData(filter.Select("profile", user), c)
620595
}
621596
```
622597

@@ -724,12 +699,11 @@ func main() {
724699
fmt.Println(string(articleBytes)) //以下是通过json-filter 过滤后的json,此输出是article接口下的json
725700
//{"avatar":"avatar","nickname":"boyan","uid":1}
726701

727-
//filter.SelectMarshal.MustJSON() 是一个方便测试查看的方法,先过滤,然后再编码成json字符串返回,有错误直接panic,三部操作一气呵成,适合测试查看等
728-
//下面为了方便演示,将使用SelectMarshal api来进行
729-
fmt.Println(filter.SelectMarshal("article", user).MustJSON()) //以下是通过json-filter 过滤后的json,此输出是article接口下的json
702+
//filter.Select fmt打印的时候会自动打印过滤后的json字符串
703+
fmt.Println(filter.Select("article", user)) //以下是通过json-filter 过滤后的json,此输出是article接口下的json
730704
//{"avatar":"avatar","nickname":"boyan","uid":1}
731705

732-
fmt.Println(filter.SelectMarshal("profile", user).MustJSON()) //profile接口下
706+
fmt.Println(filter.Select("profile", user)) //profile接口下
733707
//{"nickname":"boyan","price":"999.9","sex":1,"vip_end_time":"2023-03-06T23:31:28.636529+08:00"}
734708
}
735709

@@ -775,7 +749,6 @@ omit则反之,标记的字段会被排除。
775749

776750
```go
777751
f:=filter.Omit("chat",el) //这时Nickname字段就被排除掉了。
778-
f:=filter.OmitMarshal("chat",el) //同样是是一气呵成的操作,将结构体编码后返回一个对象,可以选择对象的任意形态
779752
```
780753

781754
##### omitempty零值忽略
@@ -803,46 +776,14 @@ type User struct {
803776
}
804777

805778
//这样无论是任何场景
806-
SelectMarshal("无论这里选择任何场景", user)//无论何种场景都会输出UID的字段。
807-
808-
OmitMarshal("无论这里选择任何场景",user)//无论何种场景都会排除password 字段。
809-
810-
811-
```
812-
779+
Select("无论这里选择任何场景", user)//无论何种场景都会输出UID的字段。
813780

781+
Omit("无论这里选择任何场景",user)//无论何种场景都会排除password 字段。
814782

815-
#### 过滤后的Filter结构体的方法
816783

817-
##### Filter.Interface
818-
819-
过滤后的json数据结构,(可以被json编码)
820-
821-
```go
822-
使用方法
823-
824-
f:=filter.SelectMarshal("场景",el) //el可以是 要过滤的 /结构体/map/切片/数组
825-
826-
会返回一个过滤后的Filter结构体,可以根据需要调用指定的方法来获取指定数据。
827-
828-
f.Interface()---->返回一个还没被解析的数据结构,此时可以直接使用官方的json.Marshal()来序列化成过滤后的json字符串。
829-
ps:
830-
f:=filter.SelectMarshal("场景",el)
831-
json.Marshal(f.Interface()) //等同于json.Marshal(filter.SelectMarshal("场景",el).Interface())
832-
```
784+
f:=filter.Select("场景",要过滤的结构体/map/切片/数组)
833785

834-
835-
836-
##### Filter.MustJSON
837-
838-
过滤后直接编码成的json字符串
839-
840-
Must前缀的方法,不会返回err,如果使用过程中遇到err直接panic,测试使用无所谓,项目里使用一定要保证结构体正确无误。
841-
842-
```go
843-
f:=filter.SelectMarshal("场景",要过滤的结构体/map/切片/数组)
844-
845-
fmt.Println(f.MustJSON()) //---> 就不需要上面那样去json.Marshal 了,因为这样直接就是返回的json字符串
786+
fmt.Println(f) //---> 就不需要上面那样去json.Marshal 了,因为这样直接就是返回的json字符串
846787

847788
//如果想安全的使用这个方法请使用f.JSON() 会返回一个json字符串和err
848789
j, err := f.JSON()
@@ -871,7 +812,7 @@ m := map[string]interface{}{
871812
},
872813
}
873814

874-
fmt.Println(filter.SelectMarshal("article", m).MustJSON())
815+
fmt.Println(filter.Select("article", m))
875816
//{"name":"哈哈","struct":{"avatar":"avatar","nickname":"boyan","uid":1}}
876817
//可以看到map也是可以直接过滤的。
877818

@@ -909,13 +850,13 @@ func main() {
909850
},
910851
}
911852

912-
fmt.Println(filter.SelectMarshal("justName", tags).MustJSON())
853+
fmt.Println(filter.Select("justName", tags))
913854
//--->输出结果: [{"name":"c"},{"name":"c++"},{"name":"go"}]
914855

915-
fmt.Println(filter.SelectMarshal("all", tags).MustJSON())
856+
fmt.Println(filter.Select("all", tags))
916857
//--->输出结果: [{"icon":"icon-c","id":1,"name":"c"},{"icon":"icon-c++","id":1,"name":"c++"},{"icon":"icon-go","id":1,"name":"go"}]
917858

918-
fmt.Println(filter.SelectMarshal("chat", tags).MustJSON())
859+
fmt.Println(filter.Select("chat", tags))
919860
//--->输出结果: [{"icon":"icon-c"},{"icon":"icon-c++"},{"icon":"icon-go"}]
920861

921862
}
@@ -956,7 +897,7 @@ func main() {
956897
},
957898
}
958899

959-
articleJson := filter.SelectMarshal("article", article).MustJSON()
900+
articleJson := filter.Select("article", article)
960901
fmt.Println(articleJson)
961902
//输出结果---> {"pageInfo":999,"pageNum":1,"title":"c++从研发到脱发"}
962903
}
@@ -1069,8 +1010,8 @@ func main() {
10691010

10701011

10711012
// 如果我只想要编程语言相关加上部分用户信息的话
1072-
lang := filter.SelectMarshal("lang", user)
1073-
fmt.Println(lang.MustJSON())
1013+
lang := filter.Select("lang", user)
1014+
fmt.Println(lang)
10741015
//{"langAge":[{"name":"c"},{"name":"c++"},{"name":"Go"}],"uid":1}
10751016

10761017
//格式化后
@@ -1090,8 +1031,8 @@ func main() {
10901031
}
10911032

10921033
//如果我只是想获取uid加上langAge下所有Art的部分字段信息, 你可以这样
1093-
lookup := filter.SelectMarshal("lookup", user)
1094-
fmt.Println(lookup.MustJSON())
1034+
lookup := filter.Select("lookup", user)
1035+
fmt.Println(lookup)
10951036
//{"langAge":[{"arts":[{"profile":{"c":"clang"},"values":["1","2"]}]},{"arts":[{"profile":{"c++":"cpp"},"values":["cpp1","cpp2"]}]},{"arts":[{"profile":{"Golang":"go"},"values":["Golang","Golang1"]}]}],"uid":1}
10961037

10971038

@@ -1233,27 +1174,3 @@ func GetUser(c *gin.Context) {
12331174

12341175
```
12351176

1236-
#### 不想直接被解析为json字符串?
1237-
1238-
你可能不希望直接解析成字符串,希望过滤后再挂在到其他结构体被解析,如果解析成字符串被挂载上去会被当成字符串解析,所以也是支持的。
1239-
1240-
```go
1241-
func OkWithData(data interface{}, c *gin.Context) {
1242-
c.JSON(200, Response{
1243-
Code: 0,
1244-
Msg: "ok",
1245-
Data: data, //这个data应该是一个结构体或者map不应该是已经解析好的json字符串
1246-
})
1247-
}
1248-
1249-
func UserRes(c *gin.Context) {
1250-
user := User{
1251-
UID: 1,
1252-
Sex: 1,
1253-
Avatar: "avatar",
1254-
}
1255-
1256-
OkWithData(filter.Select("profile", user), c)
1257-
}
1258-
```
1259-

filter/cache.go

-1
This file was deleted.

filter/filter.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (f Filter) String() string {
4242
return json
4343
}
4444

45+
// Deprecated
4546
// SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
4647
func SelectMarshal(selectScene string, el interface{}) Filter {
4748
if enableCache {
@@ -88,6 +89,7 @@ func Omit(omitScene string, el interface{}) interface{} {
8889
return omitMarshal(omitScene, el)
8990
}
9091

92+
// Deprecated
9193
// OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
9294
func OmitMarshal(omitScene string, el interface{}) Filter {
9395
if enableCache {
@@ -121,5 +123,5 @@ func omitWithCache(omitScene string, el interface{}) Filter {
121123

122124
// EnableCache 决定是否启用缓存,默认开启(强烈建议,除非万一缓存模式下出现bug,可以关闭缓存退回曾经的无缓存过滤模式),开启缓存后会有30%-40%的性能提升,开启缓存并没有副作用,只是会让结构体的字段tag常驻内存减少tag字符串处理操作
123125
func EnableCache(enable bool) {
124-
enableCache = false
126+
enableCache = enable
125127
}

filter/filter_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,14 @@ var s string
381381
func TestTestCases(t *testing.T) {
382382

383383
t.Run("all", func(t *testing.T) {
384-
filter := SelectMarshal("all", NewTestCases())
385-
fmt.Println(filter.MustJSON())
384+
filter := Select("all", NewTestCases())
385+
fmt.Println(filter)
386386
//{"anonymous_value_name":"anonymous_value_name","article":{"name":"","page_num":0,"page_size":0,"price":"","title":""},"bool":true,"bool_p":true,"byte":1,"byte_p":10,"float32":32.1,"float64":64.1,"float_32_p":320.1,"float_64_p":320.1,"int":100,"int16":16,"int16_p":16,"int32":32,"int32_p":32,"int64":64,"int64_p":64,"int8":8,"int8_p":8,"int_p":100,"interface":"interface","interface_p":"interface p","m_time":"0001-01-01T00:00:00Z","m_time_p":null,"map":{"string":"map val","struct":{"age":0,"name":"hhhhh","struct":{"c_age":0,"c_name":""}}},"map_p":{"map_p":"map val"},"slice_bool":[true,true],"slice_bool_p":[true],"slice_byte":[10],"slice_byte_p":[10],"slice_float_32":[12.7],"slice_float_32_p":[320.1],"slice_float_64":[12.3],"slice_float_64_p":[320.1],"slice_int":[1,2],"slice_int_16":[1,2],"slice_int_16_p":[16],"slice_int_32":[1,2],"slice_int_32_i":[32],"slice_int_64":[1,2],"slice_int_64_i":[64],"slice_int_8":[1,2],"slice_int_8_p":[8],"slice_int_p":[100,100],"slice_interface":["12","13"],"slice_interface_p":["string p","string p"],"slice_map":[{"map1":1},{"map2":2}],"slice_map_p":[{"map1":1},{"map2":2}],"slice_slice_int":[[1,23],[2,3]],"slice_slice_users_case":[[{"age":1,"name":"","struct":{"c_age":0,"c_name":""}}],[{"age":2,"name":"","struct":{"c_age":0,"c_name":""}}]],"slice_string":["slice string","123"],"slice_string_s":["string p","string p"],"slice_struct":[],"slice_struct_p":[],"slice_time":["2022-03-07T16:58:40.546172+08:00","2022-03-07T16:58:40.546172+08:00"],"slice_uint":[1,3],"slice_uint_16":[1,3],"slice_uint_16_p":[16],"slice_uint_32":[1,2],"slice_uint_32_p":[32],"slice_uint_64":[1,4],"slice_uint_64_p":[64],"slice_uint_8":[1,2],"slice_uint_8_p":[8],"slice_uint_p":[100],"slice_user_p":[{"age":10,"name":null,"struct":null}],"slice_users_case":[{"age":0,"name":"nameP","struct":{"c_age":0,"c_name":""}}],"string":"string","string_p":"string p","struct":{},"struct_el":{"name":"el"},"struct_p":{},"structs":{"age":10,"name":"name","struct":{"c_age":100,"c_name":"cname"}},"structs_p":{"age":10,"name":"nameP","struct":{"c_age":10,"c_name":"nameP"}},"time":"0001-01-01T00:00:00Z","time_p":null,"u_int":1000,"u_int16":160,"u_int32":320,"u_int64":640,"u_int8":80,"u_intP":100,"u_int_16_p":16,"u_int_32_p":32,"u_int_64_p":64,"u_int_8_p":8,"y_time":"0001-01-01T00:00:00Z","y_time_p":null}
387387
})
388388

389389
t.Run("intAll", func(t *testing.T) {
390-
filter := SelectMarshal("intAll", NewTestCases())
391-
fmt.Println(filter.MustJSON())
390+
filter := Select("intAll", NewTestCases())
391+
fmt.Println(filter)
392392
//{"int":100,"int16":16,"int16_p":16,"int32":32,"int32_p":32,"int64":64,"int64_p":64,"int8":8,"int8_p":8,"int_p":100}
393393
})
394394
t.Run("intAllSelect", func(t *testing.T) {
@@ -411,24 +411,24 @@ func TestTestCases(t *testing.T) {
411411
})
412412

413413
t.Run("sliceAll", func(t *testing.T) {
414-
filter := SelectMarshal("sliceAll", NewTestCases())
415-
fmt.Println(filter.MustJSON())
414+
filter := Select("sliceAll", NewTestCases())
415+
fmt.Println(filter)
416416
//{"slice_bool":[true,true],"slice_bool_p":[true],"slice_byte":[10],"slice_byte_p":[10],"slice_float_32":[12.7],"slice_float_32_p":[320.1],"slice_float_64":[12.3],"slice_float_64_p":[320.1],"slice_int":[1,2],"slice_int_16":[1,2],"slice_int_16_p":[16],"slice_int_32":[1,2],"slice_int_32_i":[32],"slice_int_64":[1,2],"slice_int_64_i":[64],"slice_int_8":[1,2],"slice_int_8_p":[8],"slice_int_p":[100,100],"slice_interface":["12","13"],"slice_interface_p":["string p","string p"],"slice_map":[{"map1":1},{"map2":2}],"slice_map_p":[{"map1":1},{"map2":2}],"slice_slice_int":[[1,23],[2,3]],"slice_slice_users_case":[[],[]],"slice_string":["slice string","123"],"slice_string_s":["string p","string p"],"slice_struct":[],"slice_struct_p":[],"slice_time":["2022-03-07T17:03:15.003074+08:00","2022-03-07T17:03:15.003074+08:00"],"slice_uint":[1,3],"slice_uint_16":[1,3],"slice_uint_16_p":[16],"slice_uint_32":[1,2],"slice_uint_32_p":[32],"slice_uint_64":[1,4],"slice_uint_64_p":[64],"slice_uint_8":[1,2],"slice_uint_8_p":[8],"slice_uint_p":[100],"slice_user_p":[],"slice_users_case":[]}
417417
})
418418

419419
t.Run("struct", func(t *testing.T) {
420-
filter := SelectMarshal("struct", NewTestCases())
421-
fmt.Println(filter.MustJSON())
420+
filter := Select("struct", NewTestCases())
421+
fmt.Println(filter)
422422
//{"struct":{},"struct_el":{"name":"el"},"struct_p":{},"structs":{"struct":{"c_age":100,"c_name":"cname"}},"structs_p":{"struct":{"c_age":10}}}
423423
})
424424
t.Run("Anonymous", func(t *testing.T) {
425-
filter := SelectMarshal("Anonymous", NewTestCases())
426-
fmt.Println(filter.MustJSON())
425+
filter := Select("Anonymous", NewTestCases())
426+
fmt.Println(filter)
427427
//{"anonymous_value_name":"anonymous_value_name","article":{"name":"","page_num":0,"page_size":0,"price":"","title":""}}
428428
})
429429
t.Run("mapAll", func(t *testing.T) {
430-
filter := SelectMarshal("mapAll", NewTestCases())
431-
fmt.Println(filter.MustJSON())
430+
filter := Select("mapAll", NewTestCases())
431+
fmt.Println(filter)
432432
//{"map":{"string":"map val"},"map_p":{"map_p":"map val"}}
433433
})
434434

0 commit comments

Comments
 (0)