diff --git a/tags/fieldextractor.go b/tags/fieldextractor.go index 1ede0866d..a4073ab49 100644 --- a/tags/fieldextractor.go +++ b/tags/fieldextractor.go @@ -64,7 +64,7 @@ func reflectMessageTags(msg interface{}, existingMap map[string]interface{}, tag field := v.Field(i) kind := field.Kind() // Only recurse down direct pointers, which should only be to nested structs. - if kind == reflect.Ptr && field.CanInterface() { + if (kind == reflect.Ptr || kind == reflect.Interface) && field.CanInterface() { reflectMessageTags(field.Interface(), existingMap, tagName) } // In case of arrays/slices (repeated fields) go down to the concrete type. diff --git a/tags/fieldextractor_test.go b/tags/fieldextractor_test.go index a20719e0e..6af38f717 100644 --- a/tags/fieldextractor_test.go +++ b/tags/fieldextractor_test.go @@ -52,6 +52,16 @@ func TestTaggedRequestFiledExtractor_PongRequest(t *testing.T) { assert.EqualValues(t, []string{"tagone", "tagtwo"}, valMap["meta_tags"]) } +func TestTaggedRequestFiledExtractor_OneOfLogField(t *testing.T) { + req := &pb_gogotestproto.OneOfLogField{ + Identifier: &pb_gogotestproto.OneOfLogField_BarId{ + BarId: "bar-log-field", + }, + } + valMap := grpc_ctxtags.TagBasedRequestFieldExtractor("log_field")("", req) + assert.EqualValues(t, "bar-log-field", valMap["bar_id"]) +} + // Test to ensure TagBasedRequestFieldExtractor does not panic when encountering private struct members such as // when using gogoproto.stdtime which results in a time.Time that has private struct members func TestTaggedRequestFiledExtractor_GogoTime(t *testing.T) { diff --git a/testing/gogotestproto/fields.pb.go b/testing/gogotestproto/fields.pb.go index 8e6c5999d..0d3ca9582 100644 --- a/testing/gogotestproto/fields.pb.go +++ b/testing/gogotestproto/fields.pb.go @@ -24,7 +24,7 @@ var _ = time.Kitchen // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package type Metadata struct { Tags []string `protobuf:"bytes,1,rep,name=tags,proto3" json:"tags,omitempty" log_field:"meta_tags"` @@ -232,6 +232,83 @@ func (m *Pong) GetId() string { return "" } +type OneOfLogField struct { + // Types that are valid to be assigned to Identifier: + // *OneOfLogField_BarId + // *OneOfLogField_BazId + Identifier isOneOfLogField_Identifier `protobuf_oneof:"identifier"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *OneOfLogField) Reset() { *m = OneOfLogField{} } +func (m *OneOfLogField) String() string { return proto.CompactTextString(m) } +func (*OneOfLogField) ProtoMessage() {} +func (*OneOfLogField) Descriptor() ([]byte, []int) { + return fileDescriptor_d39ad626ec0e575e, []int{5} +} +func (m *OneOfLogField) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_OneOfLogField.Unmarshal(m, b) +} +func (m *OneOfLogField) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_OneOfLogField.Marshal(b, m, deterministic) +} +func (m *OneOfLogField) XXX_Merge(src proto.Message) { + xxx_messageInfo_OneOfLogField.Merge(m, src) +} +func (m *OneOfLogField) XXX_Size() int { + return xxx_messageInfo_OneOfLogField.Size(m) +} +func (m *OneOfLogField) XXX_DiscardUnknown() { + xxx_messageInfo_OneOfLogField.DiscardUnknown(m) +} + +var xxx_messageInfo_OneOfLogField proto.InternalMessageInfo + +type isOneOfLogField_Identifier interface { + isOneOfLogField_Identifier() +} + +type OneOfLogField_BarId struct { + BarId string `protobuf:"bytes,1,opt,name=bar_id,json=barId,proto3,oneof" json:"bar_id,omitempty" log_field:"bar_id"` +} +type OneOfLogField_BazId struct { + BazId string `protobuf:"bytes,2,opt,name=baz_id,json=bazId,proto3,oneof" json:"baz_id,omitempty" log_field:"baz_id"` +} + +func (*OneOfLogField_BarId) isOneOfLogField_Identifier() {} +func (*OneOfLogField_BazId) isOneOfLogField_Identifier() {} + +func (m *OneOfLogField) GetIdentifier() isOneOfLogField_Identifier { + if m != nil { + return m.Identifier + } + return nil +} + +func (m *OneOfLogField) GetBarId() string { + if x, ok := m.GetIdentifier().(*OneOfLogField_BarId); ok { + return x.BarId + } + return "" +} + +func (m *OneOfLogField) GetBazId() string { + if x, ok := m.GetIdentifier().(*OneOfLogField_BazId); ok { + return x.BazId + } + return "" +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*OneOfLogField) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*OneOfLogField_BarId)(nil), + (*OneOfLogField_BazId)(nil), + } +} + type PongRequest struct { Pong *Pong `protobuf:"bytes,1,opt,name=pong,proto3" json:"pong,omitempty"` Meta *Metadata `protobuf:"bytes,2,opt,name=meta,proto3" json:"meta,omitempty"` @@ -244,7 +321,7 @@ func (m *PongRequest) Reset() { *m = PongRequest{} } func (m *PongRequest) String() string { return proto.CompactTextString(m) } func (*PongRequest) ProtoMessage() {} func (*PongRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_d39ad626ec0e575e, []int{5} + return fileDescriptor_d39ad626ec0e575e, []int{6} } func (m *PongRequest) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_PongRequest.Unmarshal(m, b) @@ -289,7 +366,7 @@ func (m *GoGoProtoStdTime) Reset() { *m = GoGoProtoStdTime{} } func (m *GoGoProtoStdTime) String() string { return proto.CompactTextString(m) } func (*GoGoProtoStdTime) ProtoMessage() {} func (*GoGoProtoStdTime) Descriptor() ([]byte, []int) { - return fileDescriptor_d39ad626ec0e575e, []int{6} + return fileDescriptor_d39ad626ec0e575e, []int{7} } func (m *GoGoProtoStdTime) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_GoGoProtoStdTime.Unmarshal(m, b) @@ -322,6 +399,7 @@ func init() { proto.RegisterType((*Ping)(nil), "mwitkow.testproto.Ping") proto.RegisterType((*PingRequest)(nil), "mwitkow.testproto.PingRequest") proto.RegisterType((*Pong)(nil), "mwitkow.testproto.Pong") + proto.RegisterType((*OneOfLogField)(nil), "mwitkow.testproto.OneOfLogField") proto.RegisterType((*PongRequest)(nil), "mwitkow.testproto.PongRequest") proto.RegisterType((*GoGoProtoStdTime)(nil), "mwitkow.testproto.GoGoProtoStdTime") } @@ -329,28 +407,32 @@ func init() { func init() { proto.RegisterFile("fields.proto", fileDescriptor_d39ad626ec0e575e) } var fileDescriptor_d39ad626ec0e575e = []byte{ - // 362 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xbd, 0x4e, 0xf3, 0x30, - 0x18, 0x85, 0xbf, 0xf4, 0x4b, 0x2b, 0xe2, 0x32, 0x80, 0x01, 0xf5, 0x07, 0x89, 0x54, 0x5e, 0x28, - 0x42, 0x75, 0x44, 0x99, 0x60, 0x60, 0xc8, 0x52, 0x31, 0x20, 0x55, 0xa6, 0x7b, 0x95, 0x62, 0xd7, - 0x58, 0x4d, 0xfa, 0x16, 0xe2, 0xd0, 0xdb, 0x60, 0xe4, 0xea, 0xca, 0xc0, 0x1d, 0xf4, 0x0a, 0x90, - 0x9d, 0xfe, 0xa1, 0x02, 0x03, 0x5b, 0x5e, 0xfb, 0x39, 0xc7, 0xe7, 0xc4, 0x46, 0xbb, 0x43, 0x25, - 0x62, 0x9e, 0xd2, 0xc9, 0x33, 0x68, 0xc0, 0xfb, 0xc9, 0x54, 0xe9, 0x11, 0x4c, 0xa9, 0x16, 0xa9, - 0xb6, 0x4b, 0xf5, 0x96, 0x54, 0xfa, 0x31, 0x1b, 0xd0, 0x07, 0x48, 0x02, 0x09, 0x12, 0x02, 0xbb, - 0x3c, 0xc8, 0x86, 0x76, 0xb2, 0x83, 0xfd, 0xca, 0x1d, 0xea, 0xbe, 0x04, 0x90, 0xb1, 0x58, 0x53, - 0x5a, 0x25, 0x22, 0xd5, 0x51, 0x32, 0xc9, 0x01, 0x72, 0x85, 0x76, 0xee, 0x84, 0x8e, 0x78, 0xa4, - 0x23, 0xdc, 0x42, 0xae, 0x8e, 0x64, 0x5a, 0x75, 0x1a, 0xff, 0x9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6b, 0x49, 0xe8, 0xbe, 0x7d, 0x9c, 0xfc, 0x1b, 0x94, 0x2c, 0x7a, 0xf9, 0x19, 0x00, 0x00, - 0xff, 0xff, 0x4a, 0x19, 0x24, 0x1a, 0x95, 0x02, 0x00, 0x00, + // 427 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xcd, 0x8e, 0xd3, 0x30, + 0x14, 0x85, 0x27, 0x25, 0xad, 0xe8, 0xed, 0x20, 0x81, 0xf9, 0x99, 0x4e, 0x91, 0x48, 0xe5, 0x0d, + 0x45, 0x68, 0x1c, 0x31, 0xac, 0x60, 0xc1, 0x22, 0x0b, 0x4a, 0x25, 0xd0, 0x54, 0x66, 0xf6, 0x95, + 0x83, 0x5d, 0x63, 0x4d, 0x93, 0x5b, 0x1a, 0x87, 0x91, 0xba, 0xe1, 0x15, 0x58, 0xf2, 0x74, 0x65, + 0xc1, 0x1b, 0xf4, 0x09, 0x90, 0x9d, 0xfe, 0x04, 0x0d, 0x65, 0xc1, 0x2e, 0xf6, 0x3d, 0xdf, 0x71, + 0xce, 0xd1, 0x85, 0xe3, 0xa9, 0x51, 0x33, 0x59, 0xb0, 0xf9, 0x02, 0x2d, 0x92, 0x7b, 0xd9, 0xb5, + 0xb1, 0x57, 0x78, 0xcd, 0xac, 0x2a, 0xac, 0xbf, 0xea, 0x9d, 0x69, 0x63, 0x3f, 0x97, 0x29, 0xfb, + 0x84, 0x59, 0xac, 0x51, 0x63, 0xec, 0xaf, 0xd3, 0x72, 0xea, 0x4f, 0xfe, 0xe0, 0xbf, 0x2a, 0x87, + 0x5e, 0xa4, 0x11, 0xf5, 0x4c, 0xed, 0x55, 0xd6, 0x64, 0xaa, 0xb0, 0x22, 0x9b, 0x57, 0x02, 0xfa, + 0x0a, 0x6e, 0x7f, 0x50, 0x56, 0x48, 0x61, 0x05, 0x39, 0x83, 0xd0, 0x0a, 0x5d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 0xc0, 0x72, 0x24, 0x93, 0x63, 0x00, 0x23, 0x55, 0x6e, 0xcd, 0xd4, 0xa8, 0x85, + 0xaf, 0x07, 0xff, 0xac, 0x07, 0xff, 0x5d, 0x0f, 0xfa, 0x7a, 0xf0, 0x7f, 0xea, 0xe1, 0x70, 0x77, + 0x88, 0x43, 0x1c, 0xbb, 0xd9, 0x47, 0x2b, 0x2f, 0x4d, 0xa6, 0xc8, 0x1b, 0x68, 0xef, 0x36, 0x6d, + 0xf3, 0x6c, 0x8f, 0x55, 0xbb, 0xc8, 0xb6, 0xbb, 0xc8, 0x2e, 0xb7, 0x8a, 0x24, 0xfc, 0xfe, 0x33, + 0x0a, 0xf8, 0x1e, 0x49, 0xc2, 0x1f, 0xbf, 0x9e, 0x1c, 0xa5, 0x2d, 0x2f, 0x7d, 0xf9, 0x3b, 0x00, + 0x00, 0xff, 0xff, 0xa5, 0x38, 0xdf, 0x28, 0x16, 0x03, 0x00, 0x00, } diff --git a/testing/gogotestproto/fields.proto b/testing/gogotestproto/fields.proto index f6986059b..995dfc7bd 100644 --- a/testing/gogotestproto/fields.proto +++ b/testing/gogotestproto/fields.proto @@ -30,6 +30,13 @@ message Pong { string id = 1 [(gogoproto.moretags) = "log_field:\"pong_id\""]; } +message OneOfLogField { + oneof identifier { + string bar_id = 1 [(gogoproto.moretags) = "log_field:\"bar_id\""]; + string baz_id = 2 [(gogoproto.moretags) = "log_field:\"baz_id\""]; + } +} + message PongRequest { Pong pong = 1; Metadata meta = 2;