Skip to content

Commit

Permalink
Generate client service name and proto service comments
Browse files Browse the repository at this point in the history
Motivation:

It can be useful to know the service path a client is for, particularly
when constructing calls manually.

Additionally, we include the methods comments for RPCs but not the
service comments, they may be helpful in some cases.

Modifications:

- Generate the 'serviceName' for the client
- Generate the proto source comments for the client and server

Result:

- Generated code is a touch more useful
- Resolves grpc#962, grpc#1036
  • Loading branch information
glbrntt committed Nov 30, 2020
1 parent 42edec5 commit 34b78ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Sources/protoc-gen-grpc-swift/Generator-Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,18 @@ extension Generator {
}

private func printServiceClientProtocol() {
let comments = self.service.protoSourceComments()
if !comments.isEmpty {
// Source comments already have the leading '///'
self.println(comments, newline: false)
self.println("///")
}
self.println(
"/// Usage: instantiate \(self.clientClassName), then call methods of this protocol to make API calls."
"/// Usage: instantiate `\(self.clientClassName)`, then call methods of this protocol to make API calls."
)
self.println("\(self.access) protocol \(self.clientProtocolName): GRPCClient {")
self.withIndentation {
self.println("var serviceName: String { get }")
self.println("var interceptors: \(self.clientInterceptorProtocolName)? { get }")

for method in service.methods {
Expand All @@ -100,8 +107,15 @@ extension Generator {
private func printClientProtocolExtension() {
self.println("extension \(self.clientProtocolName) {")

// Default method implementations.
self.withIndentation {
// Service name.
self.println("\(self.access) var serviceName: String {")
self.withIndentation {
self.println("return \"\(self.servicePath)\"")
}
self.println("}")

// Default method implementations.
self.printMethods()
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/protoc-gen-grpc-swift/Generator-Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ extension Generator {
}

private func printServerProtocol() {
let comments = self.service.protoSourceComments()
if !comments.isEmpty {
// Source comments already have the leading '///'
self.println(comments, newline: false)
self.println("///")
}
println("/// To build a server, implement a class that conforms to this protocol.")
println("\(access) protocol \(providerName): CallHandlerProvider {")
self.withIndentation {
Expand Down

0 comments on commit 34b78ff

Please sign in to comment.