소스 검색

Generate client service name and proto service comments (#1059)

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 #962, #1036
George Barnett 5 년 전
부모
커밋
5771146a56
2개의 변경된 파일22개의 추가작업 그리고 2개의 파일을 삭제
  1. 16 2
      Sources/protoc-gen-grpc-swift/Generator-Client.swift
  2. 6 0
      Sources/protoc-gen-grpc-swift/Generator-Server.swift

+ 16 - 2
Sources/protoc-gen-grpc-swift/Generator-Client.swift

@@ -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 {
@@ -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()
     }
 

+ 6 - 0
Sources/protoc-gen-grpc-swift/Generator-Server.swift

@@ -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 {