Change server.ServiceMetadata to take service name and method name.
And some minor changes.
This commit is contained in:
Родитель
1302eb9c41
Коммит
69c7425a21
|
@ -58,6 +58,7 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||
|
@ -205,8 +206,15 @@ func (s *serverReflectionServer) fileDescEncodingContainingSymbol(name string) (
|
|||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// Check if it's a service name or method name.
|
||||
meta := s.s.Metadata(name)
|
||||
// Check if it's a service 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 enc, ok := meta.([]byte); ok {
|
||||
fd, err = s.decodeFileDesc(enc)
|
||||
|
|
|
@ -138,7 +138,7 @@ func (s *server) StreamingSearch(stream pb.SearchService_StreamingSearchServer)
|
|||
return nil
|
||||
}
|
||||
|
||||
func TestEnd2end(t *testing.T) {
|
||||
func TestReflectionEnd2end(t *testing.T) {
|
||||
// Start server.
|
||||
lis, err := net.Listen("tcp", "localhost:0")
|
||||
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
|
||||
}
|
||||
|
||||
// Metadata returns the metadata for a given symbol name.
|
||||
// The name can be a service name or a method name in the form of
|
||||
// <package>.<service>[.<method>].
|
||||
func (s *Server) Metadata(name string) interface{} {
|
||||
// Check if the name is a service name.
|
||||
if srv, ok := s.m[name]; ok {
|
||||
return srv.meta
|
||||
}
|
||||
// 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 {
|
||||
// ServiceMetadata returns the metadata for a service or method.
|
||||
// service should be the full service name with package, in the form of <package>.<service>.
|
||||
// method should be the method name only.
|
||||
// If only service is important, method should be an empty string.
|
||||
func (s *Server) ServiceMetadata(service, method string) interface{} {
|
||||
// Check if service is registered.
|
||||
if srv, ok := s.m[service]; ok {
|
||||
if method == "" {
|
||||
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 nil
|
||||
}
|
||||
|
||||
// AllServiceNames returns names of all the registered services.
|
||||
// AllServiceNames returns all the registered service names.
|
||||
func (s *Server) AllServiceNames() []string {
|
||||
ret := make([]string, len(s.m))
|
||||
i := 0
|
||||
ret := make([]string, 0, len(s.m))
|
||||
for k := range s.m {
|
||||
ret[i] = k
|
||||
i++
|
||||
ret = append(ret, k)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
|
|
@ -88,46 +88,50 @@ func TestStopBeforeServe(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMetadata(t *testing.T) {
|
||||
func TestServiceMetadata(t *testing.T) {
|
||||
server := NewServer()
|
||||
server.RegisterService(&testSd, &testServer{})
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
want []byte
|
||||
service string
|
||||
method string
|
||||
want []byte
|
||||
}{
|
||||
{"grpc.testing.EmptyService", testFd},
|
||||
{"grpc.testing.EmptyService.EmptyCall", testFd},
|
||||
{"grpc.testing.EmptyService.EmptyStream", testFd},
|
||||
{"grpc.testing.EmptyService", "", testFd},
|
||||
{"grpc.testing.EmptyService", "EmptyCall", testFd},
|
||||
{"grpc.testing.EmptyService", "EmptyStream", testFd},
|
||||
} {
|
||||
meta := server.Metadata(test.name)
|
||||
meta := server.ServiceMetadata(test.service, test.method)
|
||||
var (
|
||||
fd []byte
|
||||
ok bool
|
||||
)
|
||||
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) {
|
||||
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.RegisterService(&testSd, &testServer{})
|
||||
|
||||
for _, test := range []string{
|
||||
"EmptyCall",
|
||||
"grpc.EmptyService",
|
||||
"grpc.EmptyService.EmptyCall",
|
||||
"grpc.testing.EmptyService.EmptyCallWrong",
|
||||
"grpc.testing.EmptyService.EmptyStreamWrong",
|
||||
for _, test := range []struct {
|
||||
service string
|
||||
method string
|
||||
}{
|
||||
{"", "EmptyCall"},
|
||||
{"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 {
|
||||
t.Errorf("Metadata(%q)=%v, want <nil>", test, meta)
|
||||
t.Errorf("ServiceMetadata(%q, %q) = %v, want <nil>", test.service, test.method, meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче