Add options to disable printing of type and length

This commit is contained in:
Kyle Ibrahim 2016-11-03 23:54:14 -07:00
parent 346938d642
commit 49288ec620
3 changed files with 40 additions and 22 deletions

View File

@ -76,6 +76,13 @@ type ConfigState struct {
// data structures in tests. // data structures in tests.
DisableCapacities bool DisableCapacities bool
// DisableTypes specifies whether to disable the printing of types.
DisableTypes bool
// DisableLengths specifies whether to disable the printing of lengths.
// This also disables the printing of capacities.
DisableLengths bool
// ContinueOnMethod specifies whether or not recursion should continue once // ContinueOnMethod specifies whether or not recursion should continue once
// a custom error or Stringer interface is invoked. The default, false, // a custom error or Stringer interface is invoked. The default, false,
// means it will print the results of invoking the custom error or Stringer // means it will print the results of invoking the custom error or Stringer

View File

@ -100,6 +100,13 @@ The following configuration options are available:
capacities for arrays, slices, maps and channels. This is useful when capacities for arrays, slices, maps and channels. This is useful when
diffing data structures in tests. diffing data structures in tests.
* DisableTypes
DisableTypes specifies whether to disable the printing of types.
* DisableLengths
DisableLengths specifies whether to disable the printing of lengths.
This also disables the printing of capacities.
* ContinueOnMethod * ContinueOnMethod
Enables recursion into types after invoking error and Stringer interface Enables recursion into types after invoking error and Stringer interface
methods. Recursion after method invocation is disabled by default. methods. Recursion after method invocation is disabled by default.

View File

@ -266,15 +266,18 @@ func (d *dumpState) dump(v reflect.Value) {
// Print type information unless already handled elsewhere. // Print type information unless already handled elsewhere.
if !d.ignoreNextType { if !d.ignoreNextType {
d.indent() d.indent()
if !d.cs.DisableTypes {
d.w.Write(openParenBytes) d.w.Write(openParenBytes)
d.w.Write([]byte(v.Type().String())) d.w.Write([]byte(v.Type().String()))
d.w.Write(closeParenBytes) d.w.Write(closeParenBytes)
d.w.Write(spaceBytes) d.w.Write(spaceBytes)
} }
}
d.ignoreNextType = false d.ignoreNextType = false
// Display length and capacity if the built-in len and cap functions // Display length and capacity if the built-in len and cap functions
// work with the value's kind and the len/cap itself is non-zero. // work with the value's kind and the len/cap itself is non-zero.
if !d.cs.DisableLengths {
valueLen, valueCap := 0, 0 valueLen, valueCap := 0, 0
switch v.Kind() { switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Chan: case reflect.Array, reflect.Slice, reflect.Chan:
@ -298,6 +301,7 @@ func (d *dumpState) dump(v reflect.Value) {
d.w.Write(closeParenBytes) d.w.Write(closeParenBytes)
d.w.Write(spaceBytes) d.w.Write(spaceBytes)
} }
}
// Call Stringer/error interfaces if they exist and the handle methods flag // Call Stringer/error interfaces if they exist and the handle methods flag
// is enabled // is enabled