Merge pull request #150 from unistack-org/unwrap

logger/unwrap: intergate omit functionality
This commit is contained in:
Василий Толстов 2022-11-07 16:00:45 +03:00 committed by GitHub
commit 18fafbbbab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 8 deletions

View File

@ -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))

View File

@ -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"