Skip to content

Commit

Permalink
feat: Added Params.GetInt method & Context.ParamInt method
Browse files Browse the repository at this point in the history
  • Loading branch information
c5ms committed Jul 20, 2024
1 parent cc4e114 commit f267335
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
11 changes: 11 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ func (c *Context) Param(key string) string {
return c.Params.ByName(key)
}

// ParamInt returns the value of the URL param as an int.
// It is a shortcut for c.Params.GetInt(key)
//
// router.GET("/user/:id", func(c *gin.Context) {
// // a GET request to /user/32
// id := c.Param("id") // id == "32"
// })
func (c *Context) ParamInt(key string) int {
return c.Params.GetInt(key)
}

// AddParam adds param to context and
// replaces path param key with given value for e2e testing purposes
// Example Route: "/user/:id"
Expand Down
28 changes: 17 additions & 11 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,19 @@ func TestRouteParamsByName(t *testing.T) {
name := ""
lastName := ""
wild := ""
id := 0
router := New()
router.GET("/test/:name/:last_name/*wild", func(c *Context) {
router.GET("/test/:name/:last_name/:id/*wild", func(c *Context) {
name = c.Params.ByName("name")
lastName = c.Params.ByName("last_name")
id = c.params.GetInt("id")
var ok bool
wild, ok = c.Params.Get("wild")

assert.True(t, ok)
assert.Equal(t, name, c.Param("name"))
assert.Equal(t, lastName, c.Param("last_name"))
assert.Equal(t, id, c.ParamInt("id"))

assert.Empty(t, c.Param("wtf"))
assert.Empty(t, c.Params.ByName("wtf"))
Expand All @@ -297,40 +300,45 @@ func TestRouteParamsByName(t *testing.T) {
assert.False(t, ok)
})

w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
w := PerformRequest(router, http.MethodGet, "/test/john/smith/32/is/super/great")

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "john", name)
assert.Equal(t, "smith", lastName)
assert.Equal(t, "/is/super/great", wild)
assert.Equal(t, 32, id)
}

// TestContextParamsGet tests that a parameter can be parsed from the URL even with extra slashes.
func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
name := ""
lastName := ""
wild := ""
id := 0
router := New()
router.RemoveExtraSlash = true
router.GET("/test/:name/:last_name/*wild", func(c *Context) {
router.GET("/test/:name/:last_name/:id/*wild", func(c *Context) {
name = c.Params.ByName("name")
lastName = c.Params.ByName("last_name")
id = c.params.GetInt("id")
var ok bool
wild, ok = c.Params.Get("wild")

assert.True(t, ok)
assert.Equal(t, name, c.Param("name"))
assert.Equal(t, lastName, c.Param("last_name"))
assert.Equal(t, id, c.ParamInt("id"))

assert.Empty(t, c.Param("wtf"))
assert.Empty(t, c.Params.ByName("wtf"))

wtf, ok := c.Params.Get("wtf")
assert.Empty(t, wtf)
assert.False(t, ok)

})

w := PerformRequest(router, http.MethodGet, "//test//john//smith//is//super//great")
w := PerformRequest(router, http.MethodGet, "//test//john//smith//32//is//super//great")

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "john", name)
Expand All @@ -343,23 +351,21 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
// initialisation (which happened in a previous requests).
func TestRouteParamsNotEmpty(t *testing.T) {
name := ""
age := 0
lastName := ""
wild := ""
router := New()

w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")

assert.Equal(t, http.StatusNotFound, w.Code)

router.GET("/test/:name/:last_name/*wild", func(c *Context) {
router.GET("/test/:name/:last_name/:age/*wild", func(c *Context) {
name = c.Params.ByName("name")
lastName = c.Params.ByName("last_name")
age = c.Params.GetInt("age")
var ok bool
wild, ok = c.Params.Get("wild")

assert.True(t, ok)
assert.Equal(t, name, c.Param("name"))
assert.Equal(t, lastName, c.Param("last_name"))
assert.Equal(t, age, c.ParamInt("age"))

assert.Empty(t, c.Param("wtf"))
assert.Empty(t, c.Params.ByName("wtf"))
Expand All @@ -369,7 +375,7 @@ func TestRouteParamsNotEmpty(t *testing.T) {
assert.False(t, ok)
})

w = PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
w := PerformRequest(router, http.MethodGet, "/test/john/smith/32/is/super/great")

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "john", name)
Expand Down
13 changes: 13 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gin
import (
"bytes"
"net/url"
"strconv"
"strings"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -49,6 +50,18 @@ func (ps Params) ByName(name string) (va string) {
return
}

// GetInt returns the param associated with the name as an integer.
func (ps Params) GetInt(name string) (i int) {
if val, exist := ps.Get(name); exist {
i, err := strconv.Atoi(val)
if err != nil {
return 0
}
return i
}
return
}

type methodTree struct {
method string
root *node
Expand Down

0 comments on commit f267335

Please sign in to comment.