Support preserving spew state (seen pointers) across operations and API calls

This commit is contained in:
James Craig Burley 2019-12-20 12:48:19 -05:00
parent d3f36f3056
commit 9422b0e545
3 changed files with 27 additions and 1 deletions

View File

@ -111,6 +111,17 @@ type ConfigState struct {
// provides some degree of stability across runs versus
// printing out raw pointers. This can consume much memory.
UseOrdinals bool
// Preserve state of a spew (dump, format) operation for use
// by the next such operation (including across multiple
// arguments to a single API call as well as across API
// calls). Currently useful only when NoDuplicates is true.
PreserveSpewState bool
// The state of the last spew (dump, format) operation, if
// PreserveSpewState was when that operation was started.
// Can be copied to a different ConfigState object.
SpewState SpewState
}
// Config is the active configuration of the top-level functions.

View File

@ -522,10 +522,19 @@ func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
d := dumpState{w: w, cs: cs}
d.pointers = make(map[uintptr]int)
if cs.NoDuplicates || cs.UseOrdinals {
d.allPointers = make(map[uintptr]uintptr)
if cs.SpewState.allPointers == nil {
d.allPointers = make(map[uintptr]uintptr)
} else {
d.allPointers = cs.SpewState.allPointers
}
}
d.dump(reflect.ValueOf(arg))
d.w.Write(newlineBytes)
if d.allPointers != nil { // cs.NoDuplicates || cs.UseOrdinals
if cs.PreserveSpewState {
cs.SpewState.allPointers = d.allPointers
}
}
}
}

View File

@ -21,6 +21,12 @@ import (
"io"
)
// Wraps the end state of a spew (dump or format) operation for use by
// the next operation, if so configured.
type SpewState struct {
allPointers map[uintptr]uintptr
}
// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
// passed with a default Formatter interface returned by NewFormatter. It
// returns the formatted string as a value that satisfies error. See