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: add DisableUnexported #122

Open
wants to merge 2 commits 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 @@ -76,6 +76,10 @@ type ConfigState struct {
// data structures in tests.
DisableCapacities bool

// DisableUnexported specifies whether to disable the unexported fields of
// struct. This is useful for debugging APIs.
DisableUnexported bool

// ContinueOnMethod specifies whether or not recursion should continue once
// a custom error or Stringer interface is invoked. The default, false,
// means it will print the results of invoking the custom error or Stringer
Expand Down
6 changes: 5 additions & 1 deletion spew/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode"
)

var (
Expand Down Expand Up @@ -413,8 +414,11 @@ func (d *dumpState) dump(v reflect.Value) {
vt := v.Type()
numFields := v.NumField()
for i := 0; i < numFields; i++ {
d.indent()
vtf := vt.Field(i)
if d.cs.DisableUnexported && !unicode.IsUpper([]rune(vtf.Name)[0]) {
continue
}
d.indent()
d.w.Write([]byte(vtf.Name))
d.w.Write(colonSpaceBytes)
d.ignoreNextIndent = true
Expand Down
59 changes: 59 additions & 0 deletions spew/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ base test element are also tested to ensure proper indirection across all types.
- Struct that contains custom type with Stringer pointer interface via both
exported and unexported fields
- Struct that contains embedded struct and field to same struct
- Struct that contains unexported field and exported field
- Uintptr to 0 (null pointer)
- Uintptr address of real variable
- Unsafe.Pointer to 0 (null pointer)
Expand Down Expand Up @@ -1040,3 +1041,61 @@ func TestDumpSortedKeys(t *testing.T) {
}

}

func TestDumpUnexportedFields(t *testing.T) {
dumpTests = make([]dumpTest, 0)

// Struct with both exported and unexported field.
type s struct {
ExportedVarA int8
ExportedVarB int8
unExportedVarC int8
unExportedVarD int8
}
v := s{10, 20, 30, 40}
nv := (*s)(nil)
pv := &v
vAddr := fmt.Sprintf("%p", pv)
pvAddr := fmt.Sprintf("%p", &pv)
vt := "spew_test.s"
vt2 := "int8"
vs := "{\n ExportedVarA: (" + vt2 + ") 10,\n ExportedVarB: (" + vt2 + ") 20,\n}"

addDumpTest(v, "("+vt+") "+vs+"\n")
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
addDumpTest(nv, "(*"+vt+")(<nil>)\n")

type s2 struct {
unExportedVarA int8
unExportedVarB int8
ExportedVarC int8
ExportedVarD int8
}
v2 := s2{10, 20, 30, 40}
nv2 := (*s2)(nil)
pv2 := &v2
v2Addr := fmt.Sprintf("%p", pv2)
pv2Addr := fmt.Sprintf("%p", &pv2)
v2t := "spew_test.s2"
v2t2 := "int8"
v2s := "{\n ExportedVarC: (" + v2t2 + ") 30,\n ExportedVarD: (" + v2t2 + ") 40\n}"

addDumpTest(v2, "("+v2t+") "+v2s+"\n")
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
addDumpTest(nv2, "(*"+v2t+")(<nil>)\n")

spew.Config.DisableUnexported = true

t.Logf("Running %d tests", len(dumpTests))
for i, test := range dumpTests {
buf := new(bytes.Buffer)
spew.Fdump(buf, test.in)
s := buf.String()
if testFailed(s, test.wants) {
t.Errorf("Dump #%d\n got: %s %s", i, s, stringizeWants(test.wants))
continue
}
}
}