]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Don't fail when calling Paginate with an empty pages.PagesGroup
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 8 Mar 2023 08:28:46 +0000 (09:28 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 10 Mar 2023 15:34:17 +0000 (16:34 +0100)
Fixes #10802

hugolib/paginator_test.go
resources/page/pagegroup.go
resources/page/pagination.go

index a8d8ac8dfb973341f607ae5e3cef835150a544a0..fd2321413fc41f4df15751f883261f35e82a44a0 100644 (file)
@@ -136,3 +136,29 @@ weight: %d
        b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
        b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
 }
+
+// Issue 10802
+func TestPaginatorEmptyPageGroups(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- config.toml --
+baseURL = "https://example.com/"
+-- content/p1.md --
+-- content/p2.md --
+-- layouts/index.html --
+{{ $empty := site.RegularPages | complement site.RegularPages }}
+Len: {{ len $empty }}: Type: {{ printf "%T" $empty }}
+{{ $pgs := $empty.GroupByPublishDate "January 2006" }}
+{{ $pag := .Paginate $pgs }}
+Len Pag: {{ len $pag.Pages }}
+`
+       b := NewIntegrationTestBuilder(
+               IntegrationTestConfig{
+                       T:           t,
+                       TxtarString: files,
+               },
+       ).Build()
+
+       b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
+}
index 99f1af3ff7e11cbbbdad8bc55cee6a62e728230d..d091c6bef05fa26f10a8c39c5b729329b3ac20bc 100644 (file)
@@ -430,14 +430,14 @@ func (psg PagesGroup) ProbablyEq(other any) bool {
 }
 
 // ToPagesGroup tries to convert seq into a PagesGroup.
-func ToPagesGroup(seq any) (PagesGroup, error) {
+func ToPagesGroup(seq any) (PagesGroup, bool, error) {
        switch v := seq.(type) {
        case nil:
-               return nil, nil
+               return nil, true, nil
        case PagesGroup:
-               return v, nil
+               return v, true, nil
        case []PageGroup:
-               return PagesGroup(v), nil
+               return PagesGroup(v), true, nil
        case []any:
                l := len(v)
                if l == 0 {
@@ -450,12 +450,12 @@ func ToPagesGroup(seq any) (PagesGroup, error) {
                                if pg, ok := ipg.(PageGroup); ok {
                                        pagesGroup[i] = pg
                                } else {
-                                       return nil, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
+                                       return nil, false, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
                                }
                        }
-                       return pagesGroup, nil
+                       return pagesGroup, true, nil
                }
        }
 
-       return nil, nil
+       return nil, false, nil
 }
index 46d9fda82a956f90b92b1252e64a1d7453c81b58..ddede792fe595b0e97abbad35f7a5d4a43650cb6 100644 (file)
@@ -277,11 +277,11 @@ func Paginate(td TargetPathDescriptor, seq any, pagerSize int) (*Paginator, erro
 
        var paginator *Paginator
 
-       groups, err := ToPagesGroup(seq)
+       groups, ok, err := ToPagesGroup(seq)
        if err != nil {
                return nil, err
        }
-       if groups != nil {
+       if ok {
                paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory)
        } else {
                pages, err := ToPages(seq)