]> Cypherpunks repositories - gostls13.git/commitdiff
template: format errors
authorRuss Cox <rsc@golang.org>
Fri, 4 Nov 2011 11:33:55 +0000 (07:33 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 4 Nov 2011 11:33:55 +0000 (07:33 -0400)
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5340043

src/pkg/text/template/exec.go
src/pkg/text/template/exec_test.go
src/pkg/text/template/funcs.go

index 228477ce7972d32f1beaebabdfb05c197bb82e25..540fb72c8e8175fda38380db5f3f7bd612d719ab 100644 (file)
@@ -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 {
index e32de4d40ffbbd933363de58c731320ecf33bbe7..2199e440bcc0a1c235754b110b5aef807702c203 100644 (file)
@@ -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},
index 26c3a6e8488622bfffdd2889b300dff8fc139ada..1eff7165faf3871fc820ba377af1123f417870af 100644 (file)
@@ -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