From 59c08c1d9a59acd92f41f58428cc2250798ba909 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 7 Nov 2022 15:56:09 +0300 Subject: [PATCH] logger/unwrap: intergate omit functionality Signed-off-by: Vasiliy Tolstov --- logger/unwrap/unwrap.go | 32 ++++++++++++++++++++++++-------- logger/unwrap/unwrap_test.go | 13 +++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/logger/unwrap/unwrap.go b/logger/unwrap/unwrap.go index 69961f9a..26d110ce 100644 --- a/logger/unwrap/unwrap.go +++ b/logger/unwrap/unwrap.go @@ -168,9 +168,7 @@ func (f *unwrap) formatPtr(v reflect.Value) { } else { _, _ = f.s.Write(bytes.Repeat(ampBytes, indirects)) } - _, _ = f.s.Write([]byte(ve.Type().String())) - if f.depth > 0 { _, _ = f.s.Write(closeParenBytes) } @@ -238,11 +236,19 @@ func (f *unwrap) format(v reflect.Value) { // get type information unless already handled elsewhere. if !f.ignoreNextType && f.s.Flag('#') { - if v.Type().Kind() != reflect.Map { + if v.Type().Kind() != reflect.Map && + v.Type().Kind() != reflect.String && + v.Type().Kind() != reflect.Array && + v.Type().Kind() != reflect.Slice { _, _ = f.s.Write(openParenBytes) } - _, _ = f.s.Write([]byte(v.Type().String())) - if v.Type().Kind() != reflect.Map { + if v.Kind() != reflect.String { + _, _ = f.s.Write([]byte(v.Type().String())) + } + if v.Type().Kind() != reflect.Map && + v.Type().Kind() != reflect.String && + v.Type().Kind() != reflect.Array && + v.Type().Kind() != reflect.Slice { _, _ = f.s.Write(closeParenBytes) } } @@ -282,18 +288,19 @@ func (f *unwrap) format(v reflect.Value) { } fallthrough case reflect.Array: - _, _ = f.s.Write(openBracketBytes) + _, _ = f.s.Write(openBraceBytes) f.depth++ numEntries := v.Len() for i := 0; i < numEntries; i++ { if i > 0 { + _, _ = f.s.Write(commaBytes) _, _ = f.s.Write(spaceBytes) } f.ignoreNextType = true f.format(f.unpackValue(v.Index(i))) } f.depth-- - _, _ = f.s.Write(closeBracketBytes) + _, _ = f.s.Write(closeBraceBytes) case reflect.String: _, _ = f.s.Write([]byte(`"` + v.String() + `"`)) case reflect.Interface: @@ -331,11 +338,20 @@ func (f *unwrap) format(v reflect.Value) { _, _ = f.s.Write(openBraceBytes) f.depth++ vt := v.Type() + prevSkip := false for i := 0; i < numFields; i++ { - if i > 0 { + sv, ok := vt.Field(i).Tag.Lookup("logger") + if ok && sv == "omit" { + prevSkip = true + continue + } + if i > 0 && !prevSkip { _, _ = f.s.Write(commaBytes) _, _ = f.s.Write(spaceBytes) } + if prevSkip { + prevSkip = false + } vtf := vt.Field(i) if f.s.Flag('+') || f.s.Flag('#') { _, _ = f.s.Write([]byte(vtf.Name)) diff --git a/logger/unwrap/unwrap_test.go b/logger/unwrap/unwrap_test.go index e4cce5ea..c0da0851 100644 --- a/logger/unwrap/unwrap_test.go +++ b/logger/unwrap/unwrap_test.go @@ -6,6 +6,19 @@ import ( "go.unistack.org/micro/v3/codec" ) +func TestUnwrapOmit(t *testing.T) { + type val struct { + MP map[string]string `json:"mp" logger:"omit"` + STR string `json:"str"` + AR []string `json:"ar"` + } + + v1 := &val{AR: []string{"string1", "string2"}, STR: "string", MP: map[string]string{"key": "val"}} + + t.Logf("output: %#v", v1) + t.Logf("output: %#v", Unwrap(v1)) +} + func TestUnwrap(t *testing.T) { string1 := "string1" string2 := "string2"