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.
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
// a custom error or Stringer interface is invoked. The default, false,
// 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
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
Enables recursion into types after invoking error and Stringer interface
methods. Recursion after method invocation is disabled by default.

View File

@ -266,37 +266,41 @@ func (d *dumpState) dump(v reflect.Value) {
// Print type information unless already handled elsewhere.
if !d.ignoreNextType {
d.indent()
d.w.Write(openParenBytes)
d.w.Write([]byte(v.Type().String()))
d.w.Write(closeParenBytes)
d.w.Write(spaceBytes)
if !d.cs.DisableTypes {
d.w.Write(openParenBytes)
d.w.Write([]byte(v.Type().String()))
d.w.Write(closeParenBytes)
d.w.Write(spaceBytes)
}
}
d.ignoreNextType = false
// 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.
valueLen, valueCap := 0, 0
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Chan:
valueLen, valueCap = v.Len(), v.Cap()
case reflect.Map, reflect.String:
valueLen = v.Len()
}
if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 {
d.w.Write(openParenBytes)
if valueLen != 0 {
d.w.Write(lenEqualsBytes)
printInt(d.w, int64(valueLen), 10)
if !d.cs.DisableLengths {
valueLen, valueCap := 0, 0
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Chan:
valueLen, valueCap = v.Len(), v.Cap()
case reflect.Map, reflect.String:
valueLen = v.Len()
}
if !d.cs.DisableCapacities && valueCap != 0 {
if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 {
d.w.Write(openParenBytes)
if valueLen != 0 {
d.w.Write(spaceBytes)
d.w.Write(lenEqualsBytes)
printInt(d.w, int64(valueLen), 10)
}
d.w.Write(capEqualsBytes)
printInt(d.w, int64(valueCap), 10)
if !d.cs.DisableCapacities && valueCap != 0 {
if valueLen != 0 {
d.w.Write(spaceBytes)
}
d.w.Write(capEqualsBytes)
printInt(d.w, int64(valueCap), 10)
}
d.w.Write(closeParenBytes)
d.w.Write(spaceBytes)
}
d.w.Write(closeParenBytes)
d.w.Write(spaceBytes)
}
// Call Stringer/error interfaces if they exist and the handle methods flag