diff --git a/README.md b/README.md index 1b5fb493..40f4d8a8 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi - [Custom Validators](#custom-validators) - [Only Bind Query String](#only-bind-query-string) - [Bind Query String or Post Data](#bind-query-string-or-post-data) + - [Bind Uri](#bind-uri) - [Bind HTML checkboxes](#bind-html-checkboxes) - [Multipart/Urlencoded binding](#multiparturlencoded-binding) - [XML, JSON, YAML and ProtoBuf rendering](#xml-json-yaml-and-protobuf-rendering) @@ -793,6 +794,40 @@ Test it with: $ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15" ``` +### Bind Uri + +See the [detail information](https://github.com/gin-gonic/gin/issues/846). + +```go +package main + +import "github.com/gin-gonic/gin" + +type Person struct { + Id string `uri:"id" binding:"required,uuid"` + Name string `uri:"name" binding:"required"` +} + +func main() { + route := gin.Default() + route.GET("/:name/:id", func(c *gin.Context) { + var person Person + if err := c.ShouldBindUri(&person); err != nil { + c.JSON(400, gin.H{"msg": err}) + return + } + c.JSON(200, gin.H{"name": person.Name, "uuid": person.Id}) + }) + route.Run(":8088") +} +``` + +Test it with: +```sh +$ curl -v localhost:8088/thinkerou/987fbc97-4bed-5078-9f07-9141ba07c9f3 +$ curl -v localhost:8088/thinkerou/not-uuid +``` + ### Bind HTML checkboxes See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092) diff --git a/binding/binding_test.go b/binding/binding_test.go index f84714da..67e62838 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -655,7 +655,7 @@ func TestUriBinding(t *testing.T) { } var tag Tag params := internal.Params{internal.Param{Key: "name", Value: "thinkerou"}} - assert.Nil(t, b.BindUri(params, &tag)) + assert.NoError(t, b.BindUri(params, &tag)) assert.Equal(t, "thinkerou", tag.Name) } diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 4c90a516..985de7e3 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -15,8 +15,7 @@ import ( ) func mapUri(ptr interface{}, ps internal.Params) error { - var m map[string][]string - m = make(map[string][]string) + m := make(map[string][]string) for _, v := range ps { m[v.Key] = []string{v.Value} } diff --git a/githubapi_test.go b/githubapi_test.go index 69e05691..dd5dcbe5 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -287,6 +287,27 @@ var githubAPI = []route{ {"DELETE", "/user/keys/:id"}, } +func TestShouldBindUri(t *testing.T) { + DefaultWriter = os.Stdout + router := Default() + + type Person struct { + Name string `uri:"name"` + Id string `uri:"id"` + } + router.Handle("GET", "/rest/:name/:id", func(c *Context) { + var person Person + assert.NoError(t, c.ShouldBindUri(&person)) + assert.True(t, "" != person.Name) + assert.True(t, "" != person.Id) + c.String(http.StatusOK, "ShouldBindUri test OK") + }) + + path, _ := exampleFromPath("/rest/:name/:id") + w := performRequest(router, "GET", path) + assert.Equal(t, "ShouldBindUri test OK", w.Body.String()) +} + func githubConfigRouter(router *Engine) { for _, route := range githubAPI { router.Handle(route.method, route.path, func(c *Context) {