resources/page: Implement compare.ProbablyEqer for the core slices

Fixes #5808
This commit is contained in:
Bjørn Erik Pedersen
2019-04-02 10:52:43 +02:00
parent 5185fb065b
commit e91e222cd2
3 changed files with 125 additions and 1 deletions

View File

@@ -17,11 +17,14 @@ import (
"fmt"
"math/rand"
"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/resources/resource"
)
var (
_ resource.ResourcesConverter = Pages{}
_ compare.ProbablyEqer = Pages{}
)
// 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)
}
// 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 {
ii := -1
for i, pp := range ps {