logger/unwrap: intergate omit functionality

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2022-11-07 15:56:09 +03:00
parent 5fbb1a923e
commit 59c08c1d9a
2 changed files with 37 additions and 8 deletions

View File

@ -168,9 +168,7 @@ func (f *unwrap) formatPtr(v reflect.Value) {
} else { } else {
_, _ = f.s.Write(bytes.Repeat(ampBytes, indirects)) _, _ = f.s.Write(bytes.Repeat(ampBytes, indirects))
} }
_, _ = f.s.Write([]byte(ve.Type().String())) _, _ = f.s.Write([]byte(ve.Type().String()))
if f.depth > 0 { if f.depth > 0 {
_, _ = f.s.Write(closeParenBytes) _, _ = f.s.Write(closeParenBytes)
} }
@ -238,11 +236,19 @@ func (f *unwrap) format(v reflect.Value) {
// get type information unless already handled elsewhere. // get type information unless already handled elsewhere.
if !f.ignoreNextType && f.s.Flag('#') { 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(openParenBytes)
} }
_, _ = f.s.Write([]byte(v.Type().String())) if v.Kind() != reflect.String {
if v.Type().Kind() != reflect.Map { _, _ = 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) _, _ = f.s.Write(closeParenBytes)
} }
} }
@ -282,18 +288,19 @@ func (f *unwrap) format(v reflect.Value) {
} }
fallthrough fallthrough
case reflect.Array: case reflect.Array:
_, _ = f.s.Write(openBracketBytes) _, _ = f.s.Write(openBraceBytes)
f.depth++ f.depth++
numEntries := v.Len() numEntries := v.Len()
for i := 0; i < numEntries; i++ { for i := 0; i < numEntries; i++ {
if i > 0 { if i > 0 {
_, _ = f.s.Write(commaBytes)
_, _ = f.s.Write(spaceBytes) _, _ = f.s.Write(spaceBytes)
} }
f.ignoreNextType = true f.ignoreNextType = true
f.format(f.unpackValue(v.Index(i))) f.format(f.unpackValue(v.Index(i)))
} }
f.depth-- f.depth--
_, _ = f.s.Write(closeBracketBytes) _, _ = f.s.Write(closeBraceBytes)
case reflect.String: case reflect.String:
_, _ = f.s.Write([]byte(`"` + v.String() + `"`)) _, _ = f.s.Write([]byte(`"` + v.String() + `"`))
case reflect.Interface: case reflect.Interface:
@ -331,11 +338,20 @@ func (f *unwrap) format(v reflect.Value) {
_, _ = f.s.Write(openBraceBytes) _, _ = f.s.Write(openBraceBytes)
f.depth++ f.depth++
vt := v.Type() vt := v.Type()
prevSkip := false
for i := 0; i < numFields; i++ { 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(commaBytes)
_, _ = f.s.Write(spaceBytes) _, _ = f.s.Write(spaceBytes)
} }
if prevSkip {
prevSkip = false
}
vtf := vt.Field(i) vtf := vt.Field(i)
if f.s.Flag('+') || f.s.Flag('#') { if f.s.Flag('+') || f.s.Flag('#') {
_, _ = f.s.Write([]byte(vtf.Name)) _, _ = f.s.Write([]byte(vtf.Name))

View File

@ -6,6 +6,19 @@ import (
"go.unistack.org/micro/v3/codec" "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) { func TestUnwrap(t *testing.T) {
string1 := "string1" string1 := "string1"
string2 := "string2" string2 := "string2"