From e393c6290e827111a8a2e486791dc21f63a92b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 28 Jul 2019 12:31:32 +0200 Subject: [PATCH] common/maps: Do not return error on params dot access on incompatible types This error was introduced in 0.56 and has shown some site breakage in the wild. Fixes #6121 --- common/maps/params.go | 4 +-- common/maps/params_test.go | 6 +++++ hugolib/page_test.go | 55 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/common/maps/params.go b/common/maps/params.go index 0b81057b1..2d62ad752 100644 --- a/common/maps/params.go +++ b/common/maps/params.go @@ -16,8 +16,6 @@ package maps import ( "strings" - "github.com/pkg/errors" - "github.com/spf13/cast" ) @@ -72,7 +70,7 @@ func traverseNestedParams(keySegments []string, lookupFn func(key string) interf v, key, owner := traverseParams(rest, m) return v, key, owner, nil default: - return nil, "", nil, errors.Errorf("unsupported Params type: %T", result) + return nil, "", nil, nil } } diff --git a/common/maps/params_test.go b/common/maps/params_test.go index 89b149617..7443553f1 100644 --- a/common/maps/params_test.go +++ b/common/maps/params_test.go @@ -22,10 +22,14 @@ import ( func TestGetNestedParam(t *testing.T) { m := map[string]interface{}{ + "string": "value", "first": 1, "with_underscore": 2, "nested": map[string]interface{}{ "color": "blue", + "nestednested": map[string]interface{}{ + "color": "green", + }, }, } @@ -41,5 +45,7 @@ func TestGetNestedParam(t *testing.T) { assert.Equal(1, must("First", "_", m)) assert.Equal(2, must("with_underscore", "_", m)) assert.Equal("blue", must("nested_color", "_", m)) + assert.Equal("green", must("nested.nestednested.color", ".", m)) + assert.Nil(must("string.name", ".", m)) } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index e754a5e4f..3a0a76c3d 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1543,3 +1543,58 @@ title: Scratch Me! b.AssertFileContent("public/index.html", "B: bv") b.AssertFileContent("public/scratchme/index.html", "C: cv") } + +func TestPageParam(t *testing.T) { + t.Parallel() + + b := newTestSitesBuilder(t).WithConfigFile("toml", ` + +baseURL = "https://example.org" + +[params] +[params.author] + name = "Kurt Vonnegut" + +`) + b.WithTemplatesAdded("index.html", ` + +{{ $withParam := .Site.GetPage "withparam" }} +{{ $noParam := .Site.GetPage "noparam" }} +{{ $withStringParam := .Site.GetPage "withstringparam" }} + +Author page: {{ $withParam.Param "author.name" }} +Author page string: {{ $withStringParam.Param "author.name" }}| +Author site config: {{ $noParam.Param "author.name" }} + +`, + ) + + b.WithContent("withparam.md", ` ++++ +title = "With Param!" +[author] + name = "Ernest Miller Hemingway" + ++++ + +`, + + "noparam.md", ` +--- +title: "No Param!" +--- +`, "withstringparam.md", ` ++++ +title = "With string Param!" +author = "Jo Nesbø" + ++++ + +`) + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", "Author page: Ernest Miller Hemingway") + b.AssertFileContent("public/index.html", "Author page string: |") + b.AssertFileContent("public/index.html", "Author site config: Kurt Vonnegut") + +}