Add BindingBody implementation for some binding

This commit is contained in:
delphinus 2018-04-29 15:16:47 +09:00
parent 3ae520b45c
commit e825c37415
4 changed files with 41 additions and 10 deletions

View File

@ -5,6 +5,8 @@
package binding package binding
import ( import (
"bytes"
"io"
"net/http" "net/http"
"github.com/gin-gonic/gin/json" "github.com/gin-gonic/gin/json"
@ -22,7 +24,15 @@ func (jsonBinding) Name() string {
} }
func (jsonBinding) Bind(req *http.Request, obj interface{}) error { func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
decoder := json.NewDecoder(req.Body) return decodeJSON(req.Body, obj)
}
func (jsonBinding) BindBody(body []byte, obj interface{}) error {
return decodeJSON(bytes.NewReader(body), obj)
}
func decodeJSON(r io.Reader, obj interface{}) error {
decoder := json.NewDecoder(r)
if EnableDecoderUseNumber { if EnableDecoderUseNumber {
decoder.UseNumber() decoder.UseNumber()
} }

View File

@ -5,6 +5,8 @@
package binding package binding
import ( import (
"bytes"
"io"
"net/http" "net/http"
"github.com/ugorji/go/codec" "github.com/ugorji/go/codec"
@ -17,7 +19,16 @@ func (msgpackBinding) Name() string {
} }
func (msgpackBinding) Bind(req *http.Request, obj interface{}) error { func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
if err := codec.NewDecoder(req.Body, new(codec.MsgpackHandle)).Decode(&obj); err != nil { return decodeMsgPack(req.Body, obj)
}
func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
return decodeMsgPack(bytes.NewReader(body), obj)
}
func decodeMsgPack(r io.Reader, obj interface{}) error {
cdc := new(codec.MsgpackHandle)
if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
return err return err
} }
return validate(obj) return validate(obj)

View File

@ -17,19 +17,20 @@ func (protobufBinding) Name() string {
return "protobuf" return "protobuf"
} }
func (protobufBinding) Bind(req *http.Request, obj interface{}) error { func (b protobufBinding) Bind(req *http.Request, obj interface{}) error {
buf, err := ioutil.ReadAll(req.Body) buf, err := ioutil.ReadAll(req.Body)
if err != nil { if err != nil {
return err return err
} }
return b.BindBody(buf, obj)
if err = proto.Unmarshal(buf, obj.(proto.Message)); err != nil {
return err
} }
//Here it's same to return validate(obj), but util now we cann't add `binding:""` to the struct func (protobufBinding) BindBody(body []byte, obj interface{}) error {
//which automatically generate by gen-proto if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {
return err
}
// Here it's same to return validate(obj), but util now we cann't add
// `binding:""` to the struct which automatically generate by gen-proto
return nil return nil
// return validate(obj) // return validate(obj)
} }

View File

@ -5,7 +5,9 @@
package binding package binding
import ( import (
"bytes"
"encoding/xml" "encoding/xml"
"io"
"net/http" "net/http"
) )
@ -16,7 +18,14 @@ func (xmlBinding) Name() string {
} }
func (xmlBinding) Bind(req *http.Request, obj interface{}) error { func (xmlBinding) Bind(req *http.Request, obj interface{}) error {
decoder := xml.NewDecoder(req.Body) return decodeXML(req.Body, obj)
}
func (xmlBinding) BindBody(body []byte, obj interface{}) error {
return decodeXML(bytes.NewReader(body), obj)
}
func decodeXML(r io.Reader, obj interface{}) error {
decoder := xml.NewDecoder(r)
if err := decoder.Decode(obj); err != nil { if err := decoder.Decode(obj); err != nil {
return err return err
} }