Bind is a helper function for given interface object and returns a Gin middleware.
(val any)
| 27 | |
| 28 | // Bind is a helper function for given interface object and returns a Gin middleware. |
| 29 | func Bind(val any) HandlerFunc { |
| 30 | value := reflect.ValueOf(val) |
| 31 | if value.Kind() == reflect.Ptr { |
| 32 | panic(`Bind struct can not be a pointer. Example: |
| 33 | Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{}) |
| 34 | `) |
| 35 | } |
| 36 | typ := value.Type() |
| 37 | |
| 38 | return func(c *Context) { |
| 39 | obj := reflect.New(typ).Interface() |
| 40 | if c.Bind(obj) == nil { |
| 41 | c.Set(BindKey, obj) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware. |
| 47 | func WrapF(f http.HandlerFunc) HandlerFunc { |