diff options
author | David Symonds <dsymonds@golang.org> | 2011-11-04 23:45:38 +1100 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2011-11-04 23:45:38 +1100 |
commit | 278c0e53ea0fbdd5d82096a2e90ea0f4b3cc3a5e (patch) | |
tree | 595e969c0078777f121a4976ede09e9722462b9e | |
parent | dee343fb44d580cab3653ccddc99f3c9b0c82b24 (diff) | |
download | go-278c0e53ea0fbdd5d82096a2e90ea0f4b3cc3a5e.tar.gz |
template: format error with pointer receiver.
This is a continuation of 982d70c6d5d6.
R=golang-dev, rsc
CC=golang-dev
http://codereview.appspot.com/5348042
-rw-r--r-- | src/pkg/text/template/exec.go | 2 | ||||
-rw-r--r-- | src/pkg/text/template/exec_test.go | 21 |
2 files changed, 22 insertions, 1 deletions
diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index 540fb72c8..8ebd52bf3 100644 --- a/src/pkg/text/template/exec.go +++ b/src/pkg/text/template/exec.go @@ -660,7 +660,7 @@ func (s *state) printValue(n parse.Node, v reflect.Value) { } if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) { - if v.CanAddr() && reflect.PtrTo(v.Type()).Implements(fmtStringerType) { + if v.CanAddr() && (reflect.PtrTo(v.Type()).Implements(errorType) || reflect.PtrTo(v.Type()).Implements(fmtStringerType)) { v = v.Addr() } else { switch v.Kind() { diff --git a/src/pkg/text/template/exec_test.go b/src/pkg/text/template/exec_test.go index 2199e440b..572166764 100644 --- a/src/pkg/text/template/exec_test.go +++ b/src/pkg/text/template/exec_test.go @@ -32,6 +32,9 @@ type T struct { // Struct with String method. V0 V V1, V2 *V + // Struct with Error method. + W0 W + W1, W2 *W // Slices SI []int SIEmpty []int @@ -77,6 +80,17 @@ func (v *V) String() string { return fmt.Sprintf("<%d>", v.j) } +type W struct { + k int +} + +func (w *W) Error() string { + if w == nil { + return "nilW" + } + return fmt.Sprintf("[%d]", w.k) +} + var tVal = &T{ True: true, I: 17, @@ -85,6 +99,8 @@ var tVal = &T{ U: &U{"v"}, V0: V{6666}, V1: &V{7777}, // leave V2 as nil + W0: W{888}, + W1: &W{999}, // leave W2 as nil SI: []int{3, 4, 5}, SB: []bool{true, false}, MSI: map[string]int{"one": 1, "two": 2, "three": 3}, @@ -251,6 +267,11 @@ var execTests = []execTest{ {"&V{7777}.String()", "-{{.V1}}-", "-<7777>-", tVal, true}, {"(*V)(nil).String()", "-{{.V2}}-", "-nilV-", tVal, true}, + // Type with Error method. + {"W{888}.Error()", "-{{.W0}}-", "-[888]-", tVal, true}, + {"&W{999}.Error()", "-{{.W1}}-", "-[999]-", tVal, true}, + {"(*W)(nil).Error()", "-{{.W2}}-", "-nilW-", tVal, true}, + // Pointers. {"*int", "{{.PI}}", "23", tVal, true}, {"*[]int", "{{.PSI}}", "[21 22 23]", tVal, true}, |