Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ctxtags TagBasedRequestFieldExtractor extracting from fields in a oneof #327

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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