Adds config option DisableNilValues, which specifies whether or not to include
nil fields. This is useful in tests that rely on "golden" test data: Often one adds a new, optional field to a struct, and it's onerous to update all test data to reflect this field, which would now be nil everywhere.
This commit is contained in:
parent
74bd53c438
commit
51a089712b
|
@ -67,6 +67,12 @@ type ConfigState struct {
|
||||||
// Google App Engine or with the "safe" build tag specified.
|
// Google App Engine or with the "safe" build tag specified.
|
||||||
DisablePointerMethods bool
|
DisablePointerMethods bool
|
||||||
|
|
||||||
|
// DisableNilValues specifies whether or not to include nil fields. This is
|
||||||
|
// useful in tests that rely on "golden" test data: Often one adds a new,
|
||||||
|
// optional field to a struct, and it's onerous to update all test data
|
||||||
|
// to reflect this field, which would now be nil everywhere.
|
||||||
|
DisableNilValues bool
|
||||||
|
|
||||||
// DisablePointerAddresses specifies whether to disable the printing of
|
// DisablePointerAddresses specifies whether to disable the printing of
|
||||||
// pointer addresses. This is useful when diffing data structures in tests.
|
// pointer addresses. This is useful when diffing data structures in tests.
|
||||||
DisablePointerAddresses bool
|
DisablePointerAddresses bool
|
||||||
|
|
82
spew/dump.go
82
spew/dump.go
|
@ -234,10 +234,16 @@ func (d *dumpState) dumpSlice(v reflect.Value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively call dump for each item.
|
// Recursively call dump for each item.
|
||||||
|
l := list{dumpState: d}
|
||||||
for i := 0; i < numEntries; i++ {
|
for i := 0; i < numEntries; i++ {
|
||||||
d.dump(d.unpackValue(v.Index(i)))
|
entryVal := d.unpackValue(v.Index(i))
|
||||||
d.writeComma(i < (numEntries - 1))
|
if !d.filterValue(entryVal) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
l.beginEntry()
|
||||||
|
d.dump(entryVal)
|
||||||
|
}
|
||||||
|
l.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump is the main workhorse for dumping a value. It uses the passed reflect
|
// dump is the main workhorse for dumping a value. It uses the passed reflect
|
||||||
|
@ -378,18 +384,23 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
d.indent()
|
d.indent()
|
||||||
d.w.Write(maxNewlineBytes)
|
d.w.Write(maxNewlineBytes)
|
||||||
} else {
|
} else {
|
||||||
numEntries := v.Len()
|
|
||||||
keys := v.MapKeys()
|
keys := v.MapKeys()
|
||||||
if d.cs.SortKeys {
|
if d.cs.SortKeys {
|
||||||
sortValues(keys, d.cs)
|
sortValues(keys, d.cs)
|
||||||
}
|
}
|
||||||
for i, key := range keys {
|
l := list{dumpState: d}
|
||||||
|
for _, key := range keys {
|
||||||
|
mapValue := d.unpackValue(v.MapIndex(key))
|
||||||
|
if !d.filterValue(mapValue) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
l.beginEntry()
|
||||||
d.dump(d.unpackValue(key))
|
d.dump(d.unpackValue(key))
|
||||||
d.w.Write(colonSpaceBytes)
|
d.w.Write(colonSpaceBytes)
|
||||||
d.ignoreNextIndent = true
|
d.ignoreNextIndent = true
|
||||||
d.dump(d.unpackValue(v.MapIndex(key)))
|
d.dump(mapValue)
|
||||||
d.writeComma(i < (numEntries - 1))
|
|
||||||
}
|
}
|
||||||
|
l.end()
|
||||||
}
|
}
|
||||||
d.depth--
|
d.depth--
|
||||||
d.indent()
|
d.indent()
|
||||||
|
@ -404,15 +415,20 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
} else {
|
} else {
|
||||||
vt := v.Type()
|
vt := v.Type()
|
||||||
numFields := v.NumField()
|
numFields := v.NumField()
|
||||||
|
l := list{dumpState: d, indented: true}
|
||||||
for i := 0; i < numFields; i++ {
|
for i := 0; i < numFields; i++ {
|
||||||
d.indent()
|
fieldValue := d.unpackValue(v.Field(i))
|
||||||
|
if !d.filterValue(fieldValue) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
l.beginEntry()
|
||||||
vtf := vt.Field(i)
|
vtf := vt.Field(i)
|
||||||
d.w.Write([]byte(vtf.Name))
|
d.w.Write([]byte(vtf.Name))
|
||||||
d.w.Write(colonSpaceBytes)
|
d.w.Write(colonSpaceBytes)
|
||||||
d.ignoreNextIndent = true
|
d.ignoreNextIndent = true
|
||||||
d.dump(d.unpackValue(v.Field(i)))
|
d.dump(fieldValue)
|
||||||
d.writeComma(i < (numFields - 1))
|
|
||||||
}
|
}
|
||||||
|
l.end()
|
||||||
}
|
}
|
||||||
d.depth--
|
d.depth--
|
||||||
d.indent()
|
d.indent()
|
||||||
|
@ -436,9 +452,9 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeComma emits a comma if hasMoreElements is true, or if trailing commas
|
// writeDelimiter emits a comma if hasMoreElements is true, or if trailing
|
||||||
// are always enabled.
|
// commas are always enabled.
|
||||||
func (d *dumpState) writeComma(hasMoreElements bool) {
|
func (d *dumpState) writeDelimiter(hasMoreElements bool) {
|
||||||
if hasMoreElements || d.cs.AlwaysIncludeTrailingComma {
|
if hasMoreElements || d.cs.AlwaysIncludeTrailingComma {
|
||||||
d.w.Write(commaNewlineBytes)
|
d.w.Write(commaNewlineBytes)
|
||||||
} else {
|
} else {
|
||||||
|
@ -446,15 +462,57 @@ func (d *dumpState) writeComma(hasMoreElements bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filterValue returns true if a value should be dumped.
|
||||||
|
func (d *dumpState) filterValue(value reflect.Value) bool {
|
||||||
|
if !d.cs.DisableNilValues {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return !isNil(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// list is a small helper to write lists and ensure that each item is
|
||||||
|
// correctly delimited, and that nothing is emitted if no entires were added.
|
||||||
|
type list struct {
|
||||||
|
count int
|
||||||
|
indented bool
|
||||||
|
*dumpState
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *list) beginEntry() {
|
||||||
|
if l.count > 0 {
|
||||||
|
l.writeDelimiter(true)
|
||||||
|
}
|
||||||
|
l.count++
|
||||||
|
if l.indented {
|
||||||
|
l.indent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *list) end() {
|
||||||
|
if l.count > 0 {
|
||||||
|
l.writeDelimiter(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isNil(v reflect.Value) bool {
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Interface, reflect.Map, reflect.Slice, reflect.Ptr:
|
||||||
|
return v.IsNil()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// fdump is a helper function to consolidate the logic from the various public
|
// fdump is a helper function to consolidate the logic from the various public
|
||||||
// methods which take varying writers and config states.
|
// methods which take varying writers and config states.
|
||||||
func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
|
func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
|
||||||
for _, arg := range a {
|
for _, arg := range a {
|
||||||
if arg == nil {
|
if arg == nil {
|
||||||
|
if !cs.DisableNilValues {
|
||||||
w.Write(interfaceBytes)
|
w.Write(interfaceBytes)
|
||||||
w.Write(spaceBytes)
|
w.Write(spaceBytes)
|
||||||
w.Write(nilAngleBytes)
|
w.Write(nilAngleBytes)
|
||||||
w.Write(newlineBytes)
|
w.Write(newlineBytes)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -133,6 +133,7 @@ func initSpewTests() {
|
||||||
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
|
scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true}
|
||||||
scsNoCap := &spew.ConfigState{DisableCapacities: true}
|
scsNoCap := &spew.ConfigState{DisableCapacities: true}
|
||||||
scsTrailingComma := &spew.ConfigState{Indent: " ", AlwaysIncludeTrailingComma: true}
|
scsTrailingComma := &spew.ConfigState{Indent: " ", AlwaysIncludeTrailingComma: true}
|
||||||
|
scsNoNils := &spew.ConfigState{Indent: " ", DisableNilValues: true}
|
||||||
|
|
||||||
// Variables for tests on types which implement Stringer interface with and
|
// Variables for tests on types which implement Stringer interface with and
|
||||||
// without a pointer receiver.
|
// without a pointer receiver.
|
||||||
|
@ -144,6 +145,17 @@ func initSpewTests() {
|
||||||
}
|
}
|
||||||
tptr := &ptrTester{s: &struct{}{}}
|
tptr := &ptrTester{s: &struct{}{}}
|
||||||
|
|
||||||
|
type testIntf interface {
|
||||||
|
TestyTesty()
|
||||||
|
}
|
||||||
|
|
||||||
|
type nilTester struct {
|
||||||
|
s *struct{}
|
||||||
|
slice []interface{}
|
||||||
|
m map[string]interface{}
|
||||||
|
intf testIntf
|
||||||
|
}
|
||||||
|
|
||||||
// depthTester is used to test max depth handling for structs, array, slices
|
// depthTester is used to test max depth handling for structs, array, slices
|
||||||
// and maps.
|
// and maps.
|
||||||
type depthTester struct {
|
type depthTester struct {
|
||||||
|
@ -226,6 +238,15 @@ func initSpewTests() {
|
||||||
" (string) (len=3) \"one\": (int) 1,\n" +
|
" (string) (len=3) \"one\": (int) 1,\n" +
|
||||||
" },\n" +
|
" },\n" +
|
||||||
"}\n"},
|
"}\n"},
|
||||||
|
{scsNoNils, fCSSdump, "", nilTester{}, "(spew_test.nilTester) {\n}\n"},
|
||||||
|
{scsNoNils, fCSSdump, "", nilTester{
|
||||||
|
slice: []interface{}{nil, "foo"},
|
||||||
|
},
|
||||||
|
"(spew_test.nilTester) {\n" +
|
||||||
|
" slice: ([]interface {}) (len=2 cap=2) {\n" +
|
||||||
|
" (string) (len=3) \"foo\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}\n"},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue