diff options
author | n1xx1 <[email protected]> | 2024-08-01 13:12:32 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-08-02 10:03:20 +0200 |
commit | 51f09b17fdbaae705f043d8e675a8bcdc3ad12c6 (patch) | |
tree | c764400fdcb1f9429b559e9f36945fc116fda529 /resources | |
parent | be643580dd3a0d25be92cb2759f21b628ad80821 (diff) | |
download | hugo-51f09b17fdbaae705f043d8e675a8bcdc3ad12c6.tar.gz hugo-51f09b17fdbaae705f043d8e675a8bcdc3ad12c6.zip |
allow nested params when using Pages.GroupByParam and Pages.GroupByParamDate
Diffstat (limited to 'resources')
-rw-r--r-- | resources/page/pagegroup_test.go | 60 | ||||
-rw-r--r-- | 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 5008aa720..1168e519e 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 90075f983..c2bb463c8 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 } |