tpl: Treat booleans as set in default function
authorCameron Moore <moorereason@gmail.com>
Wed, 16 Mar 2016 17:45:50 +0000 (12:45 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 16 Mar 2016 19:05:48 +0000 (20:05 +0100)
Booleans and `default` don't really make sense together, so we'll always treat
booleans as "set" and return the given value.

docs/content/templates/functions.md
tpl/template_funcs.go
tpl/template_funcs_test.go

index b749e774c684f222ebb898ffaa2411fc1280ff7c..984efc9b14809fc6591a67df14ff7bc1854e38b5 100644 (file)
@@ -30,9 +30,9 @@ and other basic tools; these are listed in the
 
 ### default
 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
-times; non-zero length for strings, arrays, slices, and maps; any struct value;
-or non-nil for any other types.
+"Set" in this context means non-zero for numeric types and times;
+non-zero length for strings, arrays, slices, and maps;
+any boolean or struct value; or non-nil for any other types.
 
 e.g.
 
index b262586929d199890a0a3790a25ac05a044d8f25..b4a737fae9b9f322b50e807946f5431da94c7d11 100644 (file)
@@ -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
-// is not.  "Set" in this context means true for booleans; non-zero for numeric
-// types and times; non-zero length for strings, arrays, slices, and maps; any
-// struct value; or non-nil for any other types.
+// is not.  "Set" in this context means non-zero for numeric types and times;
+// non-zero length for strings, arrays, slices, and maps;
+// any boolean or struct value; or non-nil for any other types.
 func dfault(dflt interface{}, given ...interface{}) (interface{}, error) {
        // given is variadic because the following construct will not pass a piped
        // 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() {
        case reflect.Bool:
-               set = g.Bool()
+               set = true
        case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
                set = g.Len() != 0
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
index ccdf7207098e088cd91ef4bb55eb2c3994cc4c74..28b18c927885ca8e6e7ed85ec0ef0800bc405c11 100644 (file)
@@ -1886,6 +1886,7 @@ func TestDefaultFunc(t *testing.T) {
                given    interface{}
                expected interface{}
        }{
+               {true, false, false},
                {"5", 0, "5"},
 
                {"test1", "set", "set"},