refect: Type.String() should use a shortened package name

This commit is contained in:
Damian Gryski
2023-03-07 12:21:17 -08:00
committed by Ron Evans
parent 7a96f0f609
commit 569817a514
2 changed files with 13 additions and 1 deletions
+7 -1
View File
@@ -502,7 +502,13 @@ func pointerTo(t *rawType) *rawType {
func (t *rawType) String() string {
if t.isNamed() {
return t.PkgPath() + "." + t.Name()
path := t.PkgPath()
slash := len(path) - 1
for slash >= 0 && path[slash] != '/' {
slash--
}
shortened := path[slash+1:]
return shortened + "." + t.Name()
}
switch t.Kind() {
+6
View File
@@ -1,6 +1,7 @@
package reflect_test
import (
"encoding/base64"
. "reflect"
"sort"
"testing"
@@ -264,6 +265,11 @@ func TestNamedTypes(t *testing.T) {
if got, want := ValueOf(m).String(), "<map[[4]uint16]string Value>"; got != want {
t.Errorf("Value.String()=%v, want %v", got, want)
}
if got, want := TypeOf(base64.Encoding{}).String(), "base64.Encoding"; got != want {
t.Errorf("Type.String(base64.Encoding{})=%v, want %v", got, want)
}
}
func TestStruct(t *testing.T) {