Change server.ServiceMetadata to take service name and method name.
And some minor changes.
This commit is contained in:
@ -58,6 +58,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||||
@ -205,8 +206,15 @@ func (s *serverReflectionServer) fileDescEncodingContainingSymbol(name string) (
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check if it's a service name or method name.
|
// Check if it's a service name.
|
||||||
meta := s.s.Metadata(name)
|
meta := s.s.ServiceMetadata(name, "")
|
||||||
|
// Check if it's a method name.
|
||||||
|
if meta == nil {
|
||||||
|
pos := strings.LastIndex(name, ".")
|
||||||
|
if pos != -1 {
|
||||||
|
meta = s.s.ServiceMetadata(name[:pos], name[pos+1:])
|
||||||
|
}
|
||||||
|
}
|
||||||
if meta != nil {
|
if meta != nil {
|
||||||
if enc, ok := meta.([]byte); ok {
|
if enc, ok := meta.([]byte); ok {
|
||||||
fd, err = s.decodeFileDesc(enc)
|
fd, err = s.decodeFileDesc(enc)
|
||||||
|
@ -138,7 +138,7 @@ func (s *server) StreamingSearch(stream pb.SearchService_StreamingSearchServer)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEnd2end(t *testing.T) {
|
func TestReflectionEnd2end(t *testing.T) {
|
||||||
// Start server.
|
// Start server.
|
||||||
lis, err := net.Listen("tcp", "localhost:0")
|
lis, err := net.Listen("tcp", "localhost:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
38
server.go
38
server.go
@ -245,38 +245,32 @@ func (s *Server) register(sd *ServiceDesc, ss interface{}) {
|
|||||||
s.m[sd.ServiceName] = srv
|
s.m[sd.ServiceName] = srv
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metadata returns the metadata for a given symbol name.
|
// ServiceMetadata returns the metadata for a service or method.
|
||||||
// The name can be a service name or a method name in the form of
|
// service should be the full service name with package, in the form of <package>.<service>.
|
||||||
// <package>.<service>[.<method>].
|
// method should be the method name only.
|
||||||
func (s *Server) Metadata(name string) interface{} {
|
// If only service is important, method should be an empty string.
|
||||||
// Check if the name is a service name.
|
func (s *Server) ServiceMetadata(service, method string) interface{} {
|
||||||
if srv, ok := s.m[name]; ok {
|
// Check if service is registered.
|
||||||
return srv.meta
|
if srv, ok := s.m[service]; ok {
|
||||||
}
|
if method == "" {
|
||||||
// Check if the name is a method name.
|
|
||||||
pos := strings.LastIndex(name, ".")
|
|
||||||
if pos == -1 {
|
|
||||||
// Invalid method name.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if srv, ok := s.m[name[:pos]]; ok {
|
|
||||||
if _, ok := srv.md[name[pos+1:]]; ok {
|
|
||||||
return srv.meta
|
return srv.meta
|
||||||
}
|
}
|
||||||
if _, ok := srv.sd[name[pos+1:]]; ok {
|
// Check if method is part of service.
|
||||||
|
if _, ok := srv.md[method]; ok {
|
||||||
|
return srv.meta
|
||||||
|
}
|
||||||
|
if _, ok := srv.sd[method]; ok {
|
||||||
return srv.meta
|
return srv.meta
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AllServiceNames returns names of all the registered services.
|
// AllServiceNames returns all the registered service names.
|
||||||
func (s *Server) AllServiceNames() []string {
|
func (s *Server) AllServiceNames() []string {
|
||||||
ret := make([]string, len(s.m))
|
ret := make([]string, 0, len(s.m))
|
||||||
i := 0
|
|
||||||
for k := range s.m {
|
for k := range s.m {
|
||||||
ret[i] = k
|
ret = append(ret, k)
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
@ -88,46 +88,50 @@ func TestStopBeforeServe(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetadata(t *testing.T) {
|
func TestServiceMetadata(t *testing.T) {
|
||||||
server := NewServer()
|
server := NewServer()
|
||||||
server.RegisterService(&testSd, &testServer{})
|
server.RegisterService(&testSd, &testServer{})
|
||||||
|
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
name string
|
service string
|
||||||
want []byte
|
method string
|
||||||
|
want []byte
|
||||||
}{
|
}{
|
||||||
{"grpc.testing.EmptyService", testFd},
|
{"grpc.testing.EmptyService", "", testFd},
|
||||||
{"grpc.testing.EmptyService.EmptyCall", testFd},
|
{"grpc.testing.EmptyService", "EmptyCall", testFd},
|
||||||
{"grpc.testing.EmptyService.EmptyStream", testFd},
|
{"grpc.testing.EmptyService", "EmptyStream", testFd},
|
||||||
} {
|
} {
|
||||||
meta := server.Metadata(test.name)
|
meta := server.ServiceMetadata(test.service, test.method)
|
||||||
var (
|
var (
|
||||||
fd []byte
|
fd []byte
|
||||||
ok bool
|
ok bool
|
||||||
)
|
)
|
||||||
if fd, ok = meta.([]byte); !ok {
|
if fd, ok = meta.([]byte); !ok {
|
||||||
t.Errorf("Metadata(%q)=%v, want %v", test.name, meta, test.want)
|
t.Errorf("ServiceMetadata(%q, %q) = %v, want %v", test.service, test.method, meta, test.want)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(fd, test.want) {
|
if !reflect.DeepEqual(fd, test.want) {
|
||||||
t.Errorf("Metadata(%q)=%v, want %v", test.name, fd, test.want)
|
t.Errorf("ServiceMetadata(%q, %q) = %v, want %v", test.service, test.method, fd, test.want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetadataNotFound(t *testing.T) {
|
func TestServiceMetadataNotFound(t *testing.T) {
|
||||||
server := NewServer()
|
server := NewServer()
|
||||||
server.RegisterService(&testSd, &testServer{})
|
server.RegisterService(&testSd, &testServer{})
|
||||||
|
|
||||||
for _, test := range []string{
|
for _, test := range []struct {
|
||||||
"EmptyCall",
|
service string
|
||||||
"grpc.EmptyService",
|
method string
|
||||||
"grpc.EmptyService.EmptyCall",
|
}{
|
||||||
"grpc.testing.EmptyService.EmptyCallWrong",
|
{"", "EmptyCall"},
|
||||||
"grpc.testing.EmptyService.EmptyStreamWrong",
|
{"grpc.EmptyService", ""},
|
||||||
|
{"grpc.EmptyService", "EmptyCall"},
|
||||||
|
{"grpc.testing.EmptyService", "EmptyCallWrong"},
|
||||||
|
{"grpc.testing.EmptyService", "EmptyStreamWrong"},
|
||||||
} {
|
} {
|
||||||
meta := server.Metadata(test)
|
meta := server.ServiceMetadata(test.service, test.method)
|
||||||
if meta != nil {
|
if meta != nil {
|
||||||
t.Errorf("Metadata(%q)=%v, want <nil>", test, meta)
|
t.Errorf("ServiceMetadata(%q, %q) = %v, want <nil>", test.service, test.method, meta)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user