浏览代码

Refactor some of the client code generation (#856)

Motivation:

In the future it might be useful to generate some of the client code in
multiple places.

Modifications:

- Split up the client code gen to print such that there are separate
  methods to print RPC methods
- The style of the generated code changed slightly too, to match the
  rest of the style
- Regenerate code

Result:

- We can avoid repeating ourselve in the future
- No functional change
George Barnett 5 年之前
父节点
当前提交
6a7bfb6881
共有 2 个文件被更改,包括 131 次插入63 次删除
  1. 125 63
      Sources/protoc-gen-grpc-swift/Generator-Client.swift
  2. 6 0
      Sources/protoc-gen-grpc-swift/Generator.swift

+ 125 - 63
Sources/protoc-gen-grpc-swift/Generator-Client.swift

@@ -66,80 +66,136 @@ extension Generator {
     println("self.defaultCallOptions = defaultCallOptions")
     outdent()
     println("}")
-    println()
 
-    for method in service.methods {
+    self.printMethods()
+
+    outdent()
+    println("}")
+  }
+
+  private func printMethods(callFactory: String = "self") {
+    for method in self.service.methods {
+      self.println()
+
       self.method = method
-      let streamType = streamingType(self.method)
-      switch streamType {
+      switch self.streamType {
       case .unary:
-        println(self.method.documentation(streamingType: streamType), newline: false)
-        println("///")
-        printParameters()
-        printRequestParameter()
-        printCallOptionsParameter()
-        println("/// - Returns: A `UnaryCall` with futures for the metadata, status and response.")
-        println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil) -> UnaryCall<\(methodInputName), \(methodOutputName)> {")
-        indent()
-        println("return self.makeUnaryCall(path: \"/\(servicePath)/\(method.name)\",")
-        println("                          request: request,")
-        println("                          callOptions: callOptions ?? self.defaultCallOptions)")
-        outdent()
-        println("}")
+        self.printUnaryCall(callFactory: callFactory)
 
       case .serverStreaming:
-        println(self.method.documentation(streamingType: streamType), newline: false)
-        println("///")
-        printParameters()
-        printRequestParameter()
-        printCallOptionsParameter()
-        printHandlerParameter()
-        println("/// - Returns: A `ServerStreamingCall` with futures for the metadata and status.")
-        println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingCall<\(methodInputName), \(methodOutputName)> {")
-        indent()
-        println("return self.makeServerStreamingCall(path: \"/\(servicePath)/\(method.name)\",")
-        println("                                    request: request,")
-        println("                                    callOptions: callOptions ?? self.defaultCallOptions,")
-        println("                                    handler: handler)")
-        outdent()
-        println("}")
+        self.printServerStreamingCall(callFactory: callFactory)
 
       case .clientStreaming:
-        println(self.method.documentation(streamingType: streamType), newline: false)
-        println("///")
-        printClientStreamingDetails()
-        println("///")
-        printParameters()
-        printCallOptionsParameter()
-        println("/// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.")
-        println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil) -> ClientStreamingCall<\(methodInputName), \(methodOutputName)> {")
-        indent()
-        println("return self.makeClientStreamingCall(path: \"/\(servicePath)/\(method.name)\",")
-        println("                                    callOptions: callOptions ?? self.defaultCallOptions)")
-        outdent()
-        println("}")
+        self.printClientStreamingCall(callFactory: callFactory)
 
       case .bidirectionalStreaming:
-        println(self.method.documentation(streamingType: streamType), newline: false)
-        println("///")
-        printClientStreamingDetails()
-        println("///")
-        printParameters()
-        printCallOptionsParameter()
-        printHandlerParameter()
-        println("/// - Returns: A `ClientStreamingCall` with futures for the metadata and status.")
-        println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingCall<\(methodInputName), \(methodOutputName)> {")
-        indent()
-        println("return self.makeBidirectionalStreamingCall(path: \"/\(servicePath)/\(method.name)\",")
-        println("                                           callOptions: callOptions ?? self.defaultCallOptions,")
-        println("                                           handler: handler)")
-        outdent()
-        println("}")
+        self.printBidirectionalStreamingCall(callFactory: callFactory)
       }
-      println()
     }
-    outdent()
-    println("}")
+  }
+
+  private func printUnaryCall(callFactory: String) {
+    self.println(self.method.documentation(streamingType: self.streamType), newline: false)
+    self.println("///")
+    self.printParameters()
+    self.printRequestParameter()
+    self.printCallOptionsParameter()
+    self.println("/// - Returns: A `UnaryCall` with futures for the metadata, status and response.")
+    self.println("\(self.access) func \(self.methodFunctionName)(")
+    self.withIndentation {
+      self.println("_ request: \(self.methodInputName),")
+      self.println("callOptions: CallOptions? = nil")
+    }
+    self.println(") -> UnaryCall<\(self.methodInputName), \(self.methodOutputName)> {")
+    self.withIndentation {
+      self.println("return \(callFactory).makeUnaryCall(")
+      self.withIndentation {
+        self.println("path: \(self.methodPath),")
+        self.println("request: request,")
+        self.println("callOptions: callOptions ?? self.defaultCallOptions")
+      }
+      self.println(")")
+    }
+    self.println("}")
+  }
+
+  private func printServerStreamingCall(callFactory: String) {
+    self.println(self.method.documentation(streamingType: self.streamType), newline: false)
+    self.println("///")
+    self.printParameters()
+    self.printRequestParameter()
+    self.printCallOptionsParameter()
+    self.printHandlerParameter()
+    self.println("/// - Returns: A `ServerStreamingCall` with futures for the metadata and status.")
+    self.println("\(self.access) func \(self.methodFunctionName)(")
+    self.withIndentation {
+      self.println("_ request: \(self.methodInputName),")
+      self.println("callOptions: CallOptions? = nil,")
+      self.println("handler: @escaping (\(methodOutputName)) -> Void")
+    }
+    self.println(") -> ServerStreamingCall<\(methodInputName), \(methodOutputName)> {")
+    self.withIndentation {
+      self.println("return \(callFactory).makeServerStreamingCall(") // path: \"/\(servicePath)/\(method.name)\",")
+      self.withIndentation {
+        self.println("path: \(self.methodPath),")
+        self.println("request: request,")
+        self.println("callOptions: callOptions ?? self.defaultCallOptions,")
+        self.println("handler: handler")
+      }
+      self.println(")")
+    }
+    self.println("}")
+  }
+
+  private func printClientStreamingCall(callFactory: String) {
+    self.println(self.method.documentation(streamingType: self.streamType), newline: false)
+    self.println("///")
+    self.printClientStreamingDetails()
+    self.println("///")
+    self.printParameters()
+    self.printCallOptionsParameter()
+    self.println("/// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.")
+    self.println("\(self.access) func \(self.methodFunctionName)(")
+    self.withIndentation {
+      self.println("callOptions: CallOptions? = nil")
+    }
+    self.println(") -> ClientStreamingCall<\(self.methodInputName), \(self.methodOutputName)> {")
+    self.withIndentation {
+      self.println("return \(callFactory).makeClientStreamingCall(")
+      self.withIndentation {
+        self.println("path: \(self.methodPath),")
+        self.println("callOptions: callOptions ?? self.defaultCallOptions")
+      }
+      self.println(")")
+    }
+    self.println("}")
+  }
+
+  private func printBidirectionalStreamingCall(callFactory: String) {
+    self.println(self.method.documentation(streamingType: self.streamType), newline: false)
+    self.println("///")
+    self.printClientStreamingDetails()
+    self.println("///")
+    self.printParameters()
+    self.printCallOptionsParameter()
+    self.printHandlerParameter()
+    self.println("/// - Returns: A `ClientStreamingCall` with futures for the metadata and status.")
+    self.println("\(self.access) func \(self.methodFunctionName)(")
+    self.withIndentation {
+      self.println("callOptions: CallOptions? = nil,")
+      self.println("handler: @escaping (\(self.methodOutputName)) -> Void")
+    }
+    self.println(") -> BidirectionalStreamingCall<\(self.methodInputName), \(self.methodOutputName)> {")
+    self.withIndentation {
+      self.println("return \(callFactory).makeBidirectionalStreamingCall(")
+      self.withIndentation {
+        self.println("path: \(self.methodPath),")
+        self.println("callOptions: callOptions ?? self.defaultCallOptions,")
+        self.println("handler: handler")
+      }
+      self.println(")")
+    }
+    self.println("}")
   }
 
   private func printClientStreamingDetails() {
@@ -164,6 +220,12 @@ extension Generator {
   }
 }
 
+fileprivate extension Generator {
+  var streamType: StreamingType {
+    return streamingType(self.method)
+  }
+}
+
 fileprivate extension StreamingType {
   var name: String {
     switch self {

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

@@ -54,6 +54,12 @@ class Generator {
     printer.outdent()
   }
 
+  internal func withIndentation(body: () -> ()) {
+    self.indent()
+    body()
+    self.outdent()
+  }
+
   private func printMain() {
     printer.print("""
       //