Commit 090843b6 authored by David Symonds's avatar David Symonds

text/template: change IsTrue to take interface{} instead of reflect.Value.

This is a follow-up to a326c3e1 to avoid reflect being in the API.

Fixes #12801.

Change-Id: Ic4c2e592e2c35b5911f75d88f1d9c44787c80f30
Reviewed-on: https://go-review.googlesource.com/15240
Run-TryBot: David Symonds <dsymonds@golang.org>
Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
parent 79480ca0
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"reflect"
"sync" "sync"
"text/template" "text/template"
"text/template/parse" "text/template/parse"
...@@ -420,6 +419,6 @@ func parseGlob(t *Template, pattern string) (*Template, error) { ...@@ -420,6 +419,6 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
// 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. This is the definition of // and whether the value has a meaningful truth value. This is the definition of
// truth used by if and other such actions. // truth used by if and other such actions.
func IsTrue(val reflect.Value) (truth, ok bool) { func IsTrue(val interface{}) (truth, ok bool) {
return template.IsTrue(val) 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)
} }
...@@ -260,7 +260,11 @@ func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse. ...@@ -260,7 +260,11 @@ 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. This is the definition of // and whether the value has a meaningful truth value. This is the definition of
// truth used by if and other such actions. // truth used by if and other such actions.
func IsTrue(val reflect.Value) (truth, ok bool) { func IsTrue(val interface{}) (truth, ok bool) {
return isTrue(reflect.ValueOf(val))
}
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(a)
return t return t
} }
......
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