@@ -101,11 +101,11 @@ func main() {
101
101
// {"uid":1,"nickname":"boyan","avatar":"avatar","sex":1,"vip_end_time":"2023-03-06T23:11:22.622693+08:00","price":"999.9"}
102
102
103
103
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
106
106
// {"avatar":"avatar","nickname":"boyan","uid":1}
107
107
108
- fmt.Println (filter.SelectMarshal (" profile" , user). MustJSON ( )) // profile result
108
+ fmt.Println (filter.Select (" profile" , user)) // profile result
109
109
// {"nickname":"boyan","price":"999.9","sex":1,"vip_end_time":"2023-03-06T23:31:28.636529+08:00"}
110
110
}
111
111
@@ -146,7 +146,7 @@ At this time, you need to add omit (excluded scene | scene 0 | Scene 1 | scene n
146
146
At this time, you need to call
147
147
148
148
``` 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.
150
150
```
151
151
152
152
##### Omitempty zero value ignored
@@ -174,9 +174,9 @@ type User struct {
174
174
}
175
175
176
176
// 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.
178
178
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.
180
180
181
181
182
182
```
@@ -185,32 +185,7 @@ OmitMarshal("Whatever case you choose here",user)//The password field is exclude
185
185
186
186
#### Method of filtering filter structure
187
187
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
214
189
215
190
//If you want to use this method safely, use F. JSON () to return a JSON string and err
216
191
j, err := f.JSON()
@@ -239,7 +214,7 @@ m := map[string]interface{}{
239
214
},
240
215
}
241
216
242
- fmt.Println (filter.SelectMarshal (" article" , m). MustJSON ( ))
217
+ fmt.Println(filter.Select ("article", m))
243
218
//{"name":"哈哈","struct":{"avatar":"avatar","nickname":"boyan","uid":1}}
244
219
//You can see that the map can also be filtered directly.
245
220
@@ -277,13 +252,13 @@ func main() {
277
252
},
278
253
}
279
254
280
- fmt.Println (filter.SelectMarshal (" justName" , tags). MustJSON ( ))
255
+ fmt.Println (filter.Select (" justName" , tags))
281
256
// --->output: [{"name":"c"},{"name":"c++"},{"name":"go"}]
282
257
283
- fmt.Println (filter.SelectMarshal (" all" , tags). MustJSON ( ))
258
+ fmt.Println (filter.Select (" all" , tags))
284
259
// --->output: [{"icon":"icon-c","id":1,"name":"c"},{"icon":"icon-c++","id":1,"name":"c++"},{"icon":"icon-go","id":1,"name":"go"}]
285
260
286
- fmt.Println (filter.SelectMarshal (" chat" , tags). MustJSON ( ))
261
+ fmt.Println (filter.Select (" chat" , tags))
287
262
// --->output: [{"icon":"icon-c"},{"icon":"icon-c++"},{"icon":"icon-go"}]
288
263
289
264
}
@@ -321,7 +296,7 @@ func main() {
321
296
},
322
297
}
323
298
324
- articleJson := filter.SelectMarshal (" article" , article). MustJSON ( )
299
+ articleJson := filter.Select (" article" , article)
325
300
fmt.Println (articleJson)
326
301
// output---> {"pageInfo":999,"pageNum":1,"title":"c++从研发到脱发"}
327
302
}
@@ -433,8 +408,8 @@ func main() {
433
408
434
409
435
410
// 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)
438
413
// {"langAge":[{"name":"c"},{"name":"c++"},{"name":"Go"}],"uid":1}
439
414
440
415
// format
@@ -454,8 +429,8 @@ func main() {
454
429
}
455
430
456
431
// 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)
459
434
// {"langAge":[{"arts":[{"profile":{"c":"clang"},"values":["1","2"]}]},{"arts":[{"profile":{"c++":"cpp"},"values":["cpp1","cpp2"]}]},{"arts":[{"profile":{"Golang":"go"},"values":["Golang","Golang1"]}]}],"uid":1}
460
435
461
436
@@ -522,12 +497,12 @@ type User struct {
522
497
523
498
func (u User ) ArticleResp () interface {} {
524
499
// 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)
526
501
}
527
502
528
503
func (u User ) ProfileResp () interface {} {
529
504
// 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)
531
506
}
532
507
533
508
func (u User ) ChatResp () interface {} {
@@ -557,7 +532,7 @@ type User struct {
557
532
}
558
533
559
534
func (u User ) FilterProfile () interface {} {
560
- return filter.SelectMarshal (" profile" , u). Interface ( )
535
+ return filter.Select (" profile" , u)
561
536
}
562
537
563
538
func main () {
@@ -616,7 +591,7 @@ func UserRes(c *gin.Context) {
616
591
Avatar: " avatar" ,
617
592
}
618
593
619
- OkWithData (filter.SelectMarshal (" profile" , user). Interface ( ), c)
594
+ OkWithData (filter.Select (" profile" , user), c)
620
595
}
621
596
```
622
597
@@ -724,12 +699,11 @@ func main() {
724
699
fmt.Println (string (articleBytes)) // 以下是通过json-filter 过滤后的json,此输出是article接口下的json
725
700
// {"avatar":"avatar","nickname":"boyan","uid":1}
726
701
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
730
704
// {"avatar":"avatar","nickname":"boyan","uid":1}
731
705
732
- fmt.Println (filter.SelectMarshal (" profile" , user). MustJSON ( )) // profile接口下
706
+ fmt.Println (filter.Select (" profile" , user)) // profile接口下
733
707
// {"nickname":"boyan","price":"999.9","sex":1,"vip_end_time":"2023-03-06T23:31:28.636529+08:00"}
734
708
}
735
709
@@ -775,7 +749,6 @@ omit则反之,标记的字段会被排除。
775
749
776
750
``` go
777
751
f := filter.Omit (" chat" ,el) // 这时Nickname字段就被排除掉了。
778
- f := filter.OmitMarshal (" chat" ,el) // 同样是是一气呵成的操作,将结构体编码后返回一个对象,可以选择对象的任意形态
779
752
```
780
753
781
754
##### omitempty零值忽略
@@ -803,46 +776,14 @@ type User struct {
803
776
}
804
777
805
778
// 这样无论是任何场景
806
- SelectMarshal (" 无论这里选择任何场景" , user)// 无论何种场景都会输出UID的字段。
807
-
808
- OmitMarshal (" 无论这里选择任何场景" ,user)// 无论何种场景都会排除password 字段。
809
-
810
-
811
- ```
812
-
779
+ Select (" 无论这里选择任何场景" , user)// 无论何种场景都会输出UID的字段。
813
780
781
+ Omit (" 无论这里选择任何场景" ,user)// 无论何种场景都会排除password 字段。
814
782
815
- #### 过滤后的Filter结构体的方法
816
783
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 /切片/数组)
833
785
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字符串
846
787
847
788
// 如果想安全的使用这个方法请使用f.JSON() 会返回一个json字符串和err
848
789
j , err := f.JSON ()
@@ -871,7 +812,7 @@ m := map[string]interface{}{
871
812
},
872
813
}
873
814
874
- fmt.Println (filter.SelectMarshal (" article" , m). MustJSON ( ))
815
+ fmt.Println (filter.Select (" article" , m))
875
816
// {"name":"哈哈","struct":{"avatar":"avatar","nickname":"boyan","uid":1}}
876
817
// 可以看到map也是可以直接过滤的。
877
818
@@ -909,13 +850,13 @@ func main() {
909
850
},
910
851
}
911
852
912
- fmt.Println (filter.SelectMarshal (" justName" , tags). MustJSON ( ))
853
+ fmt.Println (filter.Select (" justName" , tags))
913
854
// --->输出结果: [{"name":"c"},{"name":"c++"},{"name":"go"}]
914
855
915
- fmt.Println (filter.SelectMarshal (" all" , tags). MustJSON ( ))
856
+ fmt.Println (filter.Select (" all" , tags))
916
857
// --->输出结果: [{"icon":"icon-c","id":1,"name":"c"},{"icon":"icon-c++","id":1,"name":"c++"},{"icon":"icon-go","id":1,"name":"go"}]
917
858
918
- fmt.Println (filter.SelectMarshal (" chat" , tags). MustJSON ( ))
859
+ fmt.Println (filter.Select (" chat" , tags))
919
860
// --->输出结果: [{"icon":"icon-c"},{"icon":"icon-c++"},{"icon":"icon-go"}]
920
861
921
862
}
@@ -956,7 +897,7 @@ func main() {
956
897
},
957
898
}
958
899
959
- articleJson := filter.SelectMarshal (" article" , article). MustJSON ( )
900
+ articleJson := filter.Select (" article" , article)
960
901
fmt.Println (articleJson)
961
902
// 输出结果---> {"pageInfo":999,"pageNum":1,"title":"c++从研发到脱发"}
962
903
}
@@ -1069,8 +1010,8 @@ func main() {
1069
1010
1070
1011
1071
1012
// 如果我只想要编程语言相关加上部分用户信息的话
1072
- lang := filter.SelectMarshal (" lang" , user)
1073
- fmt.Println (lang. MustJSON () )
1013
+ lang := filter.Select (" lang" , user)
1014
+ fmt.Println (lang)
1074
1015
// {"langAge":[{"name":"c"},{"name":"c++"},{"name":"Go"}],"uid":1}
1075
1016
1076
1017
// 格式化后
@@ -1090,8 +1031,8 @@ func main() {
1090
1031
}
1091
1032
1092
1033
// 如果我只是想获取uid加上langAge下所有Art的部分字段信息, 你可以这样
1093
- lookup := filter.SelectMarshal (" lookup" , user)
1094
- fmt.Println (lookup. MustJSON () )
1034
+ lookup := filter.Select (" lookup" , user)
1035
+ fmt.Println (lookup)
1095
1036
// {"langAge":[{"arts":[{"profile":{"c":"clang"},"values":["1","2"]}]},{"arts":[{"profile":{"c++":"cpp"},"values":["cpp1","cpp2"]}]},{"arts":[{"profile":{"Golang":"go"},"values":["Golang","Golang1"]}]}],"uid":1}
1096
1037
1097
1038
@@ -1233,27 +1174,3 @@ func GetUser(c *gin.Context) {
1233
1174
1234
1175
```
1235
1176
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
-
0 commit comments