From 569817a514a9577d39ffdf2c118778c885a93e26 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Tue, 7 Mar 2023 12:21:17 -0800 Subject: [PATCH] refect: Type.String() should use a shortened package name --- src/reflect/type.go | 8 +++++++- src/reflect/value_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/reflect/type.go b/src/reflect/type.go index 54a5e349..db09fb43 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -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() { diff --git a/src/reflect/value_test.go b/src/reflect/value_test.go index 05b0139b..1231a790 100644 --- a/src/reflect/value_test.go +++ b/src/reflect/value_test.go @@ -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(), ""; 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) {