Add options to disable printing of type and length
This commit is contained in:
parent
346938d642
commit
49288ec620
|
@ -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
|
||||||
|
|
|
@ -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.
|
||||||
|
|
48
spew/dump.go
48
spew/dump.go
|
@ -266,37 +266,41 @@ 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()
|
||||||
d.w.Write(openParenBytes)
|
if !d.cs.DisableTypes {
|
||||||
d.w.Write([]byte(v.Type().String()))
|
d.w.Write(openParenBytes)
|
||||||
d.w.Write(closeParenBytes)
|
d.w.Write([]byte(v.Type().String()))
|
||||||
d.w.Write(spaceBytes)
|
d.w.Write(closeParenBytes)
|
||||||
|
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.
|
||||||
valueLen, valueCap := 0, 0
|
if !d.cs.DisableLengths {
|
||||||
switch v.Kind() {
|
valueLen, valueCap := 0, 0
|
||||||
case reflect.Array, reflect.Slice, reflect.Chan:
|
switch v.Kind() {
|
||||||
valueLen, valueCap = v.Len(), v.Cap()
|
case reflect.Array, reflect.Slice, reflect.Chan:
|
||||||
case reflect.Map, reflect.String:
|
valueLen, valueCap = v.Len(), v.Cap()
|
||||||
valueLen = v.Len()
|
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.DisableCapacities && valueCap != 0 {
|
if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 {
|
||||||
|
d.w.Write(openParenBytes)
|
||||||
if valueLen != 0 {
|
if valueLen != 0 {
|
||||||
d.w.Write(spaceBytes)
|
d.w.Write(lenEqualsBytes)
|
||||||
|
printInt(d.w, int64(valueLen), 10)
|
||||||
}
|
}
|
||||||
d.w.Write(capEqualsBytes)
|
if !d.cs.DisableCapacities && valueCap != 0 {
|
||||||
printInt(d.w, int64(valueCap), 10)
|
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
|
// Call Stringer/error interfaces if they exist and the handle methods flag
|
||||||
|
|
Loading…
Reference in New Issue