Skip to content

Commit c38fbe1

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 c38fbe1

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

Diff for: 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
}

Diff for: tpl/cast/cast_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ func TestToTruth(t *testing.T) {
194194
{template.JSStr("6"), true},
195195
{t, true},
196196
{nil, false},
197-
{"null", false},
198-
{"undefined", false},
199-
{"NaN", false},
197+
{"null", true},
198+
{"undefined", true},
199+
{"NaN", true},
200200
} {
201201
errMsg := qt.Commentf("[%d] %v", i, test.v)
202202

0 commit comments

Comments
 (0)