8000 Don't fail when calling Paginate with an empty pages.PagesGroup · gohugoio/hugo@34a86e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34a86e1

Browse files
committed
Don't fail when calling Paginate with an empty pages.PagesGroup
Fixes #10802
1 parent 0f01bd4 commit 34a86e1

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

hugolib/paginator_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,29 @@ weight: %d
136136
b.Assert(b.CheckExists("public/page/1/index.json"), qt.Equals, false)
137137
b.AssertFileContent("public/page/2/index.json", `JSON: 22: |/p11/index.json|/p12/index.json`)
138138
}
139+
140+
// Issue 10802
141+
func TestPaginatorEmptyPageGroups(t *testing.T) {
142+
t.Parallel()
143+
144+
files := `
145+
-- config.toml --
146+
baseURL = "https://example.com/"
147+
-- content/p1.md --
148+
-- content/p2.md --
149+
-- layouts/index.html --
150+
{{ $empty := site.RegularPages | complement site.RegularPages }}
151+
Len: {{ len $empty }}: Type: {{ printf "%T" $empty }}
152+
{{ $pgs := $empty.GroupByPublishDate "January 2006" }}
153+
{{ $pag := .Paginate $pgs }}
154+
Len Pag: {{ len $pag.Pages }}
155+
`
156+
b := NewIntegrationTestBuilder(
157+
IntegrationTestConfig{
158+
T: t,
159+
TxtarString: files,
160+
},
161+
).Build()
162+
163+
b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
164+
}

resources/page/pagegroup.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,14 @@ func (psg PagesGroup) ProbablyEq(other any) bool {
430430
}
431431

432432
// ToPagesGroup tries to convert seq into a PagesGroup.
433-
func ToPagesGroup(seq any) (PagesGroup, error) {
433+
func ToPagesGroup(seq any) (PagesGroup, bool, error) {
434434
switch v := seq.(type) {
435435
case nil:
436-
return nil, nil
436+
return nil, true, nil
437437
case PagesGroup:
438-
return v, nil
438+
return v, true, nil
439439
case []PageGroup:
440-
return PagesGroup(v), nil
440+
return PagesGroup(v), true, nil
441441
case []any:
442442
l := len(v)
443443
if l == 0 {
@@ -450,12 +450,12 @@ func ToPagesGroup(seq any) (PagesGroup, error) {
450450
if pg, ok := ipg.(PageGroup); ok {
451451
pagesGroup[i] = pg
452452
} else {
453-
return nil, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
453+
return nil, false, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg)
454454
}
455455
}
456-
return pagesGroup, nil
456+
return pagesGroup, true, nil
457457
}
458458
}
459459

460-
return nil, nil
460+
return nil, false, nil
461461
}

resources/page/pagination.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ func Paginate(td TargetPathDescriptor, seq any, pagerSize int) (*Paginator, erro
277277

278278
var paginator *Paginator
279279

280-
groups, err := ToPagesGroup(seq)
280+
groups, ok, err := ToPagesGroup(seq)
281281
if err != nil {
282282
return nil, err
283283
}
284-
if groups != nil {
284+
if ok {
285285
paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory)
286286
} else {
287287
pages, err := ToPages(seq)

0 commit comments

Comments
 (0)
0