diff --git a/client/client.go b/client/client.go index 57a4f2b7..3d43df7a 100644 --- a/client/client.go +++ b/client/client.go @@ -74,7 +74,7 @@ type Request interface { type Response interface { // Read the response Codec() codec.Codec - // read the header + // Header data Header() metadata.Metadata // Read the undecoded response Read() ([]byte, error) diff --git a/logger/unwrap/unwrap.go b/logger/unwrap/unwrap.go index 26d110ce..30c647e1 100644 --- a/logger/unwrap/unwrap.go +++ b/logger/unwrap/unwrap.go @@ -55,15 +55,16 @@ type unwrap struct { } type Options struct { - Codec codec.Codec - Indent string - UnwrapMethods bool + Codec codec.Codec + Indent string + Methods bool + Tagged bool } func NewOptions(opts ...Option) Options { options := Options{ - Indent: " ", - UnwrapMethods: false, + Indent: " ", + Methods: false, } for _, o := range opts { o(&options) @@ -73,24 +74,30 @@ func NewOptions(opts ...Option) Options { type Option func(*Options) -func UnwrapIndent(f string) Option { +func Indent(f string) Option { return func(o *Options) { o.Indent = f } } -func UnwrapMethods(b bool) Option { +func Methods(b bool) Option { return func(o *Options) { - o.UnwrapMethods = b + o.Methods = b } } -func UnwrapCodec(c codec.Codec) Option { +func Codec(c codec.Codec) Option { return func(o *Options) { o.Codec = c } } +func Tagged(b bool) Option { + return func(o *Options) { + o.Tagged = b + } +} + func Unwrap(val interface{}, opts ...Option) *unwrap { options := NewOptions(opts...) return &unwrap{val: val, opts: &options, pointers: make(map[uintptr]int)} @@ -256,7 +263,7 @@ func (f *unwrap) format(v reflect.Value) { // Call Stringer/error interfaces if they exist and the handle methods // flag is enabled. - if !f.opts.UnwrapMethods { + if !f.opts.Methods { if (kind != reflect.Invalid) && (kind != reflect.Interface) { if handled := handleMethods(f.opts, f.s, v); handled { return @@ -341,7 +348,12 @@ func (f *unwrap) format(v reflect.Value) { prevSkip := false for i := 0; i < numFields; i++ { sv, ok := vt.Field(i).Tag.Lookup("logger") - if ok && sv == "omit" { + if ok { + if sv == "omit" { + prevSkip = true + continue + } + } else if f.opts.Tagged { prevSkip = true continue } diff --git a/logger/unwrap/unwrap_test.go b/logger/unwrap/unwrap_test.go index c0da0851..b24fe0e9 100644 --- a/logger/unwrap/unwrap_test.go +++ b/logger/unwrap/unwrap_test.go @@ -1,24 +1,13 @@ package unwrap import ( + "fmt" + "strings" "testing" "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" @@ -32,7 +21,10 @@ func TestUnwrap(t *testing.T) { v1 := &val1{ar: []*string{&string1, &string2}, str: &string1, val: &val1{str: &string2}, mp: map[string]string{"key": "val"}} - t.Logf("output: %#v", Unwrap(v1)) + buf := fmt.Sprintf("%#v", Unwrap(v1)) + if strings.Compare(buf, `&unwrap.val1{mp:map[string]string{"key":"val"}, val:(*unwrap.val1){mp:map[string]string, val:(*unwrap.val1), str:(*string)"string2", ar:[]*string}, str:(*string)"string1", ar:[]*string{<*>, <*>"string2"}}`) != 0 { + t.Fatalf("not proper written %s", buf) + } type val2 struct { mp map[string]string @@ -42,11 +34,11 @@ func TestUnwrap(t *testing.T) { } v2 := &val2{ar: []string{string1, string2}, str: string1, val: &val2{str: string2}, mp: map[string]string{"key": "val"}} - - t.Logf("output: %#v", v2) + _ = v2 + // t.Logf("output: %#v", v2) } -func TestUnwrapCodec(t *testing.T) { +func TestCodec(t *testing.T) { type val struct { MP map[string]string `json:"mp"` STR string `json:"str"` @@ -55,5 +47,33 @@ func TestUnwrapCodec(t *testing.T) { v1 := &val{AR: []string{"string1", "string2"}, STR: "string", MP: map[string]string{"key": "val"}} - t.Logf("output: %#v", Unwrap(v1, UnwrapCodec(codec.NewCodec()))) + buf := fmt.Sprintf("%#v", Unwrap(v1, Codec(codec.NewCodec()))) + if strings.Compare(buf, `{"mp":{"key":"val"},"str":"string","ar":["string1","string2"]}`) != 0 { + t.Fatalf("not proper written %s", buf) + } +} + +func TestOmit(t *testing.T) { + type val struct { + Key1 string `logger:"omit"` + Key2 string `logger:"take"` + } + v1 := &val{Key1: "val1", Key2: "val2"} + buf := fmt.Sprintf("%#v", Unwrap(v1)) + if strings.Compare(buf, `&unwrap.val{Key2:"val2"}`) != 0 { + t.Fatalf("not proper written %s", buf) + } +} + +func TestTagged(t *testing.T) { + type val struct { + Key1 string `logger:"take"` + Key2 string + } + + v1 := &val{Key1: "val1", Key2: "val2"} + buf := fmt.Sprintf("%#v", Unwrap(v1, Tagged(true))) + if strings.Compare(buf, `&unwrap.val{Key1:"val1"}`) != 0 { + t.Fatalf("not proper written %s", buf) + } }