Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ func (p *Page) LoadArray(slice any, results Pageable, callback func(item any) Er
func (p *PageParams) skipToIndex() int {
// set the defaults
defaultPerPage, maxPerPage := 10, 5000
// if the perPage isn't set
if p.PerPage == 0 {
// if the perPage isn't set or is invalid
if p.PerPage <= 0 {
// use the default
p.PerPage = defaultPerPage
}
Expand All @@ -192,7 +192,7 @@ func (p *PageParams) skipToIndex() int {
p.PerPage = maxPerPage
}
// start page count at 1 not 0
if p.PageNumber == 0 {
if p.PageNumber <= 0 {
// set to page 1
p.PageNumber = 1
}
Expand Down
21 changes: 21 additions & 0 deletions lib/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

// NOTE: pages are covered in the FSM module

type testPageItems []int

func (t *testPageItems) New() Pageable { return new(testPageItems) }

func TestMarshalUnmarshal(t *testing.T) {
// create a proto structure to test with
expected := &Signature{
Expand All @@ -28,6 +32,23 @@ func TestMarshalUnmarshal(t *testing.T) {
require.EqualExportedValues(t, expected, got)
}

func TestPageLoadArrayNormalizesNegativeParams(t *testing.T) {
results := make(testPageItems, 0)
page := NewPage(PageParams{PageNumber: -2, PerPage: -5}, "test-items")

err := page.LoadArray([]int{1, 2, 3}, &results, func(item any) ErrorI {
results = append(results, item.(int))
return nil
})

require.NoError(t, err)
require.Equal(t, 1, page.PageNumber)
require.Equal(t, 10, page.PerPage)
require.Equal(t, 3, page.Count)
require.Equal(t, 1, page.TotalPages)
require.Equal(t, testPageItems{1, 2, 3}, results)
}

func TestJSONMarshalUnmarshal(t *testing.T) {
// create a proto structure to test with
expected := &Signature{
Expand Down