hugolib: Enhance .Param to permit arbitrarily nested parameter references

The Param method currently assumes that its argument is a single,
distinct, top-level key to look up in the Params map. This enhances the
Param method; it will now also attempt to see if the key can be
interpreted as a nested chain of keys to look up in Params.

Fixes #2598
This commit is contained in:
John Feminella 2017-02-19 02:50:08 -05:00 committed by Bjørn Erik Pedersen
parent 6d2281c8ea
commit b2e3748a4e
6 changed files with 142 additions and 11 deletions

View file

@ -20,7 +20,6 @@ import (
"testing"
"time"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
)
@ -121,11 +120,11 @@ func TestPageSortReverse(t *testing.T) {
func TestPageSortByParam(t *testing.T) {
t.Parallel()
var k interface{} = "arbitrary"
var k interface{} = "arbitrarily.nested"
s := newTestSite(t)
unsorted := createSortTestPages(s, 10)
delete(unsorted[9].Params, cast.ToString(k))
delete(unsorted[9].Params, "arbitrarily")
firstSetValue, _ := unsorted[0].Param(k)
secondSetValue, _ := unsorted[1].Param(k)
@ -137,7 +136,7 @@ func TestPageSortByParam(t *testing.T) {
assert.Equal(t, "xyz92", lastSetValue)
assert.Equal(t, nil, unsetValue)
sorted := unsorted.ByParam("arbitrary")
sorted := unsorted.ByParam("arbitrarily.nested")
firstSetSortedValue, _ := sorted[0].Param(k)
secondSetSortedValue, _ := sorted[1].Param(k)
lastSetSortedValue, _ := sorted[8].Param(k)
@ -182,7 +181,9 @@ func createSortTestPages(s *Site, num int) Pages {
for i := 0; i < num; i++ {
p := s.newPage(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))
p.Params = map[string]interface{}{
"arbitrary": "xyz" + fmt.Sprintf("%v", 100-i),
"arbitrarily": map[string]interface{}{
"nested": ("xyz" + fmt.Sprintf("%v", 100-i)),
},
}
w := 5