diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-06-09 12:50:53 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-06-10 08:32:44 +0200 |
commit | e8a716b23a1ca78cf29460daacd4ba49bbc05ad1 (patch) | |
tree | a55a3ade6ca19306b3ebbae6cbafd2aefaa4c880 /resources | |
parent | 3e6cb2cb77e16be5b6ddd4ae55d5fc6bfba2d226 (diff) |
tpl/collections: Fix slice type handling in sort
The `sort` template func was producing a `[]page.Page` which did not work in `.Paginate`.
Fixes #6023
Diffstat (limited to 'resources')
-rw-r--r-- | resources/page/pages.go | 6 | ||||
-rw-r--r-- | resources/page/pages_test.go | 21 |
2 files changed, 27 insertions, 0 deletions
diff --git a/resources/page/pages.go b/resources/page/pages.go index ccfecdf2b..ac69a8079 100644 --- a/resources/page/pages.go +++ b/resources/page/pages.go @@ -66,6 +66,12 @@ func ToPages(seq interface{}) (Pages, error) { return v.Pages(), nil case PageGroup: return v.Pages, nil + case []Page: + pages := make(Pages, len(v)) + for i, vv := range v { + pages[i] = vv + } + return pages, nil case []interface{}: pages := make(Pages, len(v)) success := true diff --git a/resources/page/pages_test.go b/resources/page/pages_test.go index 5220a6d33..c90d9f5d4 100644 --- a/resources/page/pages_test.go +++ b/resources/page/pages_test.go @@ -53,3 +53,24 @@ func TestProbablyEq(t *testing.T) { }) } + +func TestToPages(t *testing.T) { + assert := require.New(t) + + p1, p2 := &testPage{title: "p1"}, &testPage{title: "p2"} + pages12 := Pages{p1, p2} + + mustToPages := func(in interface{}) Pages { + p, err := ToPages(in) + assert.NoError(err) + return p + } + + assert.Equal(Pages{}, mustToPages(nil)) + assert.Equal(pages12, mustToPages(pages12)) + assert.Equal(pages12, mustToPages([]Page{p1, p2})) + assert.Equal(pages12, mustToPages([]interface{}{p1, p2})) + + _, err := ToPages("not a page") + assert.Error(err) +} |