diff --git a/defaults.go b/defaults.go index b9f97966..5d250565 100644 --- a/defaults.go +++ b/defaults.go @@ -6,6 +6,7 @@ import ( "math/rand" "reflect" "strings" + "text/template" "time" ) @@ -154,8 +155,8 @@ func mustToRawJson(v interface{}) (string, error) { } // ternary returns the first value if the last value is true, otherwise returns the second value. -func ternary(vt interface{}, vf interface{}, v bool) interface{} { - if v { +func ternary(vt interface{}, vf interface{}, v interface{}) interface{} { + if truth, ok := template.IsTrue(v); ok && truth { return vt } diff --git a/defaults_test.go b/defaults_test.go index a35ebf62..8bd6292f 100644 --- a/defaults_test.go +++ b/defaults_test.go @@ -193,4 +193,24 @@ func TestTernary(t *testing.T) { if err := runt(tpl, "bar"); err != nil { t.Error(err) } + + tpl = `{{ternary "foo" "bar" "baz"}}` + if err := runt(tpl, "foo"); err != nil { + t.Error(err) + } + + tpl = `{{"baz" | ternary "foo" "bar"}}` + if err := runt(tpl, "foo"); err != nil { + t.Error(err) + } + + tpl = `{{ternary "foo" "bar" ""}}` + if err := runt(tpl, "bar"); err != nil { + t.Error(err) + } + + tpl = `{{"" | ternary "foo" "bar"}}` + if err := runt(tpl, "bar"); err != nil { + t.Error(err) + } }