Skip to content

Commit 38661c1

Browse files
moorereasonbep
authored andcommitted
tpl/collections: Log an error on unsupported types in IsSet
Unsupported types are currently silently ignored by IsSet. An earlier attempt was made to solve the issue by returning an error. That attempt was reverted since it broke some existing themes. So instead, we'll log an error. Hopefully, people will stop using IsSet in this way, and we can eventually return an error outright. Updates #3092
1 parent 42fbded commit 38661c1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

‎tpl/collections/collections.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ func (ns *Namespace) IsSet(a interface{}, key interface{}) (bool, error) {
370370
if kv.Type() == av.Type().Key() {
371371
return av.MapIndex(kv).IsValid(), nil
372372
}
373+
default:
374+
ns.deps.Log.ERROR.Printf("calling IsSet with unsupported type %T will always return false", a)
373375
}
374376

375377
return false, nil

‎tpl/collections/collections_test.go‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ import (
1717
"errors"
1818
"fmt"
1919
"html/template"
20+
"io/ioutil"
21+
"log"
2022
"math/rand"
23+
"os"
2124
"reflect"
2225
"testing"
2326
"time"
2427

28+
"github.com/spf13/hugo/config"
2529
"github.com/spf13/hugo/deps"
30+
"github.com/spf13/hugo/helpers"
31+
"github.com/spf13/hugo/hugofs"
32+
jww "github.com/spf13/jwalterweatherman"
33+
"github.com/spf13/viper"
2634
"github.com/stretchr/testify/assert"
2735
"github.com/stretchr/testify/require"
2836
)
@@ -320,7 +328,7 @@ func TestIntersect(t *testing.T) {
320328
func TestIsSet(t *testing.T) {
321329
t.Parallel()
322330

323-
ns := New(&deps.Deps{})
331+
ns := New(newDeps(viper.New()))
324332

325333
for i, test := range []struct {
326334
a interface{}
@@ -336,6 +344,7 @@ func TestIsSet(t *testing.T) {
336344
{map[string]interface{}{"a": 1, "b": 2}, "bc", false, false, ""},
337345

338346
{time.Now(), "Day", false, false, ""},
347+
{nil, "nil", false, false, ""},
339348
} {
340349
errMsg := fmt.Sprintf("[%d] %v", i, test)
341350

@@ -632,3 +641,14 @@ type TstX struct {
632641
A, B string
633642
unexported string
634643
}
644+
645+
func newDeps(cfg config.Provider) *deps.Deps {
646+
l := helpers.NewLanguage("en", cfg)
647+
l.Set("i18nDir", "i18n")
648+
return &deps.Deps{
649+
Cfg: cfg,
650+
Fs: hugofs.NewMem(l),
651+
ContentSpec: helpers.NewContentSpec(l),
652+
Log: jww.NewNotepad(jww.LevelError, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime),
653+
}
654+
}

0 commit comments

Comments
 (0)