From c9b086a12b588ae99cbe2be279a8950473604b96 Mon Sep 17 00:00:00 2001 From: aerth Date: Wed, 30 Dec 2020 02:58:10 +0000 Subject: [PATCH] use field tags to ignore a struct field in pretty output --- formatter.go | 16 ++++++++++++--- formatter_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/formatter.go b/formatter.go index a317d7b..db777a8 100644 --- a/formatter.go +++ b/formatter.go @@ -169,7 +169,18 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) { writeByte(p, '\n') pp = p.indent() } - for i := 0; i < v.NumField(); i++ { + + numfields := v.NumField() + writecomma := false + for i := 0; i < numfields; i++ { + fieldTag := t.Field(i) + val := fieldTag.Tag.Get("pretty") + if val == "-" { + continue + } + if writecomma && !expand && i > 0 { + io.WriteString(pp, ", ") + } showTypeInStruct := true if f := t.Field(i); f.Name != "" { io.WriteString(pp, f.Name) @@ -178,12 +189,11 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) { writeByte(pp, '\t') } showTypeInStruct = labelType(f.Type) + writecomma = true } pp.printValue(getField(v, i), showTypeInStruct, true) if expand { io.WriteString(pp, ",\n") - } else if i < v.NumField()-1 { - io.WriteString(pp, ", ") } } if expand { diff --git a/formatter_test.go b/formatter_test.go index c8c0b51..98a466a 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -286,3 +286,53 @@ func TestCycle(t *testing.T) { *iv = *i t.Logf("Example long interface cycle:\n%# v", Formatter(i)) } +func TestStructFieldTags1(t *testing.T) { + type testFieldTagCase struct { + tc interface{} + want string + } + + type fieldTagTest1 struct { + z string `pretty:"-"` + x, y int + } + type commaTest1 struct { + z string + x, y int `pretty:"-"` + } + type commaTest2 struct { + x, y int + private string `pretty:"-"` + a, b, c int + } + type structInInterfaceTest struct { + fields []interface{} + } + + // httpGet, _ := http.NewRequest(http.MethodGet, "https://example.org/", nil) + for i, tc := range []testFieldTagCase{ + // {tc: struct { + // r *http.Request `pretty:"-"` + // name string + // }{r: httpGet, name: "http Request test"}, + // want: `struct { r *http.Request "pretty:\"-\""; name string }{ + // name: "http Request test", + // }`}, + {tc: fieldTagTest1{x: 1, y: 2, z: "should be ignored"}, want: `pretty.fieldTagTest1{x:1, y:2}`}, + {tc: commaTest1{x: 1, y: 2, z: "should be displayed with no commas"}, want: `pretty.commaTest1{z:"should be displayed with no commas"}`}, + {tc: commaTest2{x: 1, y: 2, private: "should be ignored", a: 42, b: 3, c: 4}, want: `pretty.commaTest2{x:1, y:2, a:42, b:3, c:4}`}, + {tc: structInInterfaceTest{fields: []interface{}{commaTest2{x: 1, y: 2, private: "should be ignored", a: 42, b: 3, c: 4}}}, want: `pretty.structInInterfaceTest{ + fields: { + pretty.commaTest2{x:1, y:2, a:42, b:3, c:4}, + }, +}`}, + } { + got := Sprint(tc.tc) + // fmt.Fprintf(os.Stderr, "\ntest %d:\n%s\n", i+1, got) + if got != tc.want { + t.Logf("test %d: Got: %s", i+1, got) + t.Logf("test %d Wanted: %s", i+1, tc.want) + t.Fail() + } + } +}