]> Cypherpunks repositories - gostls13.git/commitdiff
template: format error with pointer receiver.
authorDavid Symonds <dsymonds@golang.org>
Fri, 4 Nov 2011 12:45:38 +0000 (23:45 +1100)
committerDavid Symonds <dsymonds@golang.org>
Fri, 4 Nov 2011 12:45:38 +0000 (23:45 +1100)
This is a continuation of 982d70c6d5d6.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5348042

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

index 540fb72c8e8175fda38380db5f3f7bd612d719ab..8ebd52bf3f84b67fa0247efd2cd23d5edaeac37c 100644 (file)
@@ -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() {
index 2199e440bcc0a1c235754b110b5aef807702c203..57216676410907aac6356e76387760d5063ee6b4 100644 (file)
@@ -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},