Skip to content

Commit

Permalink
Improve handling of <nil> Params
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 30, 2021
1 parent 7907d24 commit 3b10543
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
6 changes: 6 additions & 0 deletions common/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"fmt"
"strings"

"github.com/gohugoio/hugo/common/types"

"github.com/gobwas/glob"
"github.com/spf13/cast"
)
Expand All @@ -39,8 +41,12 @@ func ToStringMapE(in interface{}) (map[string]interface{}, error) {
}

// ToParamsAndPrepare converts in to Params and prepares it for use.
// If in is nil, an empty map is returned.
// See PrepareParams.
func ToParamsAndPrepare(in interface{}) (Params, bool) {
if types.IsNil(in) {
return Params{}, true
}
m, err := ToStringMapE(in)
if err != nil {
return nil, false
Expand Down
10 changes: 10 additions & 0 deletions common/maps/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func TestToSliceStringMap(t *testing.T) {
}
}

func TestToParamsAndPrepare(t *testing.T) {
c := qt.New(t)
_, ok := ToParamsAndPrepare(map[string]interface{}{"A": "av"})
c.Assert(ok, qt.IsTrue)

params, ok := ToParamsAndPrepare(nil)
c.Assert(ok, qt.IsTrue)
c.Assert(params, qt.DeepEquals, Params{})
}

func TestRenameKeys(t *testing.T) {
c := qt.New(t)

Expand Down
18 changes: 13 additions & 5 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,17 +1400,25 @@ func (s *Site) getMenusFromConfig() navigation.Menus {
s.Log.Errorf("unable to process menus in site config\n")
s.Log.Errorln(err)
} else {
handleErr := func(err error) {
if err == nil {
return
}
s.Log.Errorf("unable to process menus in site config\n")
s.Log.Errorln(err)

}

for _, entry := range m {
s.Log.Debugf("found menu: %q, in site config\n", name)

menuEntry := navigation.MenuEntry{Menu: name}
ime, err := maps.ToStringMapE(entry)
if err != nil {
s.Log.Errorf("unable to process menus in site config\n")
s.Log.Errorln(err)
}
handleErr(err)

err = menuEntry.MarshallMap(ime)
handleErr(err)

menuEntry.MarshallMap(ime)
// TODO(bep) clean up all of this
menuEntry.ConfiguredURL = s.Info.createNodeMenuEntryURL(menuEntry.ConfiguredURL)

Expand Down
20 changes: 17 additions & 3 deletions navigation/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
package navigation

import (
"fmt"
"html/template"
"sort"
"strings"

"github.com/pkg/errors"

"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/compare"
Expand Down Expand Up @@ -65,6 +68,7 @@ func (m *MenuEntry) URL() string {
type Page interface {
LinkTitle() string
RelPermalink() string
Path() string
Section() string
Weight() int
IsPage() bool
Expand Down Expand Up @@ -127,7 +131,8 @@ func (m *MenuEntry) isSamePage(p Page) bool {
return false
}

func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
func (m *MenuEntry) MarshallMap(ime map[string]interface{}) error {
var err error
for k, v := range ime {
loki := strings.ToLower(k)
switch loki {
Expand All @@ -150,10 +155,19 @@ func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
case "parent":
m.Parent = cast.ToString(v)
case "params":
m.Params = maps.MustToParamsAndPrepare(v)

var ok bool
m.Params, ok = maps.ToParamsAndPrepare(v)
if !ok {
err = fmt.Errorf("cannot convert %T to Params", v)
}
}
}

if err != nil {
return errors.Wrapf(err, "failed to marshal menu entry %q", m.KeyName())
}

return nil
}

func (m Menu) Add(me *MenuEntry) Menu {
Expand Down
12 changes: 9 additions & 3 deletions navigation/pagemenus.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,27 @@ func PageMenusFromPage(p Page) (PageMenus, error) {
return pm, nil
}

var wrapErr = func(err error) error {
return errors.Wrapf(err, "unable to process menus for page %q", p.Path())
}

// Could be a structured menu entry
menus, err := maps.ToStringMapE(ms)
if err != nil {
return pm, errors.Wrapf(err, "unable to process menus for %q", p.LinkTitle())
return pm, wrapErr(err)
}

for name, menu := range menus {
menuEntry := MenuEntry{Page: p, Name: p.LinkTitle(), Weight: p.Weight(), Menu: name}
if menu != nil {
ime, err := maps.ToStringMapE(menu)
if err != nil {
return pm, errors.Wrapf(err, "unable to process menus for %q", p.LinkTitle())
return pm, wrapErr(err)
}

menuEntry.MarshallMap(ime)
if err = menuEntry.MarshallMap(ime); err != nil {
return pm, wrapErr(err)
}
}
pm[name] = &menuEntry
}
Expand Down

0 comments on commit 3b10543

Please sign in to comment.