]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: always handle special methods if print operand is a reflect.Value
authorMartin Möhrmann <martisch@uos.de>
Sun, 9 Oct 2016 19:06:03 +0000 (21:06 +0200)
committerMartin Möhrmann <martisch@uos.de>
Tue, 18 Oct 2016 10:50:26 +0000 (10:50 +0000)
Check for and call the special printing and format methods such as String
at printing depth 0 when printing the concrete value of a reflect.Value.

Fixes: #16015
Change-Id: I23bd2927255b60924e5558321e98dd4a95e11c4c
Reviewed-on: https://go-review.googlesource.com/30753
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Martin Möhrmann <martisch@uos.de>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/fmt/fmt_test.go
src/fmt/print.go

index 8c1c02158e5761d5e73207b67836f93c154dc8a1..6f8c1550a09153e8f46749f5288541d7b2e55395 100644 (file)
@@ -605,7 +605,10 @@ var fmtTests = []struct {
        {"%x", I(23), `3c32333e`},
        {"%#x", I(23), `0x3c32333e`},
        {"%# x", I(23), `0x3c 0x32 0x33 0x3e`},
-       {"%d", I(23), `23`}, // Stringer applies only to string formats.
+       // Stringer applies only to string formats.
+       {"%d", I(23), `23`},
+       // Stringer applies to the extracted value.
+       {"%s", reflect.ValueOf(I(23)), `<23>`},
 
        // go syntax
        {"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`},
index f8c731656e5af40518d97ebbd2bd61e7d7cb6a40..75301a238e29c526f742f2f8e14aaf9ad1bb38e3 100644 (file)
@@ -659,6 +659,14 @@ func (p *pp) printArg(arg interface{}, verb rune) {
        case []byte:
                p.fmtBytes(f, verb, "[]byte")
        case reflect.Value:
+               // Handle extractable values with special methods
+               // since printValue does not handle them at depth 0.
+               if f.IsValid() && f.CanInterface() {
+                       p.arg = f.Interface()
+                       if p.handleMethods(verb) {
+                               return
+                       }
+               }
                p.printValue(f, verb, 0)
        default:
                // If the type is not simple, it might have methods.