mirror of
https://github.com/golang/go
synced 2024-11-24 15:00:14 -07:00
template: format errors
R=golang-dev, r CC=golang-dev https://golang.org/cl/5340043
This commit is contained in:
parent
cb2040b2ab
commit
e73680aec0
@ -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 {
|
||||
|
@ -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},
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user