From 58f31d2769ec63aa8dfd4eaad47703ce09b57a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 17 Oct 2016 19:30:21 +0200 Subject: [PATCH] Consolidate the Param methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Maps in Viper, Hugo's config backing store, is now properly lower-cased not just on top level, the current situation. While this is mostly a good thing, as you don't need to know the original casing to look up a value, it will be breaking for people doing direct lookups in the ´Site.Params` map. We will try to find a solution to this "breakage", but the recommended method to get params values is via the `.Param` methods. This method is now implemented on `Node`, `Page` and `Site` and is case-insensitive: * Use `.Param "someKey" ` if you want page param with fall back to site param if not found on page. * Use `.Site.Param "someKey"` to get a site param See #2590 --- hugolib/node.go | 10 ++-------- hugolib/page.go | 3 ++- hugolib/site.go | 13 +++++++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/hugolib/node.go b/hugolib/node.go index 566fd4799..eefb78bec 100644 --- a/hugolib/node.go +++ b/hugolib/node.go @@ -24,8 +24,6 @@ import ( jww "github.com/spf13/jwalterweatherman" "github.com/spf13/hugo/helpers" - - "github.com/spf13/cast" ) type Node struct { @@ -124,13 +122,9 @@ func (n *Node) IsMenuCurrent(menuID string, inme *MenuEntry) bool { // Param is a convenience method to do lookups in Site's Params map. // -// This method is also implemented on Page. +// This method is also implemented on Page and SiteInfo. func (n *Node) Param(key interface{}) (interface{}, error) { - keyStr, err := cast.ToStringE(key) - if err != nil { - return nil, err - } - return n.Site.Params[keyStr], err + return n.Site.Param(key) } func (n *Node) Hugo() *HugoInfo { diff --git a/hugolib/page.go b/hugolib/page.go index 99ef6e413..508bf252a 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -194,12 +194,13 @@ func (p *Page) IsPage() bool { // Param is a convenience method to do lookups in Page's and Site's Params map, // in that order. // -// This method is also implemented on Node. +// This method is also implemented on Node and SiteInfo. func (p *Page) Param(key interface{}) (interface{}, error) { keyStr, err := cast.ToStringE(key) if err != nil { return nil, err } + keyStr = strings.ToLower(keyStr) if val, ok := p.Params[keyStr]; ok { return val, nil } diff --git a/hugolib/site.go b/hugolib/site.go index 7b74aa02f..f6a787298 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -223,8 +223,21 @@ func newSiteInfoDefaultLanguage(baseURL string, pages ...*Page) *SiteInfo { // linkedin type SiteSocial map[string]string +// Param is a convenience method to do lookups in Site's Params map. +// +// This method is also implemented on Page and Node. +func (s *SiteInfo) Param(key interface{}) (interface{}, error) { + keyStr, err := cast.ToStringE(key) + if err != nil { + return nil, err + } + keyStr = strings.ToLower(keyStr) + return s.Params[keyStr], nil +} + // GetParam gets a site parameter value if found, nil if not. func (s *SiteInfo) GetParam(key string) interface{} { + helpers.Deprecated("SiteInfo", ".GetParam", ".Param") v := s.Params[strings.ToLower(key)] if v == nil {