Skip to content

Commit

Permalink
reflection: rename proto imports for disambiguation in import script (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars authored Jun 26, 2023
1 parent 0673105 commit 789cf4e
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 159 deletions.
104 changes: 52 additions & 52 deletions reflection/adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,84 @@
package reflection

import (
v1grpc "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1pb "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1alphagrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
v1alphapb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
v1reflectiongrpc "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1reflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1alphareflectiongrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
v1alphareflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)

// asV1Alpha returns an implementation of the v1alpha version of the reflection
// interface that delegates all calls to the given v1 version.
func asV1Alpha(svr v1grpc.ServerReflectionServer) v1alphagrpc.ServerReflectionServer {
func asV1Alpha(svr v1reflectiongrpc.ServerReflectionServer) v1alphareflectiongrpc.ServerReflectionServer {
return v1AlphaServerImpl{svr: svr}
}

type v1AlphaServerImpl struct {
svr v1grpc.ServerReflectionServer
svr v1reflectiongrpc.ServerReflectionServer
}

func (s v1AlphaServerImpl) ServerReflectionInfo(stream v1alphagrpc.ServerReflection_ServerReflectionInfoServer) error {
func (s v1AlphaServerImpl) ServerReflectionInfo(stream v1alphareflectiongrpc.ServerReflection_ServerReflectionInfoServer) error {
return s.svr.ServerReflectionInfo(v1AlphaServerStreamAdapter{stream})
}

type v1AlphaServerStreamAdapter struct {
v1alphagrpc.ServerReflection_ServerReflectionInfoServer
v1alphareflectiongrpc.ServerReflection_ServerReflectionInfoServer
}

func (s v1AlphaServerStreamAdapter) Send(response *v1pb.ServerReflectionResponse) error {
func (s v1AlphaServerStreamAdapter) Send(response *v1reflectionpb.ServerReflectionResponse) error {
return s.ServerReflection_ServerReflectionInfoServer.Send(v1ToV1AlphaResponse(response))
}

func (s v1AlphaServerStreamAdapter) Recv() (*v1pb.ServerReflectionRequest, error) {
func (s v1AlphaServerStreamAdapter) Recv() (*v1reflectionpb.ServerReflectionRequest, error) {
resp, err := s.ServerReflection_ServerReflectionInfoServer.Recv()
if err != nil {
return nil, err
}
return v1AlphaToV1Request(resp), nil
}

func v1ToV1AlphaResponse(v1 *v1pb.ServerReflectionResponse) *v1alphapb.ServerReflectionResponse {
var v1alpha v1alphapb.ServerReflectionResponse
func v1ToV1AlphaResponse(v1 *v1reflectionpb.ServerReflectionResponse) *v1alphareflectionpb.ServerReflectionResponse {
var v1alpha v1alphareflectionpb.ServerReflectionResponse
v1alpha.ValidHost = v1.ValidHost
if v1.OriginalRequest != nil {
v1alpha.OriginalRequest = v1ToV1AlphaRequest(v1.OriginalRequest)
}
switch mr := v1.MessageResponse.(type) {
case *v1pb.ServerReflectionResponse_FileDescriptorResponse:
case *v1reflectionpb.ServerReflectionResponse_FileDescriptorResponse:
if mr != nil {
v1alpha.MessageResponse = &v1alphapb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1alphapb.FileDescriptorResponse{
v1alpha.MessageResponse = &v1alphareflectionpb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1alphareflectionpb.FileDescriptorResponse{
FileDescriptorProto: mr.FileDescriptorResponse.GetFileDescriptorProto(),
},
}
}
case *v1pb.ServerReflectionResponse_AllExtensionNumbersResponse:
case *v1reflectionpb.ServerReflectionResponse_AllExtensionNumbersResponse:
if mr != nil {
v1alpha.MessageResponse = &v1alphapb.ServerReflectionResponse_AllExtensionNumbersResponse{
AllExtensionNumbersResponse: &v1alphapb.ExtensionNumberResponse{
v1alpha.MessageResponse = &v1alphareflectionpb.ServerReflectionResponse_AllExtensionNumbersResponse{
AllExtensionNumbersResponse: &v1alphareflectionpb.ExtensionNumberResponse{
BaseTypeName: mr.AllExtensionNumbersResponse.GetBaseTypeName(),
ExtensionNumber: mr.AllExtensionNumbersResponse.GetExtensionNumber(),
},
}
}
case *v1pb.ServerReflectionResponse_ListServicesResponse:
case *v1reflectionpb.ServerReflectionResponse_ListServicesResponse:
if mr != nil {
svcs := make([]*v1alphapb.ServiceResponse, len(mr.ListServicesResponse.GetService()))
svcs := make([]*v1alphareflectionpb.ServiceResponse, len(mr.ListServicesResponse.GetService()))
for i, svc := range mr.ListServicesResponse.GetService() {
svcs[i] = &v1alphapb.ServiceResponse{
svcs[i] = &v1alphareflectionpb.ServiceResponse{
Name: svc.GetName(),
}
}
v1alpha.MessageResponse = &v1alphapb.ServerReflectionResponse_ListServicesResponse{
ListServicesResponse: &v1alphapb.ListServiceResponse{
v1alpha.MessageResponse = &v1alphareflectionpb.ServerReflectionResponse_ListServicesResponse{
ListServicesResponse: &v1alphareflectionpb.ListServiceResponse{
Service: svcs,
},
}
}
case *v1pb.ServerReflectionResponse_ErrorResponse:
case *v1reflectionpb.ServerReflectionResponse_ErrorResponse:
if mr != nil {
v1alpha.MessageResponse = &v1alphapb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1alphapb.ErrorResponse{
v1alpha.MessageResponse = &v1alphareflectionpb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1alphareflectionpb.ErrorResponse{
ErrorCode: mr.ErrorResponse.GetErrorCode(),
ErrorMessage: mr.ErrorResponse.GetErrorMessage(),
},
Expand All @@ -108,33 +108,33 @@ func v1ToV1AlphaResponse(v1 *v1pb.ServerReflectionResponse) *v1alphapb.ServerRef
return &v1alpha
}

func v1AlphaToV1Request(v1alpha *v1alphapb.ServerReflectionRequest) *v1pb.ServerReflectionRequest {
var v1 v1pb.ServerReflectionRequest
func v1AlphaToV1Request(v1alpha *v1alphareflectionpb.ServerReflectionRequest) *v1reflectionpb.ServerReflectionRequest {
var v1 v1reflectionpb.ServerReflectionRequest
v1.Host = v1alpha.Host
switch mr := v1alpha.MessageRequest.(type) {
case *v1alphapb.ServerReflectionRequest_FileByFilename:
v1.MessageRequest = &v1pb.ServerReflectionRequest_FileByFilename{
case *v1alphareflectionpb.ServerReflectionRequest_FileByFilename:
v1.MessageRequest = &v1reflectionpb.ServerReflectionRequest_FileByFilename{
FileByFilename: mr.FileByFilename,
}
case *v1alphapb.ServerReflectionRequest_FileContainingSymbol:
v1.MessageRequest = &v1pb.ServerReflectionRequest_FileContainingSymbol{
case *v1alphareflectionpb.ServerReflectionRequest_FileContainingSymbol:
v1.MessageRequest = &v1reflectionpb.ServerReflectionRequest_FileContainingSymbol{
FileContainingSymbol: mr.FileContainingSymbol,
}
case *v1alphapb.ServerReflectionRequest_FileContainingExtension:
case *v1alphareflectionpb.ServerReflectionRequest_FileContainingExtension:
if mr.FileContainingExtension != nil {
v1.MessageRequest = &v1pb.ServerReflectionRequest_FileContainingExtension{
FileContainingExtension: &v1pb.ExtensionRequest{
v1.MessageRequest = &v1reflectionpb.ServerReflectionRequest_FileContainingExtension{
FileContainingExtension: &v1reflectionpb.ExtensionRequest{
ContainingType: mr.FileContainingExtension.GetContainingType(),
ExtensionNumber: mr.FileContainingExtension.GetExtensionNumber(),
},
}
}
case *v1alphapb.ServerReflectionRequest_AllExtensionNumbersOfType:
v1.MessageRequest = &v1pb.ServerReflectionRequest_AllExtensionNumbersOfType{
case *v1alphareflectionpb.ServerReflectionRequest_AllExtensionNumbersOfType:
v1.MessageRequest = &v1reflectionpb.ServerReflectionRequest_AllExtensionNumbersOfType{
AllExtensionNumbersOfType: mr.AllExtensionNumbersOfType,
}
case *v1alphapb.ServerReflectionRequest_ListServices:
v1.MessageRequest = &v1pb.ServerReflectionRequest_ListServices{
case *v1alphareflectionpb.ServerReflectionRequest_ListServices:
v1.MessageRequest = &v1reflectionpb.ServerReflectionRequest_ListServices{
ListServices: mr.ListServices,
}
default:
Expand All @@ -143,40 +143,40 @@ func v1AlphaToV1Request(v1alpha *v1alphapb.ServerReflectionRequest) *v1pb.Server
return &v1
}

func v1ToV1AlphaRequest(v1 *v1pb.ServerReflectionRequest) *v1alphapb.ServerReflectionRequest {
var v1alpha v1alphapb.ServerReflectionRequest
func v1ToV1AlphaRequest(v1 *v1reflectionpb.ServerReflectionRequest) *v1alphareflectionpb.ServerReflectionRequest {
var v1alpha v1alphareflectionpb.ServerReflectionRequest
v1alpha.Host = v1.Host
switch mr := v1.MessageRequest.(type) {
case *v1pb.ServerReflectionRequest_FileByFilename:
case *v1reflectionpb.ServerReflectionRequest_FileByFilename:
if mr != nil {
v1alpha.MessageRequest = &v1alphapb.ServerReflectionRequest_FileByFilename{
v1alpha.MessageRequest = &v1alphareflectionpb.ServerReflectionRequest_FileByFilename{
FileByFilename: mr.FileByFilename,
}
}
case *v1pb.ServerReflectionRequest_FileContainingSymbol:
case *v1reflectionpb.ServerReflectionRequest_FileContainingSymbol:
if mr != nil {
v1alpha.MessageRequest = &v1alphapb.ServerReflectionRequest_FileContainingSymbol{
v1alpha.MessageRequest = &v1alphareflectionpb.ServerReflectionRequest_FileContainingSymbol{
FileContainingSymbol: mr.FileContainingSymbol,
}
}
case *v1pb.ServerReflectionRequest_FileContainingExtension:
case *v1reflectionpb.ServerReflectionRequest_FileContainingExtension:
if mr != nil {
v1alpha.MessageRequest = &v1alphapb.ServerReflectionRequest_FileContainingExtension{
FileContainingExtension: &v1alphapb.ExtensionRequest{
v1alpha.MessageRequest = &v1alphareflectionpb.ServerReflectionRequest_FileContainingExtension{
FileContainingExtension: &v1alphareflectionpb.ExtensionRequest{
ContainingType: mr.FileContainingExtension.GetContainingType(),
ExtensionNumber: mr.FileContainingExtension.GetExtensionNumber(),
},
}
}
case *v1pb.ServerReflectionRequest_AllExtensionNumbersOfType:
case *v1reflectionpb.ServerReflectionRequest_AllExtensionNumbersOfType:
if mr != nil {
v1alpha.MessageRequest = &v1alphapb.ServerReflectionRequest_AllExtensionNumbersOfType{
v1alpha.MessageRequest = &v1alphareflectionpb.ServerReflectionRequest_AllExtensionNumbersOfType{
AllExtensionNumbersOfType: mr.AllExtensionNumbersOfType,
}
}
case *v1pb.ServerReflectionRequest_ListServices:
case *v1reflectionpb.ServerReflectionRequest_ListServices:
if mr != nil {
v1alpha.MessageRequest = &v1alphapb.ServerReflectionRequest_ListServices{
v1alpha.MessageRequest = &v1alphareflectionpb.ServerReflectionRequest_ListServices{
ListServices: mr.ListServices,
}
}
Expand Down
74 changes: 37 additions & 37 deletions reflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

v1grpc "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1pb "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1alphagrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
v1reflectiongrpc "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1reflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1"
v1alphareflectiongrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)

// GRPCServer is the interface provided by a gRPC server. It is implemented by
Expand All @@ -67,16 +67,16 @@ var _ GRPCServer = (*grpc.Server)(nil)
// Both the v1 and v1alpha versions are registered.
func Register(s GRPCServer) {
svr := NewServerV1(ServerOptions{Services: s})
v1alphagrpc.RegisterServerReflectionServer(s, asV1Alpha(svr))
v1grpc.RegisterServerReflectionServer(s, svr)
v1alphareflectiongrpc.RegisterServerReflectionServer(s, asV1Alpha(svr))
v1reflectiongrpc.RegisterServerReflectionServer(s, svr)
}

// RegisterV1 registers only the v1 version of the server reflection service
// on the given gRPC server. Many clients may only support v1alpha so most
// users should use Register instead, at least until clients have upgraded.
func RegisterV1(s GRPCServer) {
svr := NewServerV1(ServerOptions{Services: s})
v1grpc.RegisterServerReflectionServer(s, svr)
v1reflectiongrpc.RegisterServerReflectionServer(s, svr)
}

// ServiceInfoProvider is an interface used to retrieve metadata about the
Expand Down Expand Up @@ -139,7 +139,7 @@ type ServerOptions struct {
//
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func NewServer(opts ServerOptions) v1alphagrpc.ServerReflectionServer {
func NewServer(opts ServerOptions) v1alphareflectiongrpc.ServerReflectionServer {
return asV1Alpha(NewServerV1(opts))
}

Expand All @@ -151,7 +151,7 @@ func NewServer(opts ServerOptions) v1alphagrpc.ServerReflectionServer {
//
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func NewServerV1(opts ServerOptions) v1grpc.ServerReflectionServer {
func NewServerV1(opts ServerOptions) v1reflectiongrpc.ServerReflectionServer {
if opts.DescriptorResolver == nil {
opts.DescriptorResolver = protoregistry.GlobalFiles
}
Expand All @@ -166,7 +166,7 @@ func NewServerV1(opts ServerOptions) v1grpc.ServerReflectionServer {
}

type serverReflectionServer struct {
v1alphagrpc.UnimplementedServerReflectionServer
v1alphareflectiongrpc.UnimplementedServerReflectionServer
s ServiceInfoProvider
descResolver protodesc.Resolver
extResolver ExtensionResolver
Expand Down Expand Up @@ -240,11 +240,11 @@ func (s *serverReflectionServer) allExtensionNumbersForTypeName(name string) ([]
}

// listServices returns the names of services this server exposes.
func (s *serverReflectionServer) listServices() []*v1pb.ServiceResponse {
func (s *serverReflectionServer) listServices() []*v1reflectionpb.ServiceResponse {
serviceInfo := s.s.GetServiceInfo()
resp := make([]*v1pb.ServiceResponse, 0, len(serviceInfo))
resp := make([]*v1reflectionpb.ServiceResponse, 0, len(serviceInfo))
for svc := range serviceInfo {
resp = append(resp, &v1pb.ServiceResponse{Name: svc})
resp = append(resp, &v1reflectionpb.ServiceResponse{Name: svc})
}
sort.Slice(resp, func(i, j int) bool {
return resp[i].Name < resp[j].Name
Expand All @@ -253,7 +253,7 @@ func (s *serverReflectionServer) listServices() []*v1pb.ServiceResponse {
}

// ServerReflectionInfo is the reflection service handler.
func (s *serverReflectionServer) ServerReflectionInfo(stream v1grpc.ServerReflection_ServerReflectionInfoServer) error {
func (s *serverReflectionServer) ServerReflectionInfo(stream v1reflectiongrpc.ServerReflection_ServerReflectionInfoServer) error {
sentFileDescriptors := make(map[string]bool)
for {
in, err := stream.Recv()
Expand All @@ -264,79 +264,79 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream v1grpc.ServerReflec
return err
}

out := &v1pb.ServerReflectionResponse{
out := &v1reflectionpb.ServerReflectionResponse{
ValidHost: in.Host,
OriginalRequest: in,
}
switch req := in.MessageRequest.(type) {
case *v1pb.ServerReflectionRequest_FileByFilename:
case *v1reflectionpb.ServerReflectionRequest_FileByFilename:
var b [][]byte
fd, err := s.descResolver.FindFileByPath(req.FileByFilename)
if err == nil {
b, err = s.fileDescWithDependencies(fd, sentFileDescriptors)
}
if err != nil {
out.MessageResponse = &v1pb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1pb.ErrorResponse{
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1reflectionpb.ErrorResponse{
ErrorCode: int32(codes.NotFound),
ErrorMessage: err.Error(),
},
}
} else {
out.MessageResponse = &v1pb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1pb.FileDescriptorResponse{FileDescriptorProto: b},
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1reflectionpb.FileDescriptorResponse{FileDescriptorProto: b},
}
}
case *v1pb.ServerReflectionRequest_FileContainingSymbol:
case *v1reflectionpb.ServerReflectionRequest_FileContainingSymbol:
b, err := s.fileDescEncodingContainingSymbol(req.FileContainingSymbol, sentFileDescriptors)
if err != nil {
out.MessageResponse = &v1pb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1pb.ErrorResponse{
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1reflectionpb.ErrorResponse{
ErrorCode: int32(codes.NotFound),
ErrorMessage: err.Error(),
},
}
} else {
out.MessageResponse = &v1pb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1pb.FileDescriptorResponse{FileDescriptorProto: b},
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1reflectionpb.FileDescriptorResponse{FileDescriptorProto: b},
}
}
case *v1pb.ServerReflectionRequest_FileContainingExtension:
case *v1reflectionpb.ServerReflectionRequest_FileContainingExtension:
typeName := req.FileContainingExtension.ContainingType
extNum := req.FileContainingExtension.ExtensionNumber
b, err := s.fileDescEncodingContainingExtension(typeName, extNum, sentFileDescriptors)
if err != nil {
out.MessageResponse = &v1pb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1pb.ErrorResponse{
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1reflectionpb.ErrorResponse{
ErrorCode: int32(codes.NotFound),
ErrorMessage: err.Error(),
},
}
} else {
out.MessageResponse = &v1pb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1pb.FileDescriptorResponse{FileDescriptorProto: b},
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_FileDescriptorResponse{
FileDescriptorResponse: &v1reflectionpb.FileDescriptorResponse{FileDescriptorProto: b},
}
}
case *v1pb.ServerReflectionRequest_AllExtensionNumbersOfType:
case *v1reflectionpb.ServerReflectionRequest_AllExtensionNumbersOfType:
extNums, err := s.allExtensionNumbersForTypeName(req.AllExtensionNumbersOfType)
if err != nil {
out.MessageResponse = &v1pb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1pb.ErrorResponse{
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_ErrorResponse{
ErrorResponse: &v1reflectionpb.ErrorResponse{
ErrorCode: int32(codes.NotFound),
ErrorMessage: err.Error(),
},
}
} else {
out.MessageResponse = &v1pb.ServerReflectionResponse_AllExtensionNumbersResponse{
AllExtensionNumbersResponse: &v1pb.ExtensionNumberResponse{
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_AllExtensionNumbersResponse{
AllExtensionNumbersResponse: &v1reflectionpb.ExtensionNumberResponse{
BaseTypeName: req.AllExtensionNumbersOfType,
ExtensionNumber: extNums,
},
}
}
case *v1pb.ServerReflectionRequest_ListServices:
out.MessageResponse = &v1pb.ServerReflectionResponse_ListServicesResponse{
ListServicesResponse: &v1pb.ListServiceResponse{
case *v1reflectionpb.ServerReflectionRequest_ListServices:
out.MessageResponse = &v1reflectionpb.ServerReflectionResponse_ListServicesResponse{
ListServicesResponse: &v1reflectionpb.ListServiceResponse{
Service: s.listServices(),
},
}
Expand Down
Loading

0 comments on commit 789cf4e

Please sign in to comment.