Fix WeightedPages in union etc.

We introduced a callback func() to get the owner Page in 0.55.0.

Sadly, funcs is  not comparable type in Go.

This commit replaces the func with a struct pointer that wraps the Page.

Fixes #5850
This commit is contained in:
Bjørn Erik Pedersen
2019-04-13 11:40:51 +02:00
parent e85c057f99
commit f2795d4d2c
4 changed files with 46 additions and 14 deletions

View File

@@ -38,11 +38,11 @@ func (p WeightedPages) Page() Page {
first := p[0]
// TODO(bep) fix tests
if first.getOwner == nil {
if first.owner == nil {
return nil
}
return first.getOwner()
return first.owner.Page
}
// A WeightedPage is a Page with a weight.
@@ -50,15 +50,20 @@ type WeightedPage struct {
Weight int
Page
// A callback used to fetch the owning Page. This avoids having to do
// Reference to the owning Page. This avoids having to do
// manual .Site.GetPage lookups. It is implemented in this roundabout way
// because we cannot add additional state to the WeightedPages slice
// without breaking lots of templates in the wild.
getOwner func() Page
owner *PageWrapper
}
func NewWeightedPage(weight int, p Page, getOwner func() Page) WeightedPage {
return WeightedPage{Weight: weight, Page: p, getOwner: getOwner}
// PageWrapper wraps a Page.
type PageWrapper struct {
Page
}
func NewWeightedPage(weight int, p Page, owner *PageWrapper) WeightedPage {
return WeightedPage{Weight: weight, Page: p, owner: owner}
}
func (w WeightedPage) String() string {