tpl/collections: Unwrap any interface value in sort and where

Hugo `0.55.0` introduced some new interface types for `Page` etc.

This worked great in general, but there were cases where this would fail in `where` and `sort`.

One such example would be sorting by `MenuItem.Page.Date` where `Page` on `MenuItem` was a small subset of the bigger `page.Page` interface.

This commit fixes that by unwrapping such interface values.

Fixes #5989
This commit is contained in:
Bjørn Erik Pedersen
2019-05-27 22:57:57 +02:00
parent fad183c4ae
commit 8d898ad667
4 changed files with 153 additions and 22 deletions

View File

@@ -819,6 +819,10 @@ func (x TstX) TstRv() string {
return "r" + x.B
}
func (x TstX) TstRv2() string {
return "r" + x.B
}
func (x TstX) unexportedMethod() string {
return x.unexported
}
@@ -850,6 +854,33 @@ type TstX struct {
unexported string
}
type TstXIHolder struct {
XI TstXI
}
// Partially implemented by the TstX struct.
type TstXI interface {
TstRv2() string
}
func ToTstXIs(slice interface{}) []TstXI {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
return nil
}
tis := make([]TstXI, s.Len())
for i := 0; i < s.Len(); i++ {
tsti, ok := s.Index(i).Interface().(TstXI)
if !ok {
return nil
}
tis[i] = tsti
}
return tis
}
func newDeps(cfg config.Provider) *deps.Deps {
l := langs.NewLanguage("en", cfg)
l.Set("i18nDir", "i18n")