@@ -22,12 +22,12 @@ var (
2222// in parameters.
2323// The handler may use the following signature:
2424//
25- // func(*gin.Context, [input object ptr]) ([output object], error)
25+ // func(*gin.Context, [input object ptr]) ([output object], error)
2626//
2727// Input and output objects are both optional.
2828// As such, the minimal accepted signature is:
2929//
30- // func(*gin.Context) error
30+ // func(*gin.Context) error
3131//
3232// The wrapping gin-handler will bind the parameters from the query-string,
3333// path, body and headers, and handle the errors.
@@ -49,16 +49,17 @@ func Handler(h interface{}, status int, options ...func(*Route)) gin.HandlerFunc
4949 // Wrap Gin handler.
5050 f := func (c * gin.Context ) {
5151 _ , ok := c .Get (tonicWantRouteInfos )
52+ r := & Route {}
53+ for _ , opt := range options {
54+ opt (r )
55+ }
5256 if ok {
5357 r := & Route {}
5458 r .defaultStatusCode = status
5559 r .handler = hv
5660 r .handlerType = ht
5761 r .inputType = in
5862 r .outputType = out
59- for _ , opt := range options {
60- opt (r )
61- }
6263 c .Set (tonicRoutesInfos , r )
6364 c .Abort ()
6465 return
@@ -71,8 +72,14 @@ func Handler(h interface{}, status int, options ...func(*Route)) gin.HandlerFunc
7172 // binding.
7273 if in != nil {
7374 input := reflect .New (in )
75+ routeBindHook := r .GetBindHook ()
76+ if routeBindHook == nil {
77+ // use the default bindHook if the route
78+ // does not have a custom one
79+ routeBindHook = bindHook
80+ }
7481 // Bind the body with the hook.
75- if err := bindHook (c , input .Interface ()); err != nil {
82+ if err := routeBindHook (c , input .Interface ()); err != nil {
7683 handleError (c , BindError {message : err .Error (), typ : in })
7784 return
7885 }
@@ -116,7 +123,11 @@ func Handler(h interface{}, status int, options ...func(*Route)) gin.HandlerFunc
116123 handleError (c , err .(error ))
117124 return
118125 }
119- renderHook (c , status , val )
126+ routeRenderHook := r .GetRenderHook ()
127+ if routeRenderHook == nil {
128+ routeRenderHook = renderHook
129+ }
130+ routeRenderHook (c , status , val )
120131 }
121132 // Register route in tonic-enabled routes map
122133 route := & Route {
@@ -154,13 +165,13 @@ func RegisterValidation(tagName string, validationFunc validator.Func) error {
154165//
155166// eg. to use the names which have been specified for JSON representations of structs, rather than normal Go field names:
156167//
157- // validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
158- // name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
159- // if name == "-" {
160- // return ""
161- // }
162- // return name
163- // }
168+ // validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
169+ // name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
170+ // if name == "-" {
171+ // return ""
172+ // }
173+ // return name
174+ // }
164175func RegisterTagNameFunc (registerTagFunc validator.TagNameFunc ) {
165176 validatorObj .RegisterTagNameFunc (registerTagFunc )
166177}
0 commit comments