Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (DisableFunctionTypePointerAddresses) hiding pointers for func fields #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spew/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ type ConfigState struct {
// be spewed to strings and sorted by those strings. This is only
// considered if SortKeys is true.
SpewKeys bool

// DisableFunctionTypePointerAddresses specifies whether to disable the printing of
// function types pointer addresses. This is useful when diffing data structures in tests.
DisableFunctionTypePointerAddresses bool
}

// Config is the active configuration of the top-level functions.
Expand Down
9 changes: 8 additions & 1 deletion spew/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,14 @@ func (d *dumpState) dump(v reflect.Value) {
case reflect.Uintptr:
printHexPtr(d.w, uintptr(v.Uint()))

case reflect.UnsafePointer, reflect.Chan, reflect.Func:
case reflect.Func:
if d.cs.DisableFunctionTypePointerAddresses {
fmt.Fprintf(d.w, "%v", v.String())
} else {
printHexPtr(d.w, v.Pointer())
}

case reflect.UnsafePointer, reflect.Chan:
printHexPtr(d.w, v.Pointer())

// There were not any other types at the time this code was written, but
Expand Down
16 changes: 16 additions & 0 deletions spew/spew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func redirStdout(f func()) ([]byte, error) {
return ioutil.ReadFile(fileName)
}

func someFunc(value string) string {
return value
}

func initSpewTests() {
// Config states with various settings.
scsDefault := spew.NewDefaultConfig()
Expand All @@ -131,17 +135,26 @@ func initSpewTests() {
scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
scsNoFuncPtrAddr := &spew.ConfigState{DisablePointerAddresses: true, DisableFunctionTypePointerAddresses: true}
scsNoCap := &spew.ConfigState{DisableCapacities: true}

// Variables for tests on types which implement Stringer interface with and
// without a pointer receiver.
ts := stringer("test")
tps := pstringer("test")

type someFuncType func(string) string

type ptrTester struct {
s *struct{}
}

type ptrFuncTester struct {
funcField someFuncType
}

tptr := &ptrTester{s: &struct{}{}}
tptrFunc := &ptrFuncTester{funcField: someFunc}

// depthTester is used to test max depth handling for structs, array, slices
// and maps.
Expand Down Expand Up @@ -201,6 +214,9 @@ func initSpewTests() {
"(error: 10) 10\n"},
{scsNoPtrAddr, fCSFprint, "", tptr, "<*>{<*>{}}"},
{scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"},

{scsNoFuncPtrAddr, fCSSdump, "", tptrFunc, "(*spew_test.ptrFuncTester)({\nfuncField: (spew_test.someFuncType) <spew_test.someFuncType Value>\n})\n"},

{scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"},
{scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"},
}
Expand Down