From 51f09b17fdbaae705f043d8e675a8bcdc3ad12c6 Mon Sep 17 00:00:00 2001 From: n1xx1 Date: Thu, 1 Aug 2024 13:12:32 +0200 Subject: [PATCH] allow nested params when using Pages.GroupByParam and Pages.GroupByParamDate --- resources/page/pagegroup_test.go | 60 ++++++++++++++++++++++++++ resources/resource/resource_helpers.go | 5 ++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/resources/page/pagegroup_test.go b/resources/page/pagegroup_test.go index 5008aa72016..1168e519e09 100644 --- a/resources/page/pagegroup_test.go +++ b/resources/page/pagegroup_test.go @@ -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 @@ -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) @@ -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() @@ -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) diff --git a/resources/resource/resource_helpers.go b/resources/resource/resource_helpers.go index 90075f983ab..c2bb463c82e 100644 --- a/resources/resource/resource_helpers.go +++ b/resources/resource/resource_helpers.go @@ -14,6 +14,7 @@ package resource import ( + "github.com/gohugoio/hugo/common/maps" "strings" "time" @@ -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 }