Browse Source

Adds Bind() middleware

Manu Mtz-Almeida 10 years ago
parent
commit
ab447bb188
1 changed files with 12 additions and 0 deletions
  1. 12 0
      utils.go

+ 12 - 0
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)