1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 16:23:58 +00:00

💊 Change default value of Querybool from true to false. (#2391)

* 🩹 Fix QueryBool function: change default value from true to false

* 📚 Update QueryBool function document

* Update ctx.md

---------

Co-authored-by: RW <rene@gofiber.io>
This commit is contained in:
Iliya 2023-03-30 14:56:26 +03:30 committed by GitHub
parent 28d9abb71b
commit bf31f1f3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 16 deletions

10
ctx.go
View File

@ -1081,17 +1081,17 @@ func (c *Ctx) QueryInt(key string, defaultValue ...int) int {
// Get /?name=alex&want_pizza=false&id=
// QueryBool("want_pizza") == false
// QueryBool("want_pizza", true) == false
// QueryBool("alex") == true
// QueryBool("alex", false) == false
// QueryBool("id") == true
// QueryBool("id", false) == false
// QueryBool("name") == false
// QueryBool("name", true) == true
// QueryBool("id") == false
// QueryBool("id", true) == true
func (c *Ctx) QueryBool(key string, defaultValue ...bool) bool {
value, err := strconv.ParseBool(c.app.getString(c.fasthttp.QueryArgs().Peek(key)))
if err != nil {
if len(defaultValue) > 0 {
return defaultValue[0]
}
return true
return false
}
return value
}

View File

@ -2179,10 +2179,10 @@ func Test_Ctx_QueryBool(t *testing.T) {
utils.AssertEqual(t, false, c.QueryBool("want_pizza"))
utils.AssertEqual(t, false, c.QueryBool("want_pizza", true))
utils.AssertEqual(t, true, c.QueryBool("name"))
utils.AssertEqual(t, false, c.QueryBool("name", false))
utils.AssertEqual(t, true, c.QueryBool("id"))
utils.AssertEqual(t, false, c.QueryBool("id", false))
utils.AssertEqual(t, false, c.QueryBool("name"))
utils.AssertEqual(t, true, c.QueryBool("name", true))
utils.AssertEqual(t, false, c.QueryBool("id"))
utils.AssertEqual(t, true, c.QueryBool("id", true))
}
func Test_Ctx_QueryFloat(t *testing.T) {

View File

@ -1088,8 +1088,8 @@ This property is an object containing a property for each query boolean paramete
:::caution
Please note if that parameter is not in the request, true will be returned.
If the parameter is not a boolean, it is still tried to be converted and usually returned as true.
Please note if that parameter is not in the request, false will be returned.
If the parameter is not a boolean, it is still tried to be converted and usually returned as false.
:::
```go title="Signature"
@ -1102,10 +1102,10 @@ func (c *Ctx) QueryBool(key string, defaultValue ...bool) bool
app.Get("/", func(c *fiber.Ctx) error {
c.QueryBool("want_pizza") // false
c.QueryBool("want_pizza", true) // false
c.QueryBool("alex") // true
c.QueryBool("alex", false) // false
c.QueryBool("id") // true
c.QueryBool("id", false) // false
c.QueryBool("name") // false
c.QueryBool("name", true) // true
c.QueryBool("id") // false
c.QueryBool("id", true) // true
// ...
})