From version v1.15
, the Request
object supports binding default values to properties of the input object via struct tag
. The name of the default value struct tag
is d
(it can also be default
).
Let's look at an example for better understanding.
Parameter Object Definition
type GetListReq struct {
g.Meta `path:"/" method:"get"`
Type string `v:"required#Please select content model" dc:"Content model"`
Page int `v:"min:0#Error in pagination number" dc:"Pagination number" d:"1"`
Size int `v:"max:50#Maximum pagination count is 50" dc:"Pagination count, maximum 50" d:"10"`
Sort int `v:"in:0,1,2#Invalid sort type" dc:"Sort type (0: Latest, default. 1: Active, 2: Popular)"`
}
type GetListRes struct {
Items []Item `dc:"Content list"`
}
type Item struct {
Id int64 `dc:"Content ID"`
Title string `dc:"Content title"`
}
This is a parameter object for receiving a request to query the content list. Here, we use the d
tag to assign default values to the attributes Page
and Size
. When these two parameters are not passed, they default to 1
and 10
, meaning pagination starts from page 1
and queries 10
items per page.
Parameter Object Usage
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
type GetListReq struct {
g.Meta `path:"/" method:"get"`
Type string `v:"required#Please select content model" dc:"Content model"`
Page int `v:"min:0#Error in pagination number" dc:"Pagination number" d:"1"`
Size int `v:"max:50#Maximum pagination count is 50" dc:"Pagination count, maximum 50" d:"10"`
Sort int `v:"in:0,1,2#Invalid sort type" dc:"Sort type (0: Latest, default. 1: Active, 2: Popular)"`
}
type GetListRes struct {
Items []Item `dc:"Content list"`
}
type Item struct {
Id int64 `dc:"Content ID"`
Title string `dc:"Content title"`
}
type Controller struct{}
func (Controller) GetList(ctx context.Context, req *GetListReq) (res *GetListRes, err error) {
g.Log().Info(ctx, req)
return
}
func main() {
s := g.Server()
s.Group("/content", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(&Controller{})
})
s.SetPort(8199)
s.Run()
}
Visit the following addresses and check the server terminal output results:
http://127.0.0.1:8199/content?type=ask
2023-03-21 21:58:23.058 [INFO] {2883f9c2dc734e170a35c73ea3560b4b} {"Type":"ask","Page":1,"Size":10,"Sort":0}
http://127.0.0.1:8199/content?type=ask&page=
2023-03-21 21:58:32.555 [INFO] {b86e22f9de734e170b35c73edf07859d} {"Type":"ask","Page":1,"Size":10,"Sort":0}
http://127.0.0.1:8199/content?type=ask&page=2
2023-03-21 22:01:02.907 [INFO] {a016c8fa01744e170f35c73e99082f53} {"Type":"ask","Page":2,"Size":10,"Sort":0}
As observed, when the client side does not pass or passes an empty page
parameter, the default values defined on the server side will be used; however, when the client side passes a specific page
parameter, the default values will not take effect.
Considerations
The default value parameter binding is determined by whether the client has not submitted the parameter to identify if the default value is enabled. If the client has submitted the parameter, even if the parameter value is an empty string, it will be treated as the client having passed a specific value, and the default value tag on the server-side data structure will not take effect.