From e73680aec0f6d6de3a8cd0f48c01c854703a5b72 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 4 Nov 2011 07:33:55 -0400 Subject: [PATCH] template: format errors R=golang-dev, r CC=golang-dev https://golang.org/cl/5340043 --- src/pkg/text/template/exec.go | 4 ++-- src/pkg/text/template/exec_test.go | 4 ++++ src/pkg/text/template/funcs.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index 228477ce797..540fb72c8e8 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 e32de4d40ff..2199e440bcc 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 26c3a6e8488..1eff7165faf 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