Added ability to ignore fields by name or type.

This commit is contained in:
klaxxon 2020-01-19 11:35:11 -06:00
parent d8f796af33
commit 88d2329e9a
2 changed files with 27 additions and 1 deletions

View File

@ -98,11 +98,15 @@ type ConfigState struct {
// be spewed to strings and sorted by those strings. This is only
// considered if SortKeys is true.
SpewKeys bool
// Name of fields to ignore
ignoreFieldByName map[string]bool
ignoreFieldByType map[string]bool
}
// Config is the active configuration of the top-level functions.
// The configuration can be changed by modifying the contents of spew.Config.
var Config = ConfigState{Indent: " "}
var Config = ConfigState{Indent: " ", ignoreFieldByName: make(map[string]bool), ignoreFieldByType: make(map[string]bool)}
// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
// passed with a Formatter interface returned by c.NewFormatter. It returns

View File

@ -413,6 +413,16 @@ func (d *dumpState) dump(v reflect.Value) {
vt := v.Type()
numFields := v.NumField()
for i := 0; i < numFields; i++ {
// Ignore?
n := vt.Field(i).Name
if _, ok := Config.ignoreFieldByName[n]; ok {
continue
}
n = vt.Field(i).Type.String()
if _, ok := Config.ignoreFieldByType[n]; ok {
continue
}
d.indent()
vtf := vt.Field(i)
d.w.Write([]byte(vtf.Name))
@ -481,6 +491,18 @@ func Sdump(a ...interface{}) string {
return buf.String()
}
func IgnoreFieldByName(f string) {
Config.ignoreFieldByName[f] = true
}
func IgnoreFieldByType(f string) {
Config.ignoreFieldByType[f] = true
// And arrays of the type
Config.ignoreFieldByType["[]"+f] = true
// And pointers to the type
Config.ignoreFieldByType["*"+f] = true
}
/*
Dump displays the passed parameters to standard out with newlines, customizable
indentation, and additional debug information such as complete types and all