-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: fix crash when itemsPerPage is zero and there are items
- Loading branch information
Showing
7 changed files
with
137 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
) | ||
|
||
func paginate2(itemsPtr interface{}, itemsPerPage int, page int) int { | ||
ritems := reflect.ValueOf(itemsPtr).Elem() | ||
|
||
itemsLen := ritems.Len() | ||
if itemsLen == 0 { | ||
return 0 | ||
} | ||
|
||
pageCount := (itemsLen / itemsPerPage) | ||
if (itemsLen % itemsPerPage) != 0 { | ||
pageCount++ | ||
} | ||
|
||
min := page * itemsPerPage | ||
if min > itemsLen { | ||
min = itemsLen | ||
} | ||
|
||
max := (page + 1) * itemsPerPage | ||
if max > itemsLen { | ||
max = itemsLen | ||
} | ||
|
||
ritems.Set(ritems.Slice(min, max)) | ||
|
||
return pageCount | ||
} | ||
|
||
func paginate(itemsPtr interface{}, itemsPerPageStr string, pageStr string) (int, error) { | ||
itemsPerPage := 100 | ||
|
||
if itemsPerPageStr != "" { | ||
tmp, err := strconv.ParseUint(itemsPerPageStr, 10, 31) | ||
if err != nil { | ||
return 0, err | ||
} | ||
itemsPerPage = int(tmp) | ||
|
||
if itemsPerPage == 0 { | ||
return 0, fmt.Errorf("invalid items per page") | ||
} | ||
} | ||
|
||
page := 0 | ||
|
||
if pageStr != "" { | ||
tmp, err := strconv.ParseUint(pageStr, 10, 31) | ||
if err != nil { | ||
return 0, err | ||
} | ||
page = int(tmp) | ||
} | ||
|
||
return paginate2(itemsPtr, itemsPerPage, page), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPaginate(t *testing.T) { | ||
func() { | ||
items := make([]int, 5) | ||
for i := 0; i < 5; i++ { | ||
items[i] = i | ||
} | ||
|
||
pageCount, err := paginate(&items, "1", "1") | ||
require.NoError(t, err) | ||
require.Equal(t, 5, pageCount) | ||
require.Equal(t, []int{1}, items) | ||
}() | ||
|
||
func() { | ||
items := make([]int, 5) | ||
for i := 0; i < 5; i++ { | ||
items[i] = i | ||
} | ||
|
||
pageCount, err := paginate(&items, "3", "2") | ||
require.NoError(t, err) | ||
require.Equal(t, 2, pageCount) | ||
require.Equal(t, []int{}, items) | ||
}() | ||
|
||
func() { | ||
items := make([]int, 6) | ||
for i := 0; i < 6; i++ { | ||
items[i] = i | ||
} | ||
|
||
pageCount, err := paginate(&items, "4", "1") | ||
require.NoError(t, err) | ||
require.Equal(t, 2, pageCount) | ||
require.Equal(t, []int{4, 5}, items) | ||
}() | ||
|
||
func() { | ||
items := make([]int, 0) | ||
|
||
pageCount, err := paginate(&items, "1", "0") | ||
require.NoError(t, err) | ||
require.Equal(t, 0, pageCount) | ||
require.Equal(t, []int{}, items) | ||
}() | ||
} | ||
|
||
func FuzzPaginate(f *testing.F) { | ||
f.Fuzz(func(_ *testing.T, str1 string, str2 string) { | ||
items := make([]int, 6) | ||
for i := 0; i < 6; i++ { | ||
items[i] = i | ||
} | ||
|
||
paginate(&items, str1, str2) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go test fuzz v1 | ||
string("A") | ||
string("0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go test fuzz v1 | ||
string("1") | ||
string("A") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go test fuzz v1 | ||
string("0") | ||
string("") |