-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add method ShouldBindWidth/ShouldBindBodyWith/ShouldBindWithJSO…
…N/ShouldBindBodyWithJSON; close #21
- Loading branch information
1 parent
028ce86
commit 010e194
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package binding | ||
|
||
import "net/http" | ||
|
||
// IBinding Binding interface | ||
type IBinding interface { | ||
Bind(*http.Request, interface{}) error | ||
} | ||
|
||
// IBindingBody bind body | ||
type IBindingBody interface { | ||
IBinding | ||
BindBody([]byte, interface{}) error | ||
} | ||
|
||
var ( | ||
// JSON json binding | ||
JSON = jsonBinding{} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package binding | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type jsonBinding struct{} | ||
|
||
func (jsonBinding) Bind(req *http.Request, obj interface{}) (err error) { | ||
if req == nil || req.Body == nil { | ||
err = fmt.Errorf("bad request") | ||
} else { | ||
err = decodeJSON(req.Body, obj) | ||
} | ||
return | ||
} | ||
func (jsonBinding) BindBody(b []byte, obj interface{}) error { | ||
return decodeJSON(bytes.NewBuffer(b), obj) | ||
} | ||
|
||
func decodeJSON(r io.Reader, obj interface{}) error { | ||
decoder := json.NewDecoder(r) | ||
return decoder.Decode(obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cotton | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/tonny-zhang/cotton/binding" | ||
) | ||
|
||
// ShouldBindWith bind | ||
func (ctx *Context) ShouldBindWith(obj interface{}, b binding.IBinding) error { | ||
return b.Bind(ctx.Request, obj) | ||
} | ||
|
||
// BodyBytesKey indicates a default body bytes key. | ||
const BodyBytesKey = "cotton/bbk" | ||
|
||
// ShouldBindBodyWith bind body | ||
func (ctx *Context) ShouldBindBodyWith(obj interface{}, bb binding.IBindingBody) (err error) { | ||
var body []byte | ||
if v, ok := ctx.Get(BodyBytesKey); ok { | ||
if vv, ok := v.([]byte); ok { | ||
body = vv | ||
} | ||
} | ||
if body == nil { | ||
body, err = ioutil.ReadAll(ctx.Request.Body) | ||
if err != nil { | ||
return | ||
} | ||
ctx.Set(BodyBytesKey, body) | ||
} | ||
return bb.BindBody(body, obj) | ||
} | ||
|
||
// ShouldBindWithJSON bind with json | ||
func (ctx *Context) ShouldBindWithJSON(obj interface{}) error { | ||
return ctx.ShouldBindWith(obj, binding.JSON) | ||
} | ||
|
||
// ShouldBindBodyWithJSON bind body with json | ||
func (ctx *Context) ShouldBindBodyWithJSON(obj interface{}) (err error) { | ||
return ctx.ShouldBindBodyWith(obj, binding.JSON) | ||
} |