Fix Params case handling in the index, sort and where func

This means that you can now do:

```
{{ range where .Site.Pages "Params.MYPARAM" "foo" }}
```
This commit is contained in:
Bjørn Erik Pedersen
2019-11-21 21:59:38 +01:00
parent cd07e6d57b
commit a3fe5e5e35
33 changed files with 317 additions and 155 deletions

View File

@@ -19,6 +19,7 @@ import (
"sort"
"strings"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/tpl/compare"
"github.com/spf13/cast"
)
@@ -75,11 +76,19 @@ func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, er
} else {
v := p.Pairs[i].Value
var err error
for _, elemName := range path {
for i, elemName := range path {
v, err = evaluateSubElem(v, elemName)
if err != nil {
return nil, err
}
if !v.IsValid() {
continue
}
// Special handling of lower cased maps.
if params, ok := v.Interface().(maps.Params); ok {
v = reflect.ValueOf(params.Get(path[i+1:]...))
break
}
}
p.Pairs[i].Key = v
}
@@ -89,6 +98,7 @@ func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, er
keys := seqv.MapKeys()
for i := 0; i < seqv.Len(); i++ {
p.Pairs[i].Value = seqv.MapIndex(keys[i])
if sortByField == "" {
p.Pairs[i].Key = keys[i]
} else if sortByField == "value" {
@@ -96,11 +106,19 @@ func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, er
} else {
v := p.Pairs[i].Value
var err error
for _, elemName := range path {
for i, elemName := range path {
v, err = evaluateSubElem(v, elemName)
if err != nil {
return nil, err
}
if !v.IsValid() {
continue
}
// Special handling of lower cased maps.
if params, ok := v.Interface().(maps.Params); ok {
v = reflect.ValueOf(params.Get(path[i+1:]...))
break
}
}
p.Pairs[i].Key = v
}
@@ -135,6 +153,7 @@ func (p pairList) Less(i, j int) bool {
// can only call Interface() on valid reflect Values
return sortComp.Lt(iv.Interface(), jv.Interface())
}
// if j is invalid, test i against i's zero value
return sortComp.Lt(iv.Interface(), reflect.Zero(iv.Type()))
}