Skip to content

Commit

Permalink
utter: allow a local package selector to be trimmed
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jan 6, 2022
1 parent 6a6fba3 commit 97166f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ var (
ampersandBytes = []byte("&")
colonSpaceBytes = []byte(": ")
spaceBytes = []byte(" ")
dotBytes = []byte(".")
openParenBytes = []byte("(")
closeParenBytes = []byte(")")
nilBytes = []byte("nil")
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions spew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit 97166f6

Please sign in to comment.