mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
resources/page: Implement compare.ProbablyEqer for the core slices
Fixes #5808
This commit is contained in:
parent
5185fb065b
commit
e91e222cd2
3 changed files with 125 additions and 1 deletions
|
@ -22,12 +22,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/common/collections"
|
"github.com/gohugoio/hugo/common/collections"
|
||||||
|
"github.com/gohugoio/hugo/compare"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/resources/resource"
|
"github.com/gohugoio/hugo/resources/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ collections.Slicer = PageGroup{}
|
_ collections.Slicer = PageGroup{}
|
||||||
|
_ compare.ProbablyEqer = PageGroup{}
|
||||||
|
_ compare.ProbablyEqer = PagesGroup{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// PageGroup represents a group of pages, grouped by the key.
|
// PageGroup represents a group of pages, grouped by the key.
|
||||||
|
@ -307,6 +310,21 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
|
||||||
return p.groupByDateField(sorter, formatter, order...)
|
return p.groupByDateField(sorter, formatter, order...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProbablyEq wraps comare.ProbablyEqer
|
||||||
|
func (p PageGroup) ProbablyEq(other interface{}) bool {
|
||||||
|
otherP, ok := other.(PageGroup)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.Key != otherP.Key {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.Pages.ProbablyEq(otherP.Pages)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Slice is not meant to be used externally. It's a bridge function
|
// Slice is not meant to be used externally. It's a bridge function
|
||||||
// for the template functions. See collections.Slice.
|
// for the template functions. See collections.Slice.
|
||||||
func (p PageGroup) Slice(in interface{}) (interface{}, error) {
|
func (p PageGroup) Slice(in interface{}) (interface{}, error) {
|
||||||
|
@ -337,6 +355,27 @@ func (psg PagesGroup) Len() int {
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProbablyEq wraps comare.ProbablyEqer
|
||||||
|
func (psg PagesGroup) ProbablyEq(other interface{}) bool {
|
||||||
|
otherPsg, ok := other.(PagesGroup)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(psg) != len(otherPsg) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range psg {
|
||||||
|
if !psg[i].ProbablyEq(otherPsg[i]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// ToPagesGroup tries to convert seq into a PagesGroup.
|
// ToPagesGroup tries to convert seq into a PagesGroup.
|
||||||
func ToPagesGroup(seq interface{}) (PagesGroup, error) {
|
func ToPagesGroup(seq interface{}) (PagesGroup, error) {
|
||||||
switch v := seq.(type) {
|
switch v := seq.(type) {
|
||||||
|
|
|
@ -17,11 +17,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/gohugoio/hugo/compare"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/resources/resource"
|
"github.com/gohugoio/hugo/resources/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ resource.ResourcesConverter = Pages{}
|
_ resource.ResourcesConverter = Pages{}
|
||||||
|
_ compare.ProbablyEqer = Pages{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pages is a slice of pages. This is the most common list type in Hugo.
|
// Pages is a slice of pages. This is the most common list type in Hugo.
|
||||||
|
@ -95,6 +98,33 @@ func (p Pages) Len() int {
|
||||||
return len(p)
|
return len(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProbablyEq wraps comare.ProbablyEqer
|
||||||
|
func (pages Pages) ProbablyEq(other interface{}) bool {
|
||||||
|
otherPages, ok := other.(Pages)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(pages) != len(otherPages) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
step := 1
|
||||||
|
|
||||||
|
for i := 0; i < len(pages); i += step {
|
||||||
|
if !pages[i].Eq(otherPages[i]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if i > 50 {
|
||||||
|
// This is most likely the same.
|
||||||
|
step = 50
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (ps Pages) removeFirstIfFound(p Page) Pages {
|
func (ps Pages) removeFirstIfFound(p Page) Pages {
|
||||||
ii := -1
|
ii := -1
|
||||||
for i, pp := range ps {
|
for i, pp := range ps {
|
||||||
|
|
55
resources/page/pages_test.go
Normal file
55
resources/page/pages_test.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package page
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProbablyEq(t *testing.T) {
|
||||||
|
|
||||||
|
p1, p2, p3 := &testPage{title: "p1"}, &testPage{title: "p2"}, &testPage{title: "p3"}
|
||||||
|
pages12 := Pages{p1, p2}
|
||||||
|
pages21 := Pages{p2, p1}
|
||||||
|
pages123 := Pages{p1, p2, p3}
|
||||||
|
|
||||||
|
t.Run("Pages", func(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
|
||||||
|
assert.True(pages12.ProbablyEq(pages12))
|
||||||
|
assert.False(pages123.ProbablyEq(pages12))
|
||||||
|
assert.False(pages12.ProbablyEq(pages21))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("PageGroup", func(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
|
||||||
|
assert.True(PageGroup{Key: "a", Pages: pages12}.ProbablyEq(PageGroup{Key: "a", Pages: pages12}))
|
||||||
|
assert.False(PageGroup{Key: "a", Pages: pages12}.ProbablyEq(PageGroup{Key: "b", Pages: pages12}))
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("PagesGroup", func(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
|
||||||
|
pg1, pg2 := PageGroup{Key: "a", Pages: pages12}, PageGroup{Key: "b", Pages: pages123}
|
||||||
|
|
||||||
|
assert.True(PagesGroup{pg1, pg2}.ProbablyEq(PagesGroup{pg1, pg2}))
|
||||||
|
assert.False(PagesGroup{pg1, pg2}.ProbablyEq(PagesGroup{pg2, pg1}))
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue