@@ -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
146146At 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
523498func (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
528503func (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
533508func (u User ) ChatResp () interface {} {
@@ -557,7 +532,7 @@ type User struct {
557532}
558533
559534func (u User ) FilterProfile () interface {} {
560- return filter.SelectMarshal (" profile" , u). Interface ( )
535+ return filter.Select (" profile" , u)
561536}
562537
563538func 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
777751f := 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-
0 commit comments