make default value in GetQueryBool true, return error in QueryBool

This commit is contained in:
iliya 2023-02-04 12:28:23 +03:30
parent 2e41772d61
commit 9ce3d65a11
2 changed files with 12 additions and 12 deletions

View File

@ -494,12 +494,12 @@ func (c *Context) GetQueryMap(key string) (map[string]string, bool) {
// c.QueryBool("force") == true // c.QueryBool("force") == true
// c.QueryBool("name") == false // c.QueryBool("name") == false
// c.QueryBool("delivery") == false // c.QueryBool("delivery") == false
func (c *Context) QueryBool(key string) bool { func (c *Context) QueryBool(key string) (bool, error) {
value, _ := c.GetQuery(key) value, _ := c.GetQuery(key)
boolValue, _ := strconv.ParseBool(value) boolValue, err := strconv.ParseBool(value)
return boolValue return boolValue, err
} }
// GetQueryBoolDefault returns the keyed boolean url query value if it exists and valid, // GetQueryBoolDefault returns the keyed boolean url query value if it exists and valid,
@ -524,22 +524,22 @@ func (c *Context) GetQueryBoolDefault(key string, defaultValue bool) bool {
// GetQueryBool is like QueryBool(), it returns the keyed url query value but in boolean, // GetQueryBool is like QueryBool(), it returns the keyed url query value but in boolean,
// if key exists and valid it returns (value, true) // if key exists and valid it returns (value, true)
// but if key is not exist or not valid, it returns (false, false) // but if key is not exist or not valid, it returns (true, false)
// //
// GET /?name=alex&force=false&delivery= // GET /?name=alex&force=false&delivery=
// GetQueryBool("force") == false, true // GetQueryBool("force") == false, true
// GetQueryBool("name") == false, false // GetQueryBool("name") == true, false
// GetQueryBool("delivery") == false, false // GetQueryBool("delivery") == true, false
func (c *Context) GetQueryBool(key string) (bool, bool) { func (c *Context) GetQueryBool(key string) (bool, bool) {
if value, ok := c.GetQuery(key); ok { if value, ok := c.GetQuery(key); ok {
boolValue, err := strconv.ParseBool(value) boolValue, err := strconv.ParseBool(value)
if err != nil { if err != nil {
return false, false return true, false
} }
return boolValue, true return boolValue, true
} }
return false, false return true, false
} }
// PostForm returns the specified key from a POST urlencoded form or multipart form // PostForm returns the specified key from a POST urlencoded form or multipart form

View File

@ -405,11 +405,11 @@ func TestContextQuery(t *testing.T) {
assert.Empty(t, value) assert.Empty(t, value)
assert.Empty(t, c.PostForm("foo")) assert.Empty(t, c.PostForm("foo"))
valueBool := c.QueryBool("read") valueBool, _ := c.QueryBool("read")
assert.True(t, valueBool) assert.True(t, valueBool)
assert.Equal(t, true, valueBool) assert.Equal(t, true, valueBool)
valueBool = c.QueryBool("id") valueBool, _ = c.QueryBool("id")
assert.False(t, valueBool) assert.False(t, valueBool)
assert.Equal(t, false, valueBool) assert.Equal(t, false, valueBool)
@ -421,8 +421,8 @@ func TestContextQuery(t *testing.T) {
valueBool, ok = c.GetQueryBool("id") valueBool, ok = c.GetQueryBool("id")
assert.False(t, ok) assert.False(t, ok)
assert.False(t, valueBool) assert.True(t, valueBool)
assert.Equal(t, valueBool, false) assert.Equal(t, valueBool, true)
assert.Equal(t, ok, false) assert.Equal(t, ok, false)
valueBool = c.GetQueryBoolDefault("read", false) valueBool = c.GetQueryBoolDefault("read", false)