update readme and add test case

This commit is contained in:
thinkerou 2018-11-03 08:35:44 +08:00
parent 06e37a02ba
commit 91e3335932
4 changed files with 58 additions and 3 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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}
}

View File

@ -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) {