Add method to get an integer directly from the Ctx params (route params) (#1243)

* Created ParamsInt method in Ctx
* Created test for the ParamsInt method
* Recover accidentally removed comments
pull/1248/head v2.7.0
Eduardo Andrés Castillo Perera 2021-03-28 07:49:51 -06:00 committed by GitHub
parent 8611c3e3ea
commit 47f44f3960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

8
ctx.go
View File

@ -672,6 +672,14 @@ func (c *Ctx) Params(key string, defaultValue ...string) string {
return defaultString("", defaultValue)
}
// ParamsInt is used to get an integer from the route parameters
// it defaults to zero if the parameter is not found or if the
// parameter cannot be converted to an integer
func (c *Ctx) ParamsInt(key string) (int, error) {
// Use Atoi to convert the param to an int or return zero and an error
return strconv.Atoi(c.Params(key))
}
// Path returns the path part of the request URL.
// Optionally, you could override the path.
func (c *Ctx) Path(override ...string) string {

View File

@ -2153,3 +2153,56 @@ func Test_Ctx_String(t *testing.T) {
utils.AssertEqual(t, "#0000000000000000 - 0.0.0.0:0 <-> 0.0.0.0:0 - GET http:///", c.String())
}
func TestCtx_ParamsInt(t *testing.T) {
// Create a test context and set some strings (or params)
// create a fake app to be used within this test
app := New()
// Create some test endpoints
// For the user id I will use the number 1111, so I should be able to get the number
// 1111 from the Ctx
app.Get("/test/:user", func(c *Ctx) error {
// utils.AssertEqual(t, "john", c.Params("user"))
num, err := c.ParamsInt("user")
// Check the number matches
if num != 1111 {
t.Fatalf("Expected number 1111 from the path, got %d", num)
}
// Check no errors are returned, because we want NO errors in this one
if err != nil {
t.Fatalf("Expected nil error for 1111 test, got " + err.Error())
}
return nil
})
// In this test case, there will be a bad request where the expected number is NOT
// a number in the path
app.Get("/testnoint/:user", func(c *Ctx) error {
// utils.AssertEqual(t, "john", c.Params("user"))
num, err := c.ParamsInt("user")
// Check the number matches
if num != 0 {
t.Fatalf("Expected number 0 from the path, got %d", num)
}
// Check an error is returned, because we want NO errors in this one
if err == nil {
t.Fatal("Expected non nil error for bad req test, got nil")
}
return nil
})
app.Test(httptest.NewRequest(MethodGet, "/test/1111", nil))
app.Test(httptest.NewRequest(MethodGet, "/testnoint/xd", nil))
}