aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/text/template
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-11-04 07:33:55 -0400
committerRuss Cox <rsc@golang.org>2011-11-04 07:33:55 -0400
commite73680aec0f6d6de3a8cd0f48c01c854703a5b72 (patch)
treef21e9bd63847c20ca25d7859b3eff7d81bdb830f /src/pkg/text/template
parentcb2040b2ab68ab7d2863e65920749ed1266f7acd (diff)
downloadgo-e73680aec0f6d6de3a8cd0f48c01c854703a5b72.tar.xz
template: format errors
R=golang-dev, r CC=golang-dev https://golang.org/cl/5340043
Diffstat (limited to 'src/pkg/text/template')
-rw-r--r--src/pkg/text/template/exec.go4
-rw-r--r--src/pkg/text/template/exec_test.go4
-rw-r--r--src/pkg/text/template/funcs.go2
3 files changed, 7 insertions, 3 deletions
diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go
index 228477ce79..540fb72c8e 100644
--- a/src/pkg/text/template/exec.go
+++ b/src/pkg/text/template/exec.go
@@ -445,7 +445,7 @@ func methodByName(receiver reflect.Value, name string) (reflect.Value, bool) {
}
var (
- osErrorType = reflect.TypeOf((*error)(nil)).Elem()
+ errorType = reflect.TypeOf((*error)(nil)).Elem()
fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
)
@@ -659,7 +659,7 @@ func (s *state) printValue(n parse.Node, v reflect.Value) {
return
}
- if !v.Type().Implements(fmtStringerType) {
+ if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) {
if v.CanAddr() && reflect.PtrTo(v.Type()).Implements(fmtStringerType) {
v = v.Addr()
} else {
diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go
index e32de4d40f..2199e440bc 100644
--- a/src/pkg/text/template/exec_test.go
+++ b/src/pkg/text/template/exec_test.go
@@ -6,6 +6,7 @@ package template
import (
"bytes"
+ "errors"
"flag"
"fmt"
"os"
@@ -52,6 +53,7 @@ type T struct {
NonEmptyInterface I
// Stringer.
Str fmt.Stringer
+ Err error
// Pointers
PI *int
PSI *[]int
@@ -99,6 +101,7 @@ var tVal = &T{
Empty4: &U{"UinEmpty"},
NonEmptyInterface: new(T),
Str: bytes.NewBuffer([]byte("foozle")),
+ Err: errors.New("erroozle"),
PI: newInt(23),
PSI: newIntSlice(21, 22, 23),
Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X
@@ -416,6 +419,7 @@ var execTests = []execTest{
{"bug4", "{{if .Empty0}}non-nil{{else}}nil{{end}}", "nil", tVal, true},
// Stringer.
{"bug5", "{{.Str}}", "foozle", tVal, true},
+ {"bug5a", "{{.Err}}", "erroozle", tVal, true},
// Args need to be indirected and dereferenced sometimes.
{"bug6a", "{{vfunc .V0 .V1}}", "vfunc", tVal, true},
{"bug6b", "{{vfunc .V0 .V0}}", "vfunc", tVal, true},
diff --git a/src/pkg/text/template/funcs.go b/src/pkg/text/template/funcs.go
index 26c3a6e848..1eff7165fa 100644
--- a/src/pkg/text/template/funcs.go
+++ b/src/pkg/text/template/funcs.go
@@ -72,7 +72,7 @@ func goodFunc(typ reflect.Type) bool {
switch {
case typ.NumOut() == 1:
return true
- case typ.NumOut() == 2 && typ.Out(1) == osErrorType:
+ case typ.NumOut() == 2 && typ.Out(1) == errorType:
return true
}
return false