Skip to content

Commit

Permalink
allow nested params when using Pages.GroupByParam and Pages.GroupByPa…
Browse files Browse the repository at this point in the history
…ramDate
  • Loading branch information
n1xx1 authored and bep committed Aug 2, 2024
1 parent be64358 commit 51f09b1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
60 changes: 60 additions & 0 deletions resources/page/pagegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func preparePageGroupTestPages(t *testing.T) Pages {
p.params["custom_param"] = src.param
p.params["custom_date"] = cast.ToTime(src.date)
p.params["custom_string_date"] = src.date
p.params["custom_object"] = map[string]any{
"param": src.param,
"date": cast.ToTime(src.date),
"string_date": src.date,
}
pages = append(pages, p)
}
return pages
Expand Down Expand Up @@ -252,6 +257,25 @@ func TestGroupByParamCalledWithUnavailableParam(t *testing.T) {
}
}

func TestGroupByParamNested(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)

expect := PagesGroup{
{Key: "bar", Pages: Pages{pages[1], pages[3]}},
{Key: "baz", Pages: Pages{pages[4]}},
{Key: "foo", Pages: Pages{pages[0], pages[2]}},
}

groups, err := pages.GroupByParam("custom_object.param")
if err != nil {
t.Fatalf("Unable to make PagesGroup array: %s", err)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
}

func TestGroupByDate(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)
Expand Down Expand Up @@ -372,6 +396,24 @@ func TestGroupByParamDate(t *testing.T) {
}
}

func TestGroupByParamDateNested(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)
expect := PagesGroup{
{Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
{Key: "2012-03", Pages: Pages{pages[3]}},
{Key: "2012-01", Pages: Pages{pages[1]}},
}

groups, err := pages.GroupByParamDate("custom_object.date", "2006-01")
if err != nil {
t.Fatalf("Unable to make PagesGroup array: %s", err)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
}

// https://github.com/gohugoio/hugo/issues/3983
func TestGroupByParamDateWithStringParams(t *testing.T) {
t.Parallel()
Expand All @@ -391,6 +433,24 @@ func TestGroupByParamDateWithStringParams(t *testing.T) {
}
}

func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)
expect := PagesGroup{
{Key: "2012-04", Pages: Pages{pages[4], pages[2], pages[0]}},
{Key: "2012-03", Pages: Pages{pages[3]}},
{Key: "2012-01", Pages: Pages{pages[1]}},
}

groups, err := pages.GroupByParamDate("custom_object.string_date", "2006-01")
if err != nil {
t.Fatalf("Unable to make PagesGroup array: %s", err)
}
if !reflect.DeepEqual(groups, expect) {
t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
}
}

func TestGroupByLastmod(t *testing.T) {
t.Parallel()
pages := preparePageGroupTestPages(t)
Expand Down
5 changes: 3 additions & 2 deletions resources/resource/resource_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package resource

import (
"github.com/gohugoio/hugo/common/maps"
"strings"
"time"

Expand All @@ -36,9 +37,9 @@ func GetParamToLower(r Resource, key string) any {
}

func getParam(r Resource, key string, stringToLower bool) any {
v := r.Params()[strings.ToLower(key)]
v, err := maps.GetNestedParam(key, ".", r.Params())

if v == nil {
if v == nil || err != nil {
return nil
}

Expand Down

0 comments on commit 51f09b1

Please sign in to comment.