💊 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>
pull/2393/head
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= // Get /?name=alex&want_pizza=false&id=
// QueryBool("want_pizza") == false // QueryBool("want_pizza") == false
// QueryBool("want_pizza", true) == false // QueryBool("want_pizza", true) == false
// QueryBool("alex") == true // QueryBool("name") == false
// QueryBool("alex", false) == false // QueryBool("name", true) == true
// QueryBool("id") == true // QueryBool("id") == false
// QueryBool("id", false) == false // QueryBool("id", true) == true
func (c *Ctx) QueryBool(key string, defaultValue ...bool) bool { func (c *Ctx) QueryBool(key string, defaultValue ...bool) bool {
value, err := strconv.ParseBool(c.app.getString(c.fasthttp.QueryArgs().Peek(key))) value, err := strconv.ParseBool(c.app.getString(c.fasthttp.QueryArgs().Peek(key)))
if err != nil { if err != nil {
if len(defaultValue) > 0 { if len(defaultValue) > 0 {
return defaultValue[0] return defaultValue[0]
} }
return true return false
} }
return value 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"))
utils.AssertEqual(t, false, c.QueryBool("want_pizza", true)) utils.AssertEqual(t, false, c.QueryBool("want_pizza", true))
utils.AssertEqual(t, true, c.QueryBool("name")) utils.AssertEqual(t, false, c.QueryBool("name"))
utils.AssertEqual(t, false, c.QueryBool("name", false)) utils.AssertEqual(t, true, c.QueryBool("name", true))
utils.AssertEqual(t, true, c.QueryBool("id")) utils.AssertEqual(t, false, c.QueryBool("id"))
utils.AssertEqual(t, false, c.QueryBool("id", false)) utils.AssertEqual(t, true, c.QueryBool("id", true))
} }
func Test_Ctx_QueryFloat(t *testing.T) { 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 :::caution
Please note if that parameter is not in the request, true will be returned. 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 true. If the parameter is not a boolean, it is still tried to be converted and usually returned as false.
::: :::
```go title="Signature" ```go title="Signature"
@ -1100,12 +1100,12 @@ func (c *Ctx) QueryBool(key string, defaultValue ...bool) bool
// GET http://example.com/?name=alex&want_pizza=false&id= // GET http://example.com/?name=alex&want_pizza=false&id=
app.Get("/", func(c *fiber.Ctx) error { app.Get("/", func(c *fiber.Ctx) error {
c.QueryBool("want_pizza") // false c.QueryBool("want_pizza") // false
c.QueryBool("want_pizza", true) // false c.QueryBool("want_pizza", true) // false
c.QueryBool("alex") // true c.QueryBool("name") // false
c.QueryBool("alex", false) // false c.QueryBool("name", true) // true
c.QueryBool("id") // true c.QueryBool("id") // false
c.QueryBool("id", false) // false c.QueryBool("id", true) // true
// ... // ...
}) })