Commit a326c3e1 authored by Rob Pike's avatar Rob Pike

text/template: export isTrue

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/14593Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
parent 1216e181
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"reflect"
"sync" "sync"
"text/template" "text/template"
"text/template/parse" "text/template/parse"
...@@ -413,3 +414,10 @@ func parseGlob(t *Template, pattern string) (*Template, error) { ...@@ -413,3 +414,10 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
} }
return parseFiles(t, filenames...) 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)
}
...@@ -242,7 +242,7 @@ func (s *state) walk(dot reflect.Value, node parse.Node) { ...@@ -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) { func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse.PipeNode, list, elseList *parse.ListNode) {
defer s.pop(s.mark()) defer s.pop(s.mark())
val := s.evalPipeline(dot, pipe) val := s.evalPipeline(dot, pipe)
truth, ok := isTrue(val) truth, ok := IsTrue(val)
if !ok { if !ok {
s.errorf("if/with can't use %v", val) 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. ...@@ -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, // 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. // and whether the value has a meaningful truth value. This is the definition of
func isTrue(val reflect.Value) (truth, ok bool) { // truth used by if and other such actions.
func IsTrue(val reflect.Value) (truth, ok bool) {
if !val.IsValid() { if !val.IsValid() {
// Something like var x interface{}, never set. It's a form of nil. // Something like var x interface{}, never set. It's a form of nil.
return false, true return false, true
......
...@@ -265,7 +265,7 @@ func call(fn interface{}, args ...interface{}) (interface{}, error) { ...@@ -265,7 +265,7 @@ func call(fn interface{}, args ...interface{}) (interface{}, error) {
// Boolean logic. // Boolean logic.
func truth(a interface{}) bool { func truth(a interface{}) bool {
t, _ := isTrue(reflect.ValueOf(a)) t, _ := IsTrue(reflect.ValueOf(a))
return t return t
} }
...@@ -300,9 +300,8 @@ func or(arg0 interface{}, args ...interface{}) interface{} { ...@@ -300,9 +300,8 @@ func or(arg0 interface{}, args ...interface{}) interface{} {
} }
// not returns the Boolean negation of its argument. // not returns the Boolean negation of its argument.
func not(arg interface{}) (truth bool) { func not(arg interface{}) bool {
truth, _ = isTrue(reflect.ValueOf(arg)) return !truth(arg)
return !truth
} }
// Comparison. // Comparison.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment