Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to suppress fields from display #120

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -31,15 +23,15 @@ 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

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
Expand Down
11 changes: 10 additions & 1 deletion spew/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spew/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down