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:
parent
f1fd14314f
commit
893f686338
15
spew/dump.go
15
spew/dump.go
|
@ -415,11 +415,24 @@ func (d *dumpState) dump(v reflect.Value) {
|
||||||
for i := 0; i < numFields; i++ {
|
for i := 0; i < numFields; i++ {
|
||||||
d.indent()
|
d.indent()
|
||||||
vtf := vt.Field(i)
|
vtf := vt.Field(i)
|
||||||
|
tag := vtf.Tag.Get("spew")
|
||||||
|
if tag == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
d.w.Write([]byte(vtf.Name))
|
d.w.Write([]byte(vtf.Name))
|
||||||
d.w.Write(colonSpaceBytes)
|
d.w.Write(colonSpaceBytes)
|
||||||
d.ignoreNextIndent = true
|
d.ignoreNextIndent = true
|
||||||
d.dump(d.unpackValue(v.Field(i)))
|
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)
|
d.w.Write(commaNewlineBytes)
|
||||||
} else {
|
} else {
|
||||||
d.w.Write(newlineBytes)
|
d.w.Write(newlineBytes)
|
||||||
|
|
|
@ -154,6 +154,13 @@ func initSpewTests() {
|
||||||
dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"},
|
dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"},
|
||||||
map[string]int{"one": 1}}
|
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.
|
// Variable for tests on types which implement error interface.
|
||||||
te := customError(10)
|
te := customError(10)
|
||||||
|
|
||||||
|
@ -179,6 +186,8 @@ func initSpewTests() {
|
||||||
{scsDefault, fSprint, "", complex(-1, -2), "(-1-2i)"},
|
{scsDefault, fSprint, "", complex(-1, -2), "(-1-2i)"},
|
||||||
{scsDefault, fSprintf, "%v", complex(float32(-3), -4), "(-3-4i)"},
|
{scsDefault, fSprintf, "%v", complex(float32(-3), -4), "(-3-4i)"},
|
||||||
{scsDefault, fSprintln, "", complex(float64(-5), -6), "(-5-6i)\n"},
|
{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, "", &ts, "<*>test"},
|
{scsNoMethods, fCSFprint, "", &ts, "<*>test"},
|
||||||
{scsNoMethods, fCSFprint, "", tps, "test"},
|
{scsNoMethods, fCSFprint, "", tps, "test"},
|
||||||
|
|
Loading…
Reference in New Issue