Browse Source

Some more manual cleanup.

Daniel Alm 7 năm trước cách đây
mục cha
commit
1fc059367a

+ 33 - 63
Examples/Echo/Generated/echo.grpc.swift

@@ -26,13 +26,6 @@ import Dispatch
 import gRPC
 import SwiftProtobuf
 
-/// Type for errors thrown from generated client code.
-internal enum Echo_EchoClientError : Error {
-  case endOfStream
-  case invalidMessageReceived
-  case error(c: CallResult)
-}
-
 internal protocol Echo_EchoGetCall: ClientCallUnary { }
 
 fileprivate final class Echo_EchoGetCallImpl: ClientCallUnaryImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetCall {
@@ -116,26 +109,22 @@ internal protocol Echo_EchoService {
   /// Synchronous. Unary.
   func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
   /// Asynchronous. Unary.
-  func get(_ request: Echo_EchoRequest,
-                  completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall
+  func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall
 
   /// Asynchronous. Server-streaming.
   /// Send the initial message.
   /// Use methods on the returned object to get streamed responses.
-  func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
-    throws -> Echo_EchoExpandCall
+  func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall
 
   /// Asynchronous. Client-streaming.
   /// Use methods on the returned object to stream messages and
   /// to close the connection and wait for a final response.
-  func collect(completion: ((CallResult)->())?)
-    throws -> Echo_EchoCollectCall
+  func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall
 
   /// Asynchronous. Bidirectional-streaming.
   /// Use methods on the returned object to stream messages,
   /// to wait for replies, and to close the connection.
-  func update(completion: ((CallResult)->())?)
-    throws -> Echo_EchoUpdateCall
+  func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall
 
 }
 
@@ -145,21 +134,13 @@ internal final class Echo_EchoServiceClient: Echo_EchoService {
   internal var metadata : Metadata
 
   internal var host : String {
-    get {
-      return self.channel.host
-    }
-    set {
-      self.channel.host = newValue
-    }
+    get { return self.channel.host }
+    set { self.channel.host = newValue }
   }
 
   internal var timeout : TimeInterval {
-    get {
-      return self.channel.timeout
-    }
-    set {
-      self.channel.timeout = newValue
-    }
+    get { return self.channel.timeout }
+    set { self.channel.timeout = newValue }
   }
 
   /// Create a client.
@@ -177,46 +158,39 @@ internal final class Echo_EchoServiceClient: Echo_EchoService {
   }
 
   /// Synchronous. Unary.
-  internal func get(_ request: Echo_EchoRequest)
-    throws
-    -> Echo_EchoResponse {
-      return try Echo_EchoGetCallImpl(channel).run(request:request, metadata:metadata)
+  internal func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
+    return try Echo_EchoGetCallImpl(channel)
+      .run(request: request, metadata: metadata)
   }
   /// Asynchronous. Unary.
   internal func get(_ request: Echo_EchoRequest,
-                  completion: @escaping (Echo_EchoResponse?, CallResult)->())
-    throws
-    -> Echo_EchoGetCall {
-      return try Echo_EchoGetCallImpl(channel).start(request:request,
-                                                 metadata:metadata,
-                                                 completion:completion)
+                  completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
+    return try Echo_EchoGetCallImpl(channel)
+      .start(request: request, metadata: metadata, completion: completion)
   }
 
   /// Asynchronous. Server-streaming.
   /// Send the initial message.
   /// Use methods on the returned object to get streamed responses.
-  internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
-    throws
-    -> Echo_EchoExpandCall {
-      return try Echo_EchoExpandCallImpl(channel).start(request:request, metadata:metadata, completion:completion)
+  internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
+    return try Echo_EchoExpandCallImpl(channel)
+      .start(request:request, metadata:metadata, completion:completion)
   }
 
   /// Asynchronous. Client-streaming.
   /// Use methods on the returned object to stream messages and
   /// to close the connection and wait for a final response.
-  internal func collect(completion: ((CallResult)->())?)
-    throws
-    -> Echo_EchoCollectCall {
-      return try Echo_EchoCollectCallImpl(channel).start(metadata:metadata, completion:completion)
+  internal func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
+    return try Echo_EchoCollectCallImpl(channel)
+       .start(metadata: metadata, completion: completion)
   }
 
   /// Asynchronous. Bidirectional-streaming.
   /// Use methods on the returned object to stream messages,
   /// to wait for replies, and to close the connection.
-  internal func update(completion: ((CallResult)->())?)
-    throws
-    -> Echo_EchoUpdateCall {
-      return try Echo_EchoUpdateCallImpl(channel).start(metadata:metadata, completion:completion)
+  internal func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
+    return try Echo_EchoUpdateCallImpl(channel)
+      .start(metadata: metadata, completion: completion)
   }
 
 }
@@ -237,32 +211,28 @@ class Echo_EchoServiceTestStub: Echo_EchoService {
     defer { getResponses.removeFirst() }
     return getResponses.first!
   }
-  func get(_ request: Echo_EchoRequest,
-                  completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall {
+  func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
     fatalError("not implemented")
   }
 
   var expandRequests: [Echo_EchoRequest] = []
   var expandCalls: [Echo_EchoExpandCall] = []
-  func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
-    throws -> Echo_EchoExpandCall {
+  func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
       expandRequests.append(request)
-      defer { expandCalls.removeFirst() }
-      return expandCalls.first!
+    defer { expandCalls.removeFirst() }
+    return expandCalls.first!
   }
 
   var collectCalls: [Echo_EchoCollectCall] = []
-  func collect(completion: ((CallResult)->())?)
-    throws -> Echo_EchoCollectCall {
-      defer { collectCalls.removeFirst() }
-      return collectCalls.first!
+  func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
+    defer { collectCalls.removeFirst() }
+    return collectCalls.first!
   }
 
   var updateCalls: [Echo_EchoUpdateCall] = []
-  func update(completion: ((CallResult)->())?)
-    throws -> Echo_EchoUpdateCall {
-      defer { updateCalls.removeFirst() }
-      return updateCalls.first!
+  func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
+    defer { updateCalls.removeFirst() }
+    return updateCalls.first!
   }
 
 }

+ 2 - 2
Examples/Echo/PackageManager/Sources/main.swift

@@ -105,7 +105,7 @@ Group {
       do {
         let responseMessage = try expandCall.receive()
         print("Received: \(responseMessage.text)")
-      } catch Echo_EchoClientError.endOfStream {
+      } catch ClientError.endOfStream {
         print("expand closed")
         running = false
       }
@@ -144,7 +144,7 @@ Group {
         do {
           let responseMessage = try updateCall.receive()
           print("Received: \(responseMessage.text)")
-        } catch Echo_EchoClientError.endOfStream {
+        } catch ClientError.endOfStream {
           print("update closed")
           sem.signal()
           running = false

+ 33 - 63
Plugin/Templates/client.swift

@@ -1,12 +1,5 @@
 //-{% for service in file.services %}
 
-/// Type for errors thrown from generated client code.
-{{ access }} enum {{ .|clienterror:file,service }} : Error {
-  case endOfStream
-  case invalidMessageReceived
-  case error(c: CallResult)
-}
-
 //-{% for method in service.methods %}
 //-{% if method|methodIsUnary %}
 //-{% include "client-call-unary.swift" %}
@@ -42,29 +35,25 @@
   /// Synchronous. Unary.
   func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }}
   /// Asynchronous. Unary.
-  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
-                  completion: @escaping ({{ method|output }}?, CallResult)->()) throws -> {{ .|call:file,service,method }}
+  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }}
   //-{% endif %}
   //-{% if method|methodIsServerStreaming %}
   /// Asynchronous. Server-streaming.
   /// Send the initial message.
   /// Use methods on the returned object to get streamed responses.
-  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult)->())?)
-    throws -> {{ .|call:file,service,method }}
+  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
   //-{% endif %}
   //-{% if method|methodIsClientStreaming %}
   /// Asynchronous. Client-streaming.
   /// Use methods on the returned object to stream messages and
   /// to close the connection and wait for a final response.
-  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult)->())?)
-    throws -> {{ .|call:file,service,method }}
+  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
   //-{% endif %}
   //-{% if method|methodIsBidiStreaming %}
   /// Asynchronous. Bidirectional-streaming.
   /// Use methods on the returned object to stream messages,
   /// to wait for replies, and to close the connection.
-  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult)->())?)
-    throws -> {{ .|call:file,service,method }}
+  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }}
   //-{% endif %}
 
   //-{% endfor %}
@@ -76,21 +65,13 @@
   {{ access }} var metadata : Metadata
 
   {{ access }} var host : String {
-    get {
-      return self.channel.host
-    }
-    set {
-      self.channel.host = newValue
-    }
+    get { return self.channel.host }
+    set { self.channel.host = newValue }
   }
 
   {{ access }} var timeout : TimeInterval {
-    get {
-      return self.channel.timeout
-    }
-    set {
-      self.channel.timeout = newValue
-    }
+    get { return self.channel.timeout }
+    set { self.channel.timeout = newValue }
   }
 
   /// Create a client.
@@ -110,49 +91,42 @@
   //-{% for method in service.methods %}
   //-{% if method|methodIsUnary %}
   /// Synchronous. Unary.
-  {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }})
-    throws
-    -> {{ method|output }} {
-      return try {{ .|call:file,service,method }}Impl(channel).run(request:request, metadata:metadata)
+  {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}) throws -> {{ method|output }} {
+    return try {{ .|call:file,service,method }}Impl(channel)
+      .run(request: request, metadata: metadata)
   }
   /// Asynchronous. Unary.
   {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
-                  completion: @escaping ({{ method|output }}?, CallResult)->())
-    throws
-    -> {{ .|call:file,service,method }} {
-      return try {{ .|call:file,service,method }}Impl(channel).start(request:request,
-                                                 metadata:metadata,
-                                                 completion:completion)
+                  completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }} {
+    return try {{ .|call:file,service,method }}Impl(channel)
+      .start(request: request, metadata: metadata, completion: completion)
   }
   //-{% endif %}
   //-{% if method|methodIsServerStreaming %}
   /// Asynchronous. Server-streaming.
   /// Send the initial message.
   /// Use methods on the returned object to get streamed responses.
-  {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult)->())?)
-    throws
-    -> {{ .|call:file,service,method }} {
-      return try {{ .|call:file,service,method }}Impl(channel).start(request:request, metadata:metadata, completion:completion)
+  {{ access }} func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
+    return try {{ .|call:file,service,method }}Impl(channel)
+      .start(request:request, metadata:metadata, completion:completion)
   }
   //-{% endif %}
   //-{% if method|methodIsClientStreaming %}
   /// Asynchronous. Client-streaming.
   /// Use methods on the returned object to stream messages and
   /// to close the connection and wait for a final response.
-  {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult)->())?)
-    throws
-    -> {{ .|call:file,service,method }} {
-      return try {{ .|call:file,service,method }}Impl(channel).start(metadata:metadata, completion:completion)
+  {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
+    return try {{ .|call:file,service,method }}Impl(channel)
+       .start(metadata: metadata, completion: completion)
   }
   //-{% endif %}
   //-{% if method|methodIsBidiStreaming %}
   /// Asynchronous. Bidirectional-streaming.
   /// Use methods on the returned object to stream messages,
   /// to wait for replies, and to close the connection.
-  {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult)->())?)
-    throws
-    -> {{ .|call:file,service,method }} {
-      return try {{ .|call:file,service,method }}Impl(channel).start(metadata:metadata, completion:completion)
+  {{ access }} func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
+    return try {{ .|call:file,service,method }}Impl(channel)
+      .start(metadata: metadata, completion: completion)
   }
   //-{% endif %}
 
@@ -178,35 +152,31 @@ class {{ .|serviceclass:file,service }}TestStub: {{ .|serviceclass:file,service
     defer { {{ method|methodDescriptorName|lowercase }}Responses.removeFirst() }
     return {{ method|methodDescriptorName|lowercase }}Responses.first!
   }
-  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }},
-                  completion: @escaping ({{ method|output }}?, CallResult)->()) throws -> {{ .|call:file,service,method }} {
+  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: @escaping ({{ method|output }}?, CallResult) -> Void) throws -> {{ .|call:file,service,method }} {
     fatalError("not implemented")
   }
   //-{% endif %}
   //-{% if method|methodIsServerStreaming %}
   var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []
   var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
-  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult)->())?)
-    throws -> {{ .|call:file,service,method }} {
+  func {{ method|methodDescriptorName|lowercase }}(_ request: {{ method|input }}, completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
       {{ method|methodDescriptorName|lowercase }}Requests.append(request)
-      defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
-      return {{ method|methodDescriptorName|lowercase }}Calls.first!
+    defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
+    return {{ method|methodDescriptorName|lowercase }}Calls.first!
   }
   //-{% endif %}
   //-{% if method|methodIsClientStreaming %}
   var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
-  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult)->())?)
-    throws -> {{ .|call:file,service,method }} {
-      defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
-      return {{ method|methodDescriptorName|lowercase }}Calls.first!
+  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
+    defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
+    return {{ method|methodDescriptorName|lowercase }}Calls.first!
   }
   //-{% endif %}
   //-{% if method|methodIsBidiStreaming %}
   var {{ method|methodDescriptorName|lowercase }}Calls: [{{ .|call:file,service,method }}] = []
-  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult)->())?)
-    throws -> {{ .|call:file,service,method }} {
-      defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
-      return {{ method|methodDescriptorName|lowercase }}Calls.first!
+  func {{ method|methodDescriptorName|lowercase }}(completion: ((CallResult) -> Void)?) throws -> {{ .|call:file,service,method }} {
+    defer { {{ method|methodDescriptorName|lowercase }}Calls.removeFirst() }
+    return {{ method|methodDescriptorName|lowercase }}Calls.first!
   }
   //-{% endif %}
 

+ 1 - 0
Sources/gRPC/GenCodeSupport/ClientError.swift

@@ -16,6 +16,7 @@
 
 import Foundation
 
+/// Type for errors thrown from generated client code.
 public enum ClientError: Error {
   case endOfStream
   case invalidMessageReceived