compare, hugolib, tpl: Add Eqer interface

And use it in `eq` and `ne` so `Page` values can be compared directly in the templates without thinking about it being a `Page` or a `PageOutput` wrapper.

Fixes #3807
This commit is contained in:
Bjørn Erik Pedersen
2017-08-17 10:24:17 +02:00
parent 2fc121ce23
commit 08f48b91d6
4 changed files with 59 additions and 0 deletions

View File

@@ -18,6 +18,8 @@ import (
"reflect"
"strconv"
"time"
"github.com/gohugoio/hugo/compare"
)
// New returns a new instance of the compare-namespaced template functions.
@@ -85,6 +87,14 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{},
// Eq returns the boolean truth of arg1 == arg2.
func (*Namespace) Eq(x, y interface{}) bool {
// hugolib.Page implements compare.Eqer to make Page and PageOutput comparable.
if e1, ok := x.(compare.Eqer); ok {
if e2, ok := y.(compare.Eqer); ok {
return e1.Eq(e2)
}
}
normalize := func(v interface{}) interface{} {
vv := reflect.ValueOf(v)
switch vv.Kind() {