Skip to content
Snippets Groups Projects
Commit 4d25fa84 authored by pengganyu's avatar pengganyu
Browse files

Fix GetParamInt bug: when key is zero, the result is not correct; and add test for this situation

parent a723f53d
No related branches found
Tags v3.16-rc1
No related merge requests found
......@@ -487,7 +487,7 @@ func (c URL) GetParamBool(key string, d bool) bool {
// GetParamInt gets int64 value by @key
func (c URL) GetParamInt(key string, d int64) int64 {
r, err := strconv.ParseInt(c.GetParam(key, ""), 10, 64)
if r == 0 || err != nil {
if err != nil {
return d
}
return r
......@@ -496,7 +496,7 @@ func (c URL) GetParamInt(key string, d int64) int64 {
// GetParamInt32 gets int32 value by @key
func (c URL) GetParamInt32(key string, d int32) int32 {
r, err := strconv.ParseInt(c.GetParam(key, ""), 10, 32)
if r == 0 || err != nil {
if err != nil {
return d
}
return int32(r)
......@@ -505,7 +505,7 @@ func (c URL) GetParamInt32(key string, d int32) int32 {
// GetParamByIntValue gets int value by @key
func (c URL) GetParamByIntValue(key string, d int) int {
r, err := strconv.ParseInt(c.GetParam(key, ""), 10, 0)
if r == 0 || err != nil {
if err != nil {
return d
}
return int(r)
......@@ -514,7 +514,7 @@ func (c URL) GetParamByIntValue(key string, d int) int {
// GetMethodParamInt gets int method param
func (c URL) GetMethodParamInt(method string, key string, d int64) int64 {
r, err := strconv.ParseInt(c.GetParam("methods."+method+"."+key, ""), 10, 64)
if r == 0 || err != nil {
if err != nil {
return d
}
return r
......@@ -523,7 +523,7 @@ func (c URL) GetMethodParamInt(method string, key string, d int64) int64 {
// GetMethodParamIntValue gets int method param
func (c URL) GetMethodParamIntValue(method string, key string, d int) int {
r, err := strconv.ParseInt(c.GetParam("methods."+method+"."+key, ""), 10, 0)
if r == 0 || err != nil {
if err != nil {
return d
}
return int(r)
......
......@@ -167,10 +167,22 @@ func TestURLGetParam(t *testing.T) {
func TestURLGetParamInt(t *testing.T) {
params := url.Values{}
params.Set("key", "3")
params.Set("key", "")
u := URL{baseUrl: baseUrl{params: params}}
v := u.GetParamInt("key", 1)
assert.Equal(t, int64(3), v)
assert.Equal(t, int64(1), v)
u = URL{}
v = u.GetParamInt("key", 1)
assert.Equal(t, int64(1), v)
}
func TestURLGetParamIntValue(t *testing.T) {
params := url.Values{}
params.Set("key", "0")
u := URL{baseUrl: baseUrl{params: params}}
v := u.GetParamInt("key", 1)
assert.Equal(t, int64(0), v)
u = URL{}
v = u.GetParamInt("key", 1)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment