diff --git a/common.go b/common.go index 870839f..3d7fa7d 100644 --- a/common.go +++ b/common.go @@ -168,6 +168,7 @@ var ( ampersandBytes = []byte("&") colonSpaceBytes = []byte(": ") spaceBytes = []byte(" ") + dotBytes = []byte(".") openParenBytes = []byte("(") closeParenBytes = []byte(")") nilBytes = []byte("nil") diff --git a/config.go b/config.go index 2d9bfa1..506ff8c 100644 --- a/config.go +++ b/config.go @@ -69,6 +69,9 @@ type ConfigState struct { // OmitZero specifies that zero values should not be printed in a dump. OmitZero bool + // LocalPackage specifies a package selector to trim from type information. + LocalPackage string + // ElideType specifies that type information defined by context should // not be printed in a dump. ElideType bool diff --git a/dump.go b/dump.go index e1174f6..7427fa1 100644 --- a/dump.go +++ b/dump.go @@ -146,16 +146,17 @@ func (d *dumpState) dumpPtr(v reflect.Value) { var typeBytes []byte if displayed { d.w.Write(openParenBytes) - typeBytes = []byte(orig.Type().String()) + typeBytes = []byte(strings.TrimPrefix(orig.Type().String(), d.cs.LocalPackage)) } else { d.w.Write(bytes.Repeat(ampersandBytes, indirects)) - typeBytes = []byte(v.Type().String()) + typeBytes = []byte(strings.TrimPrefix(v.Type().String(), d.cs.LocalPackage)) } kind := v.Kind() bufferedChan := kind == reflect.Chan && v.Cap() != 0 if kind == reflect.Ptr || bufferedChan { d.w.Write(openParenBytes) } + typeBytes = bytes.TrimPrefix(typeBytes, dotBytes) d.w.Write(bytes.ReplaceAll(typeBytes, interfaceTypeBytes, interfaceBytes)) if displayed { d.w.Write(closeParenBytes) @@ -371,7 +372,8 @@ func (d *dumpState) dump(v reflect.Value, wasPtr, static bool, addr uintptr) { if bufferedChan { d.w.Write(openParenBytes) } - typeBytes := []byte(v.Type().String()) + typeBytes := []byte(strings.TrimPrefix(v.Type().String(), d.cs.LocalPackage)) + typeBytes = bytes.TrimPrefix(typeBytes, dotBytes) d.w.Write(bytes.ReplaceAll(typeBytes, interfaceTypeBytes, interfaceBytes)) if bufferedChan { switch len := v.Len(); len { diff --git a/spew_test.go b/spew_test.go index 13287d7..b710fce 100644 --- a/spew_test.go +++ b/spew_test.go @@ -102,6 +102,10 @@ func initSpewTests() { ignUnexDefault := utter.NewDefaultConfig() ignUnexDefault.IgnoreUnexported = true + // Remove local package prefix. + elideLocalDefault := utter.NewDefaultConfig() + elideLocalDefault.LocalPackage = "utter_test" + // Elide implicit types. elideTypeDefault := utter.NewDefaultConfig() elideTypeDefault.ElideType = true @@ -187,6 +191,9 @@ func initSpewTests() { {ignUnexDefault, fCSFdump, Foo{Bar{flag: 1}, map[interface{}]interface{}{"one": true}}, "utter_test.Foo{\n ExportedField: map[interface{}]interface{}{\n string(\"one\"): bool(true),\n },\n}\n", }, + {elideLocalDefault, fCSFdump, Foo{Bar{flag: 1}, map[interface{}]interface{}{"one": true}}, + "Foo{\n unexportedField: Bar{\n flag: Flag(1),\n data: uintptr(0),\n },\n ExportedField: map[interface{}]interface{}{\n string(\"one\"): bool(true),\n },\n}\n", + }, {elideTypeDefault, fCSFdump, float64(1), "1.0\n"}, {elideTypeDefault, fCSFdump, float32(1), "float32(1)\n"}, {elideTypeDefault, fCSFdump, int(1), "1\n"},