@@ -13,34 +13,34 @@ import (
1313 "github.com/gogf/gf/v2/util/guid"
1414)
1515
16- type UserReq struct {
16+ // UserTagInReq struct tag "in" supports: header, cookie
17+ type UserTagInReq struct {
1718 g.Meta `path:"/user" tags:"User" method:"post" summary:"user api" title:"api title"`
1819 Id int `v:"required" d:"1"`
1920 Name string `v:"required" in:"cookie"`
2021 Age string `v:"required" in:"header"`
21- // header,query,cookie,form
2222}
2323
24- type UserRes struct {
24+ type UserTagInRes struct {
2525 g.Meta `mime:"text/html" example:"string"`
2626}
2727
2828var (
29- User = cUser {}
29+ UserTagIn = cUserTagIn {}
3030)
3131
32- type cUser struct {}
32+ type cUserTagIn struct {}
3333
34- func (c * cUser ) User (ctx context.Context , req * UserReq ) (res * UserRes , err error ) {
34+ func (c * cUserTagIn ) User (ctx context.Context , req * UserTagInReq ) (res * UserTagInRes , err error ) {
3535 g .RequestFromCtx (ctx ).Response .WriteJson (req )
3636 return
3737}
3838
39- func Test_Params_Tag (t * testing.T ) {
39+ func Test_ParamsTagIn (t * testing.T ) {
4040 s := g .Server (guid .S ())
4141 s .Group ("/" , func (group * ghttp.RouterGroup ) {
4242 group .Middleware (ghttp .MiddlewareHandlerResponse )
43- group .Bind (User )
43+ group .Bind (UserTagIn )
4444 })
4545 s .SetDumpRouterMap (false )
4646 s .Start ()
@@ -56,17 +56,101 @@ func Test_Params_Tag(t *testing.T) {
5656 client .SetHeader ("age" , "18" )
5757
5858 t .Assert (client .PostContent (ctx , "/user" ), `{"Id":1,"Name":"john","Age":"18"}` )
59- t .Assert (client .PostContent (ctx , "/user" , "name=&age=&id= " ), `{"Id":1,"Name":"john","Age":"18"}` )
59+ t .Assert (client .PostContent (ctx , "/user" , "name=&age=" ), `{"Id":1,"Name":"john","Age":"18"}` )
6060 })
6161}
6262
63- func Benchmark_ParamTag (b * testing.B ) {
63+ type UserTagDefaultReq struct {
64+ g.Meta `path:"/user-default" method:"post,get" summary:"user default tag api"`
65+ Id int `v:"required" d:"1"`
66+ Name string `d:"john"`
67+ Age int `d:"18"`
68+ Score float64 `d:"99.9"`
69+ IsVip bool `d:"true"`
70+ NickName string `p:"nickname" d:"nickname-default"`
71+ EmptyStr string `d:""`
72+ Email string
73+ Address string
74+ }
75+
76+ type UserTagDefaultRes struct {
77+ g.Meta `mime:"application/json" example:"string"`
78+ }
79+
80+ var (
81+ UserTagDefault = cUserTagDefault {}
82+ )
83+
84+ type cUserTagDefault struct {}
85+
86+ func (c * cUserTagDefault ) User (ctx context.Context , req * UserTagDefaultReq ) (res * UserTagDefaultRes , err error ) {
87+ g .RequestFromCtx (ctx ).Response .WriteJson (req )
88+ return
89+ }
90+
91+ func Test_ParamsTagDefault (t * testing.T ) {
92+ s := g .Server (guid .S ())
93+ s .Group ("/" , func (group * ghttp.RouterGroup ) {
94+ group .Middleware (ghttp .MiddlewareHandlerResponse )
95+ group .Bind (UserTagDefault )
96+ })
97+ s .SetDumpRouterMap (false )
98+ s .Start ()
99+ defer s .Shutdown ()
100+
101+ time .Sleep (100 * time .Millisecond )
102+
103+ gtest .C (t , func (t * gtest.T ) {
104+ prefix := fmt .Sprintf ("http://127.0.0.1:%d" , s .GetListenedPort ())
105+ client := g .Client ()
106+ client .SetPrefix (prefix )
107+
108+ // Test with no parameters, should use all default values
109+ resp := client .GetContent (ctx , "/user-default" )
110+ t .Assert (resp , `{"Id":1,"Name":"john","Age":18,"Score":99.9,"IsVip":true,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":""}` )
111+
112+ // Test with partial parameters (query method), should use partial default values
113+ resp = client .GetContent (ctx , "/user-default?id=100&name=smith" )
114+ t .Assert (resp , `{"Id":100,"Name":"smith","Age":18,"Score":99.9,"IsVip":true,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":""}` )
115+
116+ // Test with partial parameters (query method), should use partial default values
117+ resp = client .GetContent (ctx , "/user-default?id=100&name=smith&age" )
118+ t .Assert (resp , `{"Id":100,"Name":"smith","Age":18,"Score":99.9,"IsVip":true,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":""}` )
119+
120+ // Test providing partial parameters via POST form
121+ resp = client .PostContent (ctx , "/user-default" , "id=200&age=30&nickname=jack" )
122+ t .Assert (resp , `{"Id":200,"Name":"john","Age":30,"Score":99.9,"IsVip":true,"NickName":"jack","EmptyStr":"","Email":"","Address":""}` )
123+
124+ // Test providing partial parameters via POST JSON
125+ resp = client .ContentJson ().PostContent (ctx , "/user-default" , g.Map {
126+ "id" : 300 ,
127+ "name" : "bob" ,
128+ "score" : 88.8 ,
129+ "address" : "beijing" ,
130+ })
131+ t .Assert (resp , `{"Id":300,"Name":"bob","Age":18,"Score":88.8,"IsVip":true,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":"beijing"}` )
132+
133+ // Test providing JSON content via GET request
134+ resp = client .ContentJson ().PostContent (ctx , "/user-default" , `{"id":500,"isVip":false}` )
135+ t .Assert (resp , `{"Id":500,"Name":"john","Age":18,"Score":99.9,"IsVip":false,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":""}` )
136+
137+ // Test providing empty values, should use default values
138+ resp = client .PostContent (ctx , "/user-default" , "id=400&name=&age=" )
139+ t .Assert (resp , `{"Id":400,"Name":"","Age":0,"Score":99.9,"IsVip":true,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":""}` )
140+
141+ // Test providing JSON content via GET request
142+ resp = client .ContentJson ().GetContent (ctx , "/user-default" , `{"id":500,"isVip":false}` )
143+ t .Assert (resp , `{"Id":500,"Name":"john","Age":18,"Score":99.9,"IsVip":false,"NickName":"nickname-default","EmptyStr":"","Email":"","Address":""}` )
144+ })
145+ }
146+
147+ func Benchmark_ParamTagIn (b * testing.B ) {
64148 b .StopTimer ()
65149
66150 s := g .Server (guid .S ())
67151 s .Group ("/" , func (group * ghttp.RouterGroup ) {
68152 group .Middleware (ghttp .MiddlewareHandlerResponse )
69- group .Bind (User )
153+ group .Bind (UserTagIn )
70154 })
71155 s .SetDumpRouterMap (false )
72156 s .SetAccessLogEnabled (false )
0 commit comments