mirror of
https://github.com/gohugoio/hugo.git
synced 2025-02-17 02:11:32 +00:00
tpl: Treat booleans as set in default function
Booleans and `default` don't really make sense together, so we'll always treat booleans as "set" and return the given value.
This commit is contained in:
parent
b5c718a4de
commit
b0326a1c0f
3 changed files with 8 additions and 7 deletions
|
@ -30,9 +30,9 @@ and other basic tools; these are listed in the
|
||||||
|
|
||||||
### default
|
### default
|
||||||
Checks whether a given value is set and returns a default value if it is not.
|
Checks whether a given value is set and returns a default value if it is not.
|
||||||
"Set" in this context means true for booleans; non-zero for numeric types and
|
"Set" in this context means non-zero for numeric types and times;
|
||||||
times; non-zero length for strings, arrays, slices, and maps; any struct value;
|
non-zero length for strings, arrays, slices, and maps;
|
||||||
or non-nil for any other types.
|
any boolean or struct value; or non-nil for any other types.
|
||||||
|
|
||||||
e.g.
|
e.g.
|
||||||
|
|
||||||
|
|
|
@ -1318,9 +1318,9 @@ func dateFormat(layout string, v interface{}) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// dfault checks whether a given value is set and returns a default value if it
|
// dfault checks whether a given value is set and returns a default value if it
|
||||||
// is not. "Set" in this context means true for booleans; non-zero for numeric
|
// is not. "Set" in this context means non-zero for numeric types and times;
|
||||||
// types and times; non-zero length for strings, arrays, slices, and maps; any
|
// non-zero length for strings, arrays, slices, and maps;
|
||||||
// struct value; or non-nil for any other types.
|
// any boolean or struct value; or non-nil for any other types.
|
||||||
func dfault(dflt interface{}, given ...interface{}) (interface{}, error) {
|
func dfault(dflt interface{}, given ...interface{}) (interface{}, error) {
|
||||||
// given is variadic because the following construct will not pass a piped
|
// given is variadic because the following construct will not pass a piped
|
||||||
// argument when the key is missing: {{ index . "key" | default "foo" }}
|
// argument when the key is missing: {{ index . "key" | default "foo" }}
|
||||||
|
@ -1342,7 +1342,7 @@ func dfault(dflt interface{}, given ...interface{}) (interface{}, error) {
|
||||||
|
|
||||||
switch g.Kind() {
|
switch g.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
set = g.Bool()
|
set = true
|
||||||
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
|
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
|
||||||
set = g.Len() != 0
|
set = g.Len() != 0
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
|
|
@ -1886,6 +1886,7 @@ func TestDefaultFunc(t *testing.T) {
|
||||||
given interface{}
|
given interface{}
|
||||||
expected interface{}
|
expected interface{}
|
||||||
}{
|
}{
|
||||||
|
{true, false, false},
|
||||||
{"5", 0, "5"},
|
{"5", 0, "5"},
|
||||||
|
|
||||||
{"test1", "set", "set"},
|
{"test1", "set", "set"},
|
||||||
|
|
Loading…
Reference in a new issue