This commit is contained in:
Klaxxon 2020-02-18 09:06:18 -06:00 committed by GitHub
commit d6f6f9c080
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 12 deletions

View File

@ -5,16 +5,8 @@ go-spew
[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
[![Coverage Status](https://img.shields.io/coveralls/davecgh/go-spew.svg)](https://coveralls.io/r/davecgh/go-spew?branch=master)
Go-spew implements a deep pretty printer for Go data structures to aid in
debugging. A comprehensive suite of tests with 100% test coverage is provided
to ensure proper functionality. See `test_coverage.txt` for the gocov coverage
report. Go-spew is licensed under the liberal ISC license, so it may be used in
open source or commercial projects.
Simply added IgnoreFieldByName(string) and IgnoreFieldByType(string) to suppress dumps. IgnoreFieldByType will also add []name and *name so pointers to and arrays of the speciified name are also ignored.
If you're interested in reading about how this package came to life and some
of the challenges involved in providing a deep pretty printer, there is a blog
post about it
[here](https://web.archive.org/web/20160304013555/https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/).
## Documentation
@ -31,7 +23,7 @@ http://localhost:6060/pkg/github.com/davecgh/go-spew/spew
## Installation
```bash
$ go get -u github.com/davecgh/go-spew/spew
$ go get -u github.com/klaxxon/go-spew/spew
```
## Quick Start
@ -39,7 +31,7 @@ $ go get -u github.com/davecgh/go-spew/spew
Add this import line to the file you're working in:
```Go
import "github.com/davecgh/go-spew/spew"
import "github.com/klaxxon/go-spew/spew"
```
To dump a variable with full newlines, indentation, type, and pointer

View File

@ -98,11 +98,20 @@ 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
}
func (c *ConfigState) ResetIgnoreFields() {
c.ignoreFieldByName = make(map[string]bool)
c.ignoreFieldByType = make(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), SortKeys: true, DisablePointerAddresses: true}
// 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