Skip to content

Commit

Permalink
grpcdynamic, grpcreflect: use grpc.ClientConnInterface and reflection…
Browse files Browse the repository at this point in the history
….GRPCServer interfaces (#491)
  • Loading branch information
jhump authored Feb 10, 2022
1 parent c329f1e commit 6224817
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
7 changes: 1 addition & 6 deletions dynamic/grpcdynamic/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ type Stub struct {
// type used to construct Stubs. But the use of this interface allows
// construction of stubs that use alternate concrete types as the transport for
// RPC operations.
type Channel interface {
Invoke(ctx context.Context, method string, args, reply interface{}, opts ...grpc.CallOption) error
NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)
}

var _ Channel = (*grpc.ClientConn)(nil)
type Channel = grpc.ClientConnInterface

// NewStub creates a new RPC stub that uses the given channel for dispatching RPCs.
func NewStub(channel Channel) Stub {
Expand Down
8 changes: 7 additions & 1 deletion grpcreflect/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import (
"fmt"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/jhump/protoreflect/desc"
)

// GRPCServer is the interface provided by a gRPC server. In addition to being a
// service registrar (for registering services and handlers), it also has an
// accessor for retrieving metadata about all registered services.
type GRPCServer = reflection.GRPCServer

// LoadServiceDescriptors loads the service descriptors for all services exposed by the
// given GRPC server.
func LoadServiceDescriptors(s *grpc.Server) (map[string]*desc.ServiceDescriptor, error) {
func LoadServiceDescriptors(s GRPCServer) (map[string]*desc.ServiceDescriptor, error) {
descs := map[string]*desc.ServiceDescriptor{}
for name, info := range s.GetServiceInfo() {
file, ok := info.Metadata.(string)
Expand Down
21 changes: 21 additions & 0 deletions grpcreflect/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,24 @@ func TestLoadServiceDescriptors(t *testing.T) {
testutil.Eq(t, c.response, md.GetOutputType().GetFullyQualifiedName())
}
}

func TestLoadServiceDescriptor(t *testing.T) {
sd, err := LoadServiceDescriptor(testprotos.TestService_ServiceDesc)
testutil.Ok(t, err)

cases := []struct{ method, request, response string }{
{"DoSomething", "testprotos.TestRequest", "jhump.protoreflect.desc.Bar"},
{"DoSomethingElse", "testprotos.TestMessage", "testprotos.TestResponse"},
{"DoSomethingAgain", "jhump.protoreflect.desc.Bar", "testprotos.AnotherTestMessage"},
{"DoSomethingForever", "testprotos.TestRequest", "testprotos.TestResponse"},
}

testutil.Eq(t, len(cases), len(sd.GetMethods()))

for i, c := range cases {
md := sd.GetMethods()[i]
testutil.Eq(t, c.method, md.GetName())
testutil.Eq(t, c.request, md.GetInputType().GetFullyQualifiedName())
testutil.Eq(t, c.response, md.GetOutputType().GetFullyQualifiedName())
}
}
2 changes: 2 additions & 0 deletions internal/testprotos/gen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package testprotos

//go:generate ./make_protos.sh

var TestService_ServiceDesc = &_TestService_serviceDesc

0 comments on commit 6224817

Please sign in to comment.