Skip to content

Commit 858c07a

Browse files
committed
tpl: updates cast.ToTruth to fall back to hreflect.IsTruthful
Concession from review, where cast.ToTruth should fall back to behavior from hreflect.IsTruthful if the cast.ToBool was unsuccessful.
1 parent 5a72de2 commit 858c07a

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

tpl/cast/cast.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cast
1717
import (
1818
"html/template"
1919

20+
"github.com/gohugoio/hugo/common/hreflect"
2021
_cast "github.com/spf13/cast"
2122
)
2223

@@ -58,12 +59,7 @@ func (ns *Namespace) ToBool(v any) (bool, error) {
5859
func (ns *Namespace) ToTruth(v any) (bool, error) {
5960
result, err := ns.ToBool(v)
6061
if err != nil {
61-
switch v {
62-
case "", "nil", "null", "undefined", "NaN":
63-
return false, nil
64-
default:
65-
return true, nil
66-
}
62+
return hreflect.IsTruthful(v), nil
6763
}
6864
return result, nil
6965
}

tpl/cast/cast_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ func TestToBool(t *testing.T) {
147147
// error cases
148148
{"cheese", nil, false},
149149
{"", nil, false},
150-
{1.67, nil, false},
151150
} {
152151
errMsg := qt.Commentf("[%d] %v", i, test.v)
153152

@@ -194,9 +193,9 @@ func TestToTruth(t *testing.T) {
194193
{template.JSStr("6"), true},
195194
{t, true},
196195
{nil, false},
197-
{"null", false},
198-
{"undefined", false},
199-
{"NaN", false},
196+
{"null", true},
197+
{"undefined", true},
198+
{"NaN", true},
200199
} {
201200
errMsg := qt.Commentf("[%d] %v", i, test.v)
202201

0 commit comments

Comments
 (0)