diff --git a/locations/method_locations.go b/locations/method_locations.go index 116f7f38b..1756be527 100644 --- a/locations/method_locations.go +++ b/locations/method_locations.go @@ -34,13 +34,13 @@ func MethodResponseType(m *desc.MethodDescriptor) *dpb.SourceCodeInfo_Location { // MethodHTTPRule returns the precise location of the method's `google.api.http` // rule, if any. func MethodHTTPRule(m *desc.MethodDescriptor) *dpb.SourceCodeInfo_Location { - return pathLocation(m, 4, int(apb.E_Http.TypeDescriptor().Number())) // MethodDescriptor.options == 4 + return MethodOption(m, int(apb.E_Http.TypeDescriptor().Number())) } // MethodOperationInfo returns the precise location of the method's // `google.longrunning.operation_info` annotation, if any. func MethodOperationInfo(m *desc.MethodDescriptor) *dpb.SourceCodeInfo_Location { - return pathLocation(m, 4, int(lrpb.E_OperationInfo.TypeDescriptor().Number())) // MethodDescriptor.options == 4 + return MethodOption(m, int(lrpb.E_OperationInfo.TypeDescriptor().Number())) } // MethodSignature returns the precise location of the method's @@ -48,3 +48,8 @@ func MethodOperationInfo(m *desc.MethodDescriptor) *dpb.SourceCodeInfo_Location func MethodSignature(m *desc.MethodDescriptor, index int) *dpb.SourceCodeInfo_Location { return pathLocation(m, 4, int(apb.E_MethodSignature.TypeDescriptor().Number()), index) // MethodDescriptor.options == 4 } + +// MethodOption returns the precise location of the method's option with the given field number, if any. +func MethodOption(m *desc.MethodDescriptor, fieldNumber int) *dpb.SourceCodeInfo_Location { + return pathLocation(m, 4, fieldNumber) // MethodDescriptor.options == 4 +} diff --git a/locations/method_locations_test.go b/locations/method_locations_test.go index 48b40e579..7e670969e 100644 --- a/locations/method_locations_test.go +++ b/locations/method_locations_test.go @@ -117,3 +117,34 @@ func TestMethodSignature(t *testing.T) { } } } + +func TestMethodOption(t *testing.T) { + f := parse(t, ` + service Library { + rpc GetBook(GetBookRequest) returns (Book) { + option deprecated = true; + } + rpc UpdateBook(UpdateBookRequest) returns (Book) {} + } + message GetBookRequest{} + message Book {} + message UpdateBookRequest {} + `) + + for _, test := range []struct { + name string + methodIdx int + want []int32 + }{ + {"OptionSet", 0, []int32{4, 4, 29}}, + {"OptionNotSet", 1, nil}, + } { + t.Run(test.name, func(t *testing.T) { + // field number of the deprecated option == 33 + loc := MethodOption(f.GetServices()[0].GetMethods()[test.methodIdx], 33) + if diff := cmp.Diff(loc.GetSpan(), test.want); diff != "" { + t.Errorf("Diff: %s", diff) + } + }) + } +}