diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-09-09 10:15:11 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-09-10 09:19:01 +0200 |
commit | fe6676c775b8d917a661238f24fd4a9088f25d50 (patch) | |
tree | a84c05a70dc203392db4343a2185cfe0a5714140 /hugolib/pagination.go | |
parent | 7a97d3e6bca1e30826e1662b5f299b66aa8ab385 (diff) |
tpl/collections: Improve type handling in collections.Slice
Fixes #5188
Diffstat (limited to 'hugolib/pagination.go')
-rw-r--r-- | hugolib/pagination.go | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/hugolib/pagination.go b/hugolib/pagination.go index 9a9b10c48..c703f6c98 100644 --- a/hugolib/pagination.go +++ b/hugolib/pagination.go @@ -453,20 +453,34 @@ func toPages(seq interface{}) (Pages, error) { return Pages{}, nil } - switch seq.(type) { + switch v := seq.(type) { case Pages: - return seq.(Pages), nil + return v, nil case *Pages: - return *(seq.(*Pages)), nil + return *(v), nil case []*Page: - return Pages(seq.([]*Page)), nil + return Pages(v), nil case WeightedPages: - return (seq.(WeightedPages)).Pages(), nil + return v.Pages(), nil case PageGroup: - return (seq.(PageGroup)).Pages, nil - default: - return nil, fmt.Errorf("unsupported type in paginate, got %T", seq) + return v.Pages, nil + case []interface{}: + pages := make(Pages, len(v)) + success := true + for i, vv := range v { + p, ok := vv.(*Page) + if !ok { + success = false + break + } + pages[i] = p + } + if success { + return pages, nil + } } + + return nil, fmt.Errorf("cannot convert type %T to Pages", seq) } // probablyEqual checks page lists for probable equality. |