Skip to content

Commit 4fb6619

Browse files
committed
修改返回类型为Filter,fmt.Println即可打印过滤后的json,修改案例
1 parent dfafc8f commit 4fb6619

10 files changed

+27
-25
lines changed

example/anonymous_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func TestAnonymous(t *testing.T) {
2828
},
2929
}
3030

31-
articleJson := filter.SelectMarshal("article", article)
32-
fmt.Println(articleJson.MustJSON())
31+
articleJson := filter.Select("article", article)
32+
fmt.Println(articleJson)
3333
//输出结果---> {"pageInfo":999,"pageNum":1,"title":"c++从研发到脱发"}
3434

3535
}

example/complex_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ func NewUser() Users {
8080

8181
func TestComplex(t *testing.T) {
8282
user := NewUser()
83-
lang := filter.SelectMarshal("lang", user)
84-
fmt.Println(lang.MustJSON())
83+
lang := filter.Select("lang", user)
84+
fmt.Println(lang)
8585
//{"langAge":[{"name":"c"},{"name":"c++"},{"name":"Go"}],"uid":1}
8686

87-
lookup := filter.SelectMarshal("lookup", user)
88-
fmt.Println(lookup.MustJSON())
87+
lookup := filter.Select("lookup", user)
88+
fmt.Println(lookup)
8989
//{"langAge":[{"arts":[{"profile":{"c":"clang"},"values":["1","2"]}]},{"arts":[{"profile":{"c++":"cpp"},"values":["cpp1","cpp2"]}]},{"arts":[{"profile":{"Golang":"go"},"values":["Golang","Golang1"]}]}],"uid":1}
9090

9191
}

example/generic.go.exam

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ func NewUser() User[Lang, string, int] {
5151
}
5252

5353
func TestJson(t *testing.T) {
54-
fmt.Println(filter.SelectMarshal("article", NewUser()).MustJSON())
54+
fmt.Println(filter.Select("article", NewUser()))
5555
//{"data":{"is_static":true,"name":"go"},"data1":{"is_static":true,"name":"c++"},"k":"key","k_p":"key","nickname":"boyan","v":10,"vp":10}
5656

5757

58-
fmt.Println(filter.SelectMarshal("chat", NewUser()).MustJSON())
58+
fmt.Println(filter.Select("chat", NewUser()))
5959
//{"nickname":"boyan"}
6060
}

example/good_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ type UserModel struct {
1010

1111
func (u UserModel) ArticleResp() interface{} {
1212
//这样当你后面想要优化性能时可以在这里进行优化,
13-
return filter.SelectMarshal("article", u).Interface()
13+
return filter.Select("article", u)
1414
}
1515

1616
func (u UserModel) ProfileResp() interface{} {
1717
//这样当你后面想要优化性能时可以在这里进行优化,
18-
return filter.SelectMarshal("profile", u).Interface()
18+
return filter.Select("profile", u)
1919
}
2020

2121
func (u UserModel) ChatResp() interface{} {

example/omit_simple_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ func NewOmitUser() OmitUser {
2929
}
3030

3131
func TestOmitUser(t *testing.T) {
32-
fmt.Println(filter.OmitMarshal("lang", NewOmitUser()).MustJSON())
32+
fmt.Println(filter.Omit("lang", NewOmitUser()))
3333
//{"LangAge":{"Name":"go"},"Name":"boyan"}
3434
}

example/select_simple.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ func main() {
4141
fmt.Println(string(marshal)) //以下是官方的json解析输出结果:可以看到所有的字段都被解析了出来
4242
//{"uid":1,"nickname":"boyan","avatar":"avatar","sex":1,"vip_end_time":"2023-03-06T23:11:22.622693+08:00","price":"999.9"}
4343

44-
//用法:filter.SelectMarshal("select里的一个场景",这里可以是slice/array/struct/pointer/map)
45-
fmt.Println(filter.SelectMarshal("article", user).MustJSON()) //以下是通过json-filter 过滤后的json,此输出是article接口下的json
44+
//用法:filter.Select("select里的一个场景",这里可以是slice/array/struct/pointer/map)
45+
fmt.Println(filter.Select("article", user)) //以下是通过json-filter 过滤后的json,此输出是article接口下的json
4646
//{"avatar":"avatar","nickname":"boyan","uid":1}
4747

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

example/slice_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ func TestSlice(t *testing.T) {
3131
},
3232
}
3333

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

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

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

filter/filter.go

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

45-
// SelectMarshal 第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
45+
// SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
4646
func SelectMarshal(selectScene string, el interface{}) Filter {
4747
if enableCache {
4848
return selectWithCache(selectScene, el)
@@ -63,9 +63,9 @@ func selectMarshal(selectScene string, el interface{}) Filter {
6363
// Select 直接返回过滤后的数据结构,相当于直接SelectMarshal后再调用Interface方法
6464
func Select(selectScene string, el interface{}) interface{} {
6565
if enableCache {
66-
return selectWithCache(selectScene, el).Interface()
66+
return selectWithCache(selectScene, el)
6767
}
68-
return selectMarshal(selectScene, el).Interface()
68+
return selectMarshal(selectScene, el)
6969
}
7070

7171
// selectWithCache 直接返回过滤后的数据结构,相当于直接SelectMarshal后再调用Interface方法
@@ -83,12 +83,12 @@ func selectWithCache(selectScene string, el interface{}) Filter {
8383
// Omit 直接返回过滤后的数据结构,相当于直接OmitMarshal后再调用Interface方法
8484
func Omit(omitScene string, el interface{}) interface{} {
8585
if enableCache {
86-
return omitWithCache(omitScene, el).Interface()
86+
return omitWithCache(omitScene, el)
8787
}
88-
return omitMarshal(omitScene, el).Interface()
88+
return omitMarshal(omitScene, el)
8989
}
9090

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

pprof.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111
)
1212

13-
var j string
13+
var j interface{}
1414

1515
func main() {
1616
log.SetFlags(log.Lshortfile | log.LstdFlags)
@@ -28,7 +28,7 @@ func main() {
2828
}()
2929

3030
for {
31-
j = filter.SelectMarshal("article", newUser()).MustJSON()
31+
j = filter.Select("article", newUser())
3232
}
3333
}
3434

test/slice.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ func TestSlice() {
3737
marshal, _ := json.Marshal(test)
3838
fmt.Println("原生slice json 解析", string(marshal))
3939
//{"slices":["值"],"test":["值"],"slice_p":["值"],"slices_pp":["值"]}
40+
41+
fmt.Println(filter.SelectMarshal("test", test))
4042
}

0 commit comments

Comments
 (0)