]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: export isTrue
authorRob Pike <r@golang.org>
Tue, 15 Sep 2015 16:27:22 +0000 (09:27 -0700)
committerRob Pike <r@golang.org>
Thu, 17 Sep 2015 23:08:33 +0000 (23:08 +0000)
The definition of 'truth' used by if etc. is not trivial to compute, so publish
the implementation to allow custom template functions to have the
same definition as the template language itself.

Fixes #12033.

Change-Id: Icdfd6039722d7d3f984ba0905105eb3253e14831
Reviewed-on: https://go-review.googlesource.com/14593
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/html/template/template.go
src/text/template/exec.go
src/text/template/funcs.go

index bb9140a4daf0855433ad0565cf109b70eeb0ac00..f9e6e43588e0d0324b011537d8578d5fb2fbdc73 100644 (file)
@@ -9,6 +9,7 @@ import (
        "io"
        "io/ioutil"
        "path/filepath"
+       "reflect"
        "sync"
        "text/template"
        "text/template/parse"
@@ -413,3 +414,10 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
        }
        return parseFiles(t, filenames...)
 }
+
+// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
+// and whether the value has a meaningful truth value. This is the definition of
+// truth used by if and other such actions.
+func IsTrue(val reflect.Value) (truth, ok bool) {
+       return template.IsTrue(val)
+}
index 625e9b54d69f6d2441ca2e770a8dcd6293babba1..8d7425507012db1613abe0f277e3bb12cf5b66fe 100644 (file)
@@ -242,7 +242,7 @@ func (s *state) walk(dot reflect.Value, node parse.Node) {
 func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse.PipeNode, list, elseList *parse.ListNode) {
        defer s.pop(s.mark())
        val := s.evalPipeline(dot, pipe)
-       truth, ok := isTrue(val)
+       truth, ok := IsTrue(val)
        if !ok {
                s.errorf("if/with can't use %v", val)
        }
@@ -257,9 +257,10 @@ func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse.
        }
 }
 
-// isTrue reports whether the value is 'true', in the sense of not the zero of its type,
-// and whether the value has a meaningful truth value.
-func isTrue(val reflect.Value) (truth, ok bool) {
+// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
+// and whether the value has a meaningful truth value. This is the definition of
+// truth used by if and other such actions.
+func IsTrue(val reflect.Value) (truth, ok bool) {
        if !val.IsValid() {
                // Something like var x interface{}, never set. It's a form of nil.
                return false, true
index b514551455f186297b02d36dbbda057ea8574348..6eda73183481f14d2681a87ec4bc183249a76f23 100644 (file)
@@ -265,7 +265,7 @@ func call(fn interface{}, args ...interface{}) (interface{}, error) {
 // Boolean logic.
 
 func truth(a interface{}) bool {
-       t, _ := isTrue(reflect.ValueOf(a))
+       t, _ := IsTrue(reflect.ValueOf(a))
        return t
 }
 
@@ -300,9 +300,8 @@ func or(arg0 interface{}, args ...interface{}) interface{} {
 }
 
 // not returns the Boolean negation of its argument.
-func not(arg interface{}) (truth bool) {
-       truth, _ = isTrue(reflect.ValueOf(arg))
-       return !truth
+func not(arg interface{}) bool {
+       return !truth(arg)
 }
 
 // Comparison.