Skip to content

Commit

Permalink
feat: add MethodOption location function
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-tschumak committed Nov 12, 2021
1 parent 9d91359 commit 4da1db7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions locations/method_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/jhump/protoreflect/desc"
apb "google.golang.org/genproto/googleapis/api/annotations"
lrpb "google.golang.org/genproto/googleapis/longrunning"
"google.golang.org/protobuf/reflect/protoreflect"
)

// MethodRequestType returns the precise location of the method's input type.
Expand Down Expand Up @@ -48,3 +49,9 @@ 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 name, if any.
func MethodOption(m *desc.MethodDescriptor, option string) *dpb.SourceCodeInfo_Location {
fd := m.GetMethodOptions().ProtoReflect().Descriptor().Fields().ByName(protoreflect.Name(option))
return pathLocation(m, 4, int(fd.Number())) // MethodDescriptor.options == 4
}
30 changes: 30 additions & 0 deletions locations/method_locations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,33 @@ func TestMethodSignature(t *testing.T) {
}
}
}

func TestMethodOption(t *testing.T) {
f := parse(t, `
import "google/api/client.proto";
service Library {
rpc GetBook(GetBookRequest) returns (Book) {
option deadline = 60.0;
}
rpc WriteBook(NewBook) returns (Book) {}
}
message GetBookRequest{}
message Book {}
message NewBook {}
`)

for _, test := range []struct {
name string
methodIdx int
option string
want []int32
}{
{"First", 0, "deadline", []int32{5, 20, 43}},
{"Second", 1, "deadline", nil},
} {
loc := MethodOption(f.GetServices()[0].GetMethods()[test.methodIdx], test.option)
if diff := cmp.Diff(loc.GetSpan(), test.want); diff != "" {
t.Errorf("%s: %s", test.name, diff)
}
}
}

0 comments on commit 4da1db7

Please sign in to comment.