Skip to content

Commit

Permalink
refactor: add MethodOption location function (#900)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxim Tschumak <[email protected]>
Co-authored-by: Andrew Paseltiner <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2021
1 parent 9d91359 commit 839205e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions locations/method_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ 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
// `google.api.method_signature` annotation, if any.
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
}
31 changes: 31 additions & 0 deletions locations/method_locations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 839205e

Please sign in to comment.