Fix WeightedPages in union etc.
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 13 Apr 2019 09:40:51 +0000 (11:40 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 13 Apr 2019 16:52:38 +0000 (18:52 +0200)
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

hugolib/collections_test.go
hugolib/site.go
hugolib/taxonomy.go
resources/page/weighted.go

index bc55bdbe8c9e496fa36438dc252ae77575309601..b7e89eec9bbcb40cfffbd8c1149ab503c62e7754 100644 (file)
@@ -87,6 +87,37 @@ tags_weight: %d
                `weightedPages:2::page.WeightedPages:[WeightedPage(10,"Page") WeightedPage(20,"Page")]`)
 }
 
+func TestUnionFunc(t *testing.T) {
+       assert := require.New(t)
+
+       pageContent := `
+---
+title: "Page"
+tags: ["blue", "green"]
+tags_weight: %d
+---
+
+`
+       b := newTestSitesBuilder(t)
+       b.WithSimpleConfigFile().
+               WithContent("page1.md", fmt.Sprintf(pageContent, 10), "page2.md", fmt.Sprintf(pageContent, 20),
+                       "page3.md", fmt.Sprintf(pageContent, 30)).
+               WithTemplatesAdded("index.html", `
+{{ $unionPages := first 2 .Site.RegularPages | union .Site.RegularPages  }}
+{{ $unionWeightedPages := .Site.Taxonomies.tags.blue | union .Site.Taxonomies.tags.green }}
+{{ printf "unionPages: %T %d" $unionPages (len $unionPages) }} 
+{{ printf "unionWeightedPages: %T %d" $unionWeightedPages (len $unionWeightedPages) }}
+`)
+       b.CreateSites().Build(BuildCfg{})
+
+       assert.Equal(1, len(b.H.Sites))
+       require.Len(t, b.H.Sites[0].RegularPages(), 3)
+
+       b.AssertFileContent("public/index.html",
+               "unionPages: page.Pages 3",
+               "unionWeightedPages: page.WeightedPages 6")
+}
+
 func TestAppendFunc(t *testing.T) {
        assert := require.New(t)
 
index 616b5b37d075173a9095911fb1a2e750d71130f6..2653479ea090d1f1f87f155ac2fd5b077bf0a39f 100644 (file)
@@ -1584,7 +1584,7 @@ func (s *Site) assembleTaxonomies() error {
                        // last one will win, e.g. "hugo" vs "Hugo".
                        n.term = term
 
-                       w := page.NewWeightedPage(weight, p, n.getOwner)
+                       w := page.NewWeightedPage(weight, p, n.owner)
 
                        s.Taxonomies[plural].add(key, w)
 
index 9d9e4f9ecd1745858d020a02e03da439c502d15c..b91318ff1878fefe4c7c0515bfb7ba91bd6b0566 100644 (file)
@@ -175,7 +175,7 @@ type taxonomyNodeInfo struct {
        parent *taxonomyNodeInfo
 
        // Either of Kind taxonomyTerm (parent) or taxonomy
-       owner page.Page
+       owner *page.PageWrapper
 }
 
 func (t *taxonomyNodeInfo) UpdateFromPage(p page.Page) {
@@ -185,17 +185,12 @@ func (t *taxonomyNodeInfo) UpdateFromPage(p page.Page) {
 }
 
 func (t *taxonomyNodeInfo) TransferValues(p *pageState) {
-       t.owner = p
+       t.owner.Page = p
        if p.Lastmod().IsZero() && p.Date().IsZero() {
                p.m.Dates.UpdateDateAndLastmodIfAfter(t.dates)
        }
 }
 
-// callback sent to the child nodes.
-func (t *taxonomyNodeInfo) getOwner() page.Page {
-       return t.owner
-}
-
 // Maps either plural or plural/term to a taxonomy node.
 // TODO(bep) consolidate somehow with s.Taxonomies
 type taxonomyNodeInfos map[string]*taxonomyNodeInfo
@@ -216,6 +211,7 @@ func (t taxonomyNodeInfos) GetOrCreate(plural, termKey, term string) *taxonomyNo
                plural:  plural,
                termKey: termKey,
                term:    term,
+               owner:   &page.PageWrapper{}, // Page will be assigned later.
        }
 
        t[key] = n
index 0937b3f86170ccbfebfddee1cfddc745ba10f942..3f75bcc3cfe041c8990ec9fd9328e797eb8072fa 100644 (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 {