From ab447bb18857d7f926e2e94e3dc13ccd27379323 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Fri, 12 Jun 2015 23:42:54 +0200 Subject: [PATCH] Adds Bind() middleware --- utils.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/utils.go b/utils.go index eaf6a5a..81c716f 100644 --- a/utils.go +++ b/utils.go @@ -13,6 +13,18 @@ import ( "strings" ) +const BindKey = "_gin-gonic/gin/bindkey" + +func Bind(val interface{}) HandlerFunc { + typ := reflect.ValueOf(val).Type() + return func(c *Context) { + obj := reflect.New(typ).Interface() + if c.Bind(obj) == nil { + c.Set(BindKey, obj) + } + } +} + func WrapF(f http.HandlerFunc) HandlerFunc { return func(c *Context) { f(c.Writer, c.Request)