Skip to content

Commit

Permalink
add support for newer ServiceDesc var, generated by newer protoc-gen-…
Browse files Browse the repository at this point in the history
…go-grpc (#38)
  • Loading branch information
jhump authored Apr 16, 2021
1 parent 8e9b279 commit 70c8197
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 21 additions & 3 deletions plugins/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions plugins/names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ func TestGoNameOfMethod(t *testing.T) {
// TODO
}

func TestGoNameOfExportedServiceDesc(t *testing.T) {
// TODO
}

func TestGoNameOfServiceDesc(t *testing.T) {
// TODO
}
Expand Down

0 comments on commit 70c8197

Please sign in to comment.