Update to 4.4.8 - Custom decoders for ReadJSON/ReadXML, read HISTORY.md

Read: https://github.com/kataras/iris/blob/master/HISTORY.md
This commit is contained in:
Gerasimos Maropoulos 2016-10-06 23:19:55 +03:00
parent 39f58fe286
commit a000da35c3
5 changed files with 144 additions and 19 deletions

View File

@ -2,6 +2,34 @@
**How to upgrade**: remove your `$GOPATH/src/github.com/kataras` folder, open your command-line and execute this command: `go get -u github.com/kataras/iris/iris`. **How to upgrade**: remove your `$GOPATH/src/github.com/kataras` folder, open your command-line and execute this command: `go get -u github.com/kataras/iris/iris`.
## 4.4.7 -> 4.4.8
- **NEW**: `RequestDecoder` gives the ability to set a custom decoder **per passed object** when `context.ReadJSON` and `context.ReadXML`
```go
// BodyDecoder is an interface which any struct can implement in order to customize the decode action
// from ReadJSON and ReadXML
//
// Trivial example of this could be:
// type User struct { Username string }
//
// func (u *User) Decode(data []byte) error {
// return json.Unmarshal(data, u)
// }
//
// the 'context.ReadJSON/ReadXML(&User{})' will call the User's
// Decode option to decode the request body
//
// Note: This is totally optionally, the default decoders
// for ReadJSON is the encoding/json and for ReadXML is the encoding/xml
type BodyDecoder interface {
Decode(data []byte) error
}
```
> for a usage example go to https://github.com/kataras/iris/blob/master/context_test.go#L262
## 4.4.6 -> 4.4.7 ## 4.4.6 -> 4.4.7
- **small fix**: websocket server is nil when more than the default websocket station tries to be registered before `OnConnection` called[*](https://github.com/kataras/iris/issues/460) - **small fix**: websocket server is nil when more than the default websocket station tries to be registered before `OnConnection` called[*](https://github.com/kataras/iris/issues/460)

View File

@ -19,7 +19,7 @@
<br/> <br/>
<a href="https://github.com/kataras/iris/releases"><img src="https://img.shields.io/badge/%20version%20-%204.4.7%20-blue.svg?style=flat-square" alt="Releases"></a> <a href="https://github.com/kataras/iris/releases"><img src="https://img.shields.io/badge/%20version%20-%204.4.8%20-blue.svg?style=flat-square" alt="Releases"></a>
<a href="https://github.com/iris-contrib/examples"><img src="https://img.shields.io/badge/%20examples-repository-3362c2.svg?style=flat-square" alt="Examples"></a> <a href="https://github.com/iris-contrib/examples"><img src="https://img.shields.io/badge/%20examples-repository-3362c2.svg?style=flat-square" alt="Examples"></a>
@ -870,7 +870,7 @@ I recommend writing your API tests using this new library, [httpexpect](https://
Versioning Versioning
------------ ------------
Current: **v4.4.7** Current: **v4.4.8**
> Iris is an active project > Iris is an active project
@ -906,7 +906,7 @@ This project is licensed under the [MIT License](LICENSE), Copyright (c) 2016 Ge
[Travis]: http://travis-ci.org/kataras/iris [Travis]: http://travis-ci.org/kataras/iris
[License Widget]: https://img.shields.io/badge/license-MIT%20%20License%20-E91E63.svg?style=flat-square [License Widget]: https://img.shields.io/badge/license-MIT%20%20License%20-E91E63.svg?style=flat-square
[License]: https://github.com/kataras/iris/blob/master/LICENSE [License]: https://github.com/kataras/iris/blob/master/LICENSE
[Release Widget]: https://img.shields.io/badge/release-4.4.7%20-blue.svg?style=flat-square [Release Widget]: https://img.shields.io/badge/release-4.4.8%20-blue.svg?style=flat-square
[Release]: https://github.com/kataras/iris/releases [Release]: https://github.com/kataras/iris/releases
[Chat Widget]: https://img.shields.io/badge/community-chat%20-00BCD4.svg?style=flat-square [Chat Widget]: https://img.shields.io/badge/community-chat%20-00BCD4.svg?style=flat-square
[Chat]: https://kataras.rocket.chat/channel/iris [Chat]: https://kataras.rocket.chat/channel/iris

View File

@ -371,33 +371,65 @@ func (ctx *Context) Subdomain() (subdomain string) {
return return
} }
// BodyDecoder is an interface which any struct can implement in order to customize the decode action
// from ReadJSON and ReadXML
//
// Trivial example of this could be:
// type User struct { Username string }
//
// func (u *User) Decode(data []byte) error {
// return json.Unmarshal(data, u)
// }
//
// the 'context.ReadJSON/ReadXML(&User{})' will call the User's
// Decode option to decode the request body
//
// Note: This is totally optionally, the default decoders
// for ReadJSON is the encoding/json and for ReadXML is the encoding/xml
type BodyDecoder interface {
Decode(data []byte) error
}
// ReadJSON reads JSON from request's body // ReadJSON reads JSON from request's body
func (ctx *Context) ReadJSON(jsonObject interface{}) error { func (ctx *Context) ReadJSON(jsonObject interface{}) error {
data := ctx.RequestCtx.Request.Body() rawData := ctx.Request.Body()
decoder := json.NewDecoder(strings.NewReader(string(data))) // check if the jsonObject contains its own decode
err := decoder.Decode(jsonObject) // in this case the jsonObject should be a pointer also,
// but this is up to the user's custom Decode implementation*
//err != nil fix by @shiena //
if err != nil && err != io.EOF { // See 'BodyDecoder' for more
return errReadBody.Format("JSON", err.Error()) if decoder, isDecoder := jsonObject.(BodyDecoder); isDecoder {
return decoder.Decode(rawData)
} }
return nil // check if jsonObject is already a pointer, if yes then pass as it's
if reflect.TypeOf(jsonObject).Kind() == reflect.Ptr {
return json.Unmarshal(rawData, jsonObject)
}
// finally, if the jsonObject doesn't contains a self-body decoder and it's not a pointer
return json.Unmarshal(rawData, &jsonObject)
} }
// ReadXML reads XML from request's body // ReadXML reads XML from request's body
func (ctx *Context) ReadXML(xmlObject interface{}) error { func (ctx *Context) ReadXML(xmlObject interface{}) error {
data := ctx.RequestCtx.Request.Body() rawData := ctx.Request.Body()
decoder := xml.NewDecoder(strings.NewReader(string(data))) // check if the xmlObject contains its own decode
err := decoder.Decode(xmlObject) // in this case the jsonObject should be a pointer also,
//err != nil fix by @shiena // but this is up to the user's custom Decode implementation*
if err != nil && err != io.EOF { //
return errReadBody.Format("XML", err.Error()) // See 'BodyDecoder' for more
if decoder, isDecoder := xmlObject.(BodyDecoder); isDecoder {
return decoder.Decode(rawData)
} }
return nil // check if xmlObject is already a pointer, if yes then pass as it's
if reflect.TypeOf(xmlObject).Kind() == reflect.Ptr {
return xml.Unmarshal(rawData, xmlObject)
}
// finally, if the xmlObject doesn't contains a self-body decoder and it's not a pointer
return xml.Unmarshal(rawData, &xmlObject)
} }
// ReadForm binds the formObject with the form data // ReadForm binds the formObject with the form data

View File

@ -11,7 +11,9 @@ CONTRIBUTE & DISCUSSION ABOUT TESTS TO: https://github.com/iris-contrib/tests
*/ */
import ( import (
"encoding/json"
"encoding/xml" "encoding/xml"
"fmt"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
@ -240,13 +242,76 @@ func TestContextReadJSON(t *testing.T) {
ctx.JSON(StatusOK, obj) ctx.JSON(StatusOK, obj)
}) })
Post("/json_pointer", func(ctx *Context) {
obj := &testBinderData{}
err := ctx.ReadJSON(obj)
if err != nil {
t.Fatalf("Error when parsing the JSON body: %s", err.Error())
}
ctx.JSON(StatusOK, obj)
})
e := Tester(t) e := Tester(t)
passed := map[string]interface{}{"Username": "myusername", "Mail": "mymail@iris-go.com", "mydata": []string{"mydata1", "mydata2"}} passed := map[string]interface{}{"Username": "myusername", "Mail": "mymail@iris-go.com", "mydata": []string{"mydata1", "mydata2"}}
expectedObject := testBinderData{Username: "myusername", Mail: "mymail@iris-go.com", Data: []string{"mydata1", "mydata2"}} expectedObject := testBinderData{Username: "myusername", Mail: "mymail@iris-go.com", Data: []string{"mydata1", "mydata2"}}
e.POST("/json").WithJSON(passed).Expect().Status(StatusOK).JSON().Object().Equal(expectedObject) e.POST("/json").WithJSON(passed).Expect().Status(StatusOK).JSON().Object().Equal(expectedObject)
e.POST("/json_pointer").WithJSON(passed).Expect().Status(StatusOK).JSON().Object().Equal(expectedObject)
} }
type testJSONBinderDataWithDecoder struct {
Username string
Mail string
Data []string `json:"mydata"`
shouldError bool
}
func (tj *testJSONBinderDataWithDecoder) Decode(data []byte) error {
if tj.shouldError {
return fmt.Errorf("Should error")
}
return json.Unmarshal(data, tj)
}
func TestContextReadJSONWithDecoder(t *testing.T) {
initDefault()
Post("/json_should_error", func(ctx *Context) {
obj := testJSONBinderDataWithDecoder{shouldError: true}
err := ctx.ReadJSON(&obj)
if err == nil {
t.Fatalf("Should prompted for error 'Should error' but not error returned from the custom decoder!")
}
ctx.Write(err.Error())
ctx.SetStatusCode(StatusOK)
})
Post("/json", func(ctx *Context) {
obj := testJSONBinderDataWithDecoder{}
err := ctx.ReadJSON(&obj)
if err != nil {
t.Fatalf("Error when parsing the JSON body: %s", err.Error())
}
ctx.JSON(StatusOK, obj)
})
Post("/json_pointer", func(ctx *Context) {
obj := &testJSONBinderDataWithDecoder{}
err := ctx.ReadJSON(obj)
if err != nil {
t.Fatalf("Error when parsing the JSON body: %s", err.Error())
}
ctx.JSON(StatusOK, obj)
})
e := Tester(t)
passed := map[string]interface{}{"Username": "kataras", "Mail": "mymail@iris-go.com", "mydata": []string{"mydata1", "mydata2"}}
expectedObject := testJSONBinderDataWithDecoder{Username: "kataras", Mail: "mymail@iris-go.com", Data: []string{"mydata1", "mydata2"}}
e.POST("/json_should_error").WithJSON(passed).Expect().Status(StatusOK).Body().Equal("Should error")
e.POST("/json").WithJSON(passed).Expect().Status(StatusOK).JSON().Object().Equal(expectedObject)
e.POST("/json_pointer").WithJSON(passed).Expect().Status(StatusOK).JSON().Object().Equal(expectedObject)
} // no need for xml, it's exact the same.
func TestContextReadXML(t *testing.T) { func TestContextReadXML(t *testing.T) {
initDefault() initDefault()

View File

@ -79,7 +79,7 @@ import (
const ( const (
// Version is the current version of the Iris web framework // Version is the current version of the Iris web framework
Version = "4.4.7" Version = "4.4.8"
banner = ` _____ _ banner = ` _____ _
|_ _| (_) |_ _| (_)