Skip to content

Commit

Permalink
Add BindUri (gin-gonic#1694)
Browse files Browse the repository at this point in the history
* add BindUri

* fix bug

* fix code style
  • Loading branch information
thinkerou authored Dec 12, 2018
1 parent f76ccb2 commit 59695e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
18 changes: 14 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,25 @@ func (c *Context) BindYAML(obj interface{}) error {
return c.MustBindWith(obj, binding.YAML)
}

// BindUri binds the passed struct pointer using binding.Uri.
// It will abort the request with HTTP 400 if any error occurs.
func (c *Context) BindUri(obj interface{}) error {
if err := c.ShouldBindUri(obj); err != nil {
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)
return err
}
return nil
}

// MustBindWith binds the passed struct pointer using the specified binding engine.
// It will abort the request with HTTP 400 if any error occurs.
// See the binding package.
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
if err = c.ShouldBindWith(obj, b); err != nil {
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) error {
if err := c.ShouldBindWith(obj, b); err != nil {
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind)
return err
}

return
return nil
}

// ShouldBind checks the Content-Type to select a binding engine automatically,
Expand Down
44 changes: 42 additions & 2 deletions githubapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ func TestShouldBindUri(t *testing.T) {
router := Default()

type Person struct {
Name string `uri:"name"`
Id string `uri:"id"`
Name string `uri:"name" binding:"required"`
Id string `uri:"id" binding:"required"`
}
router.Handle("GET", "/rest/:name/:id", func(c *Context) {
var person Person
Expand All @@ -304,6 +304,46 @@ func TestShouldBindUri(t *testing.T) {
path, _ := exampleFromPath("/rest/:name/:id")
w := performRequest(router, "GET", path)
assert.Equal(t, "ShouldBindUri test OK", w.Body.String())
assert.Equal(t, http.StatusOK, w.Code)
}

func TestBindUri(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()

type Person struct {
Name string `uri:"name" binding:"required"`
Id string `uri:"id" binding:"required"`
}
router.Handle("GET", "/rest/:name/:id", func(c *Context) {
var person Person
assert.NoError(t, c.BindUri(&person))
assert.True(t, "" != person.Name)
assert.True(t, "" != person.Id)
c.String(http.StatusOK, "BindUri test OK")
})

path, _ := exampleFromPath("/rest/:name/:id")
w := performRequest(router, "GET", path)
assert.Equal(t, "BindUri test OK", w.Body.String())
assert.Equal(t, http.StatusOK, w.Code)
}

func TestBindUriError(t *testing.T) {
DefaultWriter = os.Stdout
router := Default()

type Member struct {
Number string `uri:"num" binding:"required,uuid"`
}
router.Handle("GET", "/new/rest/:num", func(c *Context) {
var m Member
c.BindUri(&m)
})

path1, _ := exampleFromPath("/new/rest/:num")
w1 := performRequest(router, "GET", path1)
assert.Equal(t, http.StatusBadRequest, w1.Code)
}

func githubConfigRouter(router *Engine) {
Expand Down

0 comments on commit 59695e7

Please sign in to comment.