From 70c8197ef4ea66d11022326b63050f6fa10f6b29 Mon Sep 17 00:00:00 2001 From: Joshua Humphries Date: Fri, 16 Apr 2021 11:06:58 -0400 Subject: [PATCH] add support for newer ServiceDesc var, generated by newer protoc-gen-go-grpc (#38) --- plugins/names.go | 24 +++++++++++++++++++++--- plugins/names_test.go | 4 ++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/plugins/names.go b/plugins/names.go index 12db894..2c2085d 100644 --- a/plugins/names.go +++ b/plugins/names.go @@ -83,8 +83,10 @@ const ( nameKeyOneofInterface // This is for the unexported implementation of a service stub interface. nameKeyServiceImplClient - // This is for the unexported grpc.ServiceDesc var. + // This is for the unexported/legacy grpc.ServiceDesc var. nameKeyServiceDesc + // This is for the exported/newer grpc.ServiceDesc var. + nameKeyExportedServiceDesc // This is for the unexported implementation of a client-side stream. nameKeyMethodStreamImplClient // This is for the unexported implementation of a server-side stream. @@ -426,8 +428,23 @@ func (n *GoNames) GoTypeForServiceClientImpl(sd *desc.ServiceDescriptor) string }) } -// GoNameOfServiceDesc returns the unexported name of the var that holds the -// grpc.ServiceDesc that describes the given service. +// GoNameOfExportedServiceDesc returns the newer exported name of the var that +// holds the grpc.ServiceDesc that describes the given service. As of v1.0 of +// protoc-gen-go-grpc, this var is exported. +// +// If generating code that needs to reference the older, unexported var (from +// earlier versions of protoc plugins for Go), use GoNameOfServiceDesc instead. +func (n *GoNames) GoNameOfExportedServiceDesc(sd *desc.ServiceDescriptor) gopoet.Symbol { + name := n.getOrComputeName(nameKey{d: sd, k: nameKeyExportedServiceDesc}, func() { + n.computeService(sd) + }) + return n.GoPackageForFile(sd.GetFile()).Symbol(name) +} + +// GoNameOfServiceDesc returns the legacy unexported name of the var that +// holds the grpc.ServiceDesc that describes the given service. Prior to v1.0 +// and of protoc-gen-go-grpc, for generating Go code related to gRPC, these +// variables were unexported. // // This does not return a gopoet.Symbol because the var is not usable outside // of the generated package due to its being unexported. So only the symbol's @@ -620,6 +637,7 @@ func (n *GoNames) computeService(sd *desc.ServiceDescriptor) { n.descTypes[typeKey{d: sd, k: typeKeyServer}] = gopoet.NamedType(pkg.Symbol(exportedSvr + "Server")) n.descNames[nameKey{d: sd, k: nameKeyServiceImplClient}] = unexportedSvr + "Client" n.descNames[nameKey{d: sd, k: nameKeyServiceDesc}] = "_" + exportedSvr + "_serviceDesc" + n.descNames[nameKey{d: sd, k: nameKeyExportedServiceDesc}] = exportedSvr + "_ServiceDesc" for _, mtd := range sd.GetMethods() { mtdName := CamelCase(mtd.GetName()) diff --git a/plugins/names_test.go b/plugins/names_test.go index 9d39d3d..14e544f 100644 --- a/plugins/names_test.go +++ b/plugins/names_test.go @@ -285,6 +285,10 @@ func TestGoNameOfMethod(t *testing.T) { // TODO } +func TestGoNameOfExportedServiceDesc(t *testing.T) { + // TODO +} + func TestGoNameOfServiceDesc(t *testing.T) { // TODO }