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

@@ -280,8 +280,16 @@ func evaluateSubElem(obj reflect.Value, elemName string) (reflect.Value, error)
typ := obj.Type()
obj, isNil := indirect(obj)
if obj.Kind() == reflect.Interface {
// If obj is an interface, we need to inspect the value it contains
// to see the full set of methods and fields.
// Indirect returns the value that it points to, which is what's needed
// below to be able to reflect on its fields.
obj = reflect.Indirect(obj.Elem())
}
// first, check whether obj has a method. In this case, obj is
// an interface, a struct or its pointer. If obj is a struct,
// a struct or its pointer. If obj is a struct,
// to check all T and *T method, use obj pointer type Value
objPtr := obj
if objPtr.Kind() != reflect.Interface && objPtr.CanAddr() {