When iterating fields, check tag for `spew:"-"` and don't print if present

Specifically checks to see if any of the remaining fields will be printed so it can accurately add a comma when more than one field are printed out.
Also there's a test for it.
This commit is contained in:
Unknown 2019-01-16 20:18:18 -07:00
parent f1fd14314f
commit 893f686338
2 changed files with 23 additions and 1 deletions

View File

@ -415,11 +415,24 @@ func (d *dumpState) dump(v reflect.Value) {
for i := 0; i < numFields; i++ {
d.indent()
vtf := vt.Field(i)
tag := vtf.Tag.Get("spew")
if tag == "-" {
continue
}
d.w.Write([]byte(vtf.Name))
d.w.Write(colonSpaceBytes)
d.ignoreNextIndent = true
d.dump(d.unpackValue(v.Field(i)))
if i < (numFields - 1) {
hasAnotherFieldToWrite := false
for j := i + 1; j < numFields; j++ {
vtf = vt.Field(j)
tag = vtf.Tag.Get("spew")
if tag != "-" {
hasAnotherFieldToWrite = true
}
}
if i < (numFields-1) && hasAnotherFieldToWrite {
d.w.Write(commaNewlineBytes)
} else {
d.w.Write(newlineBytes)

View File

@ -154,6 +154,13 @@ func initSpewTests() {
dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"},
map[string]int{"one": 1}}
type ignoreTester struct {
visible bool
invisible bool `spew:"-"`
}
it := ignoreTester{true, false}
// Variable for tests on types which implement error interface.
te := customError(10)
@ -179,6 +186,8 @@ func initSpewTests() {
{scsDefault, fSprint, "", complex(-1, -2), "(-1-2i)"},
{scsDefault, fSprintf, "%v", complex(float32(-3), -4), "(-3-4i)"},
{scsDefault, fSprintln, "", complex(float64(-5), -6), "(-5-6i)\n"},
{scsDefault, fCSFdump, "", it, "(spew_test.ignoreTester) {\n" +
" visible: (bool) true\n }\n"},
{scsNoMethods, fCSFprint, "", ts, "test"},
{scsNoMethods, fCSFprint, "", &ts, "<*>test"},
{scsNoMethods, fCSFprint, "", tps, "test"},