Skip to content

Commit

Permalink
Fix ctxtags TagBasedRequestFieldExtractor extracting from fields in a…
Browse files Browse the repository at this point in the history
… oneof

Resolves grpc-ecosystem#326
  • Loading branch information
nvx committed Aug 28, 2020
1 parent 7a5efaa commit 4976427
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 28 deletions.
2 changes: 1 addition & 1 deletion tags/fieldextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions tags/fieldextractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
136 changes: 109 additions & 27 deletions testing/gogotestproto/fields.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions testing/gogotestproto/fields.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4976427

Please sign in to comment.