Some work around bindings. it may do not compile

This commit is contained in:
Manu Mtz-Almeida 2014-07-03 19:19:06 +02:00
parent b8053b284d
commit 1aa3216303
3 changed files with 72 additions and 9 deletions

34
binding/binding.go Normal file
View File

@ -0,0 +1,34 @@
package binding
import (
"encoding/json"
"encoding/xml"
"io"
)
type (
Binding interface {
Bind(io.Reader, interface{}) error
}
// JSON binding
jsonBinding struct{}
// JSON binding
xmlBinding struct{}
)
var (
JSON = jsonBinding{}
XML = xmlBinding{}
)
func (_ jsonBinding) Bind(r io.Reader, obj interface{}) error {
decoder := json.NewDecoder(r)
return decoder.Decode(&obj)
}
func (_ xmlBinding) Bind(r io.Reader, obj interface{}) error {
decoder := xml.NewDecoder(r)
return decoder.Decode(&obj)
}

View File

@ -44,7 +44,8 @@ func main() {
var json struct { var json struct {
Value string `json:"value" binding:"required"` Value string `json:"value" binding:"required"`
} }
if c.EnsureBody(&json) {
if c.Bind(&json) {
DB[user] = json.Value DB[user] = json.Value
c.JSON(200, gin.H{"status": "ok"}) c.JSON(200, gin.H{"status": "ok"})
} }

44
gin.go
View File

@ -6,6 +6,7 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin/binding"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"html/template" "html/template"
"log" "log"
@ -371,22 +372,49 @@ func (c *Context) Get(key string) interface{} {
/************************************/ /************************************/
// Like ParseBody() but this method also writes a 400 error if the json is not valid. // Like ParseBody() but this method also writes a 400 error if the json is not valid.
// DEPRECATED, use Bind() instead.
func (c *Context) EnsureBody(item interface{}) bool { func (c *Context) EnsureBody(item interface{}) bool {
if err := c.ParseBody(item); err != nil { return c.Bind(item)
}
// DEPRECATED use bindings directly
// Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer.
func (c *Context) ParseBody(item interface{}) error {
return binding.JSON.Bind(c.Req.Body, item)
}
// This function checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
// "application/json" --> JSON binding
// "application/xml" --> XML binding
// else --> returns an error
// if Parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer.Like ParseBody() but this method also writes a 400 error if the json is not valid.
func (c *Context) Bind(obj interface{}) bool {
var err error
switch c.Req.Header.Get("Content-Type") {
case "application/json":
err = binding.JSON.Bind(c.Req.Body, obj)
case "application/xml":
err = binding.XML.Bind(c.Req.Body, obj)
default:
err = errors.New("unknown content-type: " + c.Req.Header.Get("Content-Type"))
}
if err == nil {
err = Validate(c, obj)
}
if err != nil {
c.Fail(400, err) c.Fail(400, err)
return false return false
} }
return true return true
} }
// Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer. func (c *Context) BindWith(obj interface{}, b binding.Binding) bool {
func (c *Context) ParseBody(item interface{}) error { if err := b.Bind(c.Req.Body, obj); err != nil {
decoder := json.NewDecoder(c.Req.Body) c.Fail(400, err)
if err := decoder.Decode(&item); err == nil { return false
return Validate(c, item)
} else {
return err
} }
return true
} }
// Serializes the given struct as a JSON into the response body in a fast and efficient way. // Serializes the given struct as a JSON into the response body in a fast and efficient way.