فهرست منبع

Extract a bit more common code into `ServiceClient` protocols and classes.

Daniel Alm 7 سال پیش
والد
کامیت
e8ad2fff93
3فایلهای تغییر یافته به همراه103 افزوده شده و 104 حذف شده
  1. 3 52
      Examples/Echo/Generated/echo.grpc.swift
  2. 3 52
      Plugin/Templates/client.swift
  3. 97 0
      Sources/gRPC/GenCodeSupport/ServiceClient.swift

+ 3 - 52
Examples/Echo/Generated/echo.grpc.swift

@@ -92,20 +92,7 @@ class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo
 
 
 /// Instantiate Echo_EchoServiceImpl, then call methods of this protocol to make API calls.
-internal protocol Echo_EchoService {
-  var channel: Channel { get }
-
-  /// This metadata will be sent with all requests.
-  var metadata: Metadata { get }
-
-  /// This property allows the service host name to be overridden.
-  /// For example, it can be used to make calls to "localhost:8080"
-  /// appear to be to "example.com".
-  var host : String { get }
-
-  /// This property allows the service timeout to be overridden.
-  var timeout : TimeInterval { get }
-  
+internal protocol Echo_EchoService: ServiceClient {
   /// Synchronous. Unary.
   func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
   /// Asynchronous. Unary.
@@ -128,35 +115,7 @@ internal protocol Echo_EchoService {
 
 }
 
-internal final class Echo_EchoServiceClient: Echo_EchoService {
-  internal private(set) var channel: Channel
-
-  internal var metadata : Metadata
-
-  internal var host : String {
-    get { return self.channel.host }
-    set { self.channel.host = newValue }
-  }
-
-  internal var timeout : TimeInterval {
-    get { return self.channel.timeout }
-    set { self.channel.timeout = newValue }
-  }
-
-  /// Create a client.
-  internal init(address: String, secure: Bool = true) {
-    gRPC.initialize()
-    channel = Channel(address:address, secure:secure)
-    metadata = Metadata()
-  }
-
-  /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
-  internal init(address: String, certificates: String, host: String?) {
-    gRPC.initialize()
-    channel = Channel(address:address, certificates:certificates, host:host)
-    metadata = Metadata()
-  }
-
+internal final class Echo_EchoServiceClient: ServiceClientBase, Echo_EchoService {
   /// Synchronous. Unary.
   internal func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
     return try Echo_EchoGetCallImpl(channel)
@@ -195,15 +154,7 @@ internal final class Echo_EchoServiceClient: Echo_EchoService {
 
 }
 
-/// Simple fake implementation of Echo_EchoService that returns a previously-defined set of results
-/// and stores request values passed into it for later verification.
-/// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
-class Echo_EchoServiceTestStub: Echo_EchoService {
-  var channel: Channel { fatalError("not implemented") }
-  var metadata = Metadata()
-  var host = ""
-  var timeout: TimeInterval = 0
-  
+class Echo_EchoServiceTestStub: ServiceClientTestStubBase, Echo_EchoService {
   var getRequests: [Echo_EchoRequest] = []
   var getResponses: [Echo_EchoResponse] = []
   func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {

+ 3 - 52
Plugin/Templates/client.swift

@@ -16,20 +16,7 @@
 //-{% endfor %}
 
 /// Instantiate {{ .|serviceclass:file,service }}Impl, then call methods of this protocol to make API calls.
-{{ access }} protocol {{ .|serviceclass:file,service }} {
-  var channel: Channel { get }
-
-  /// This metadata will be sent with all requests.
-  var metadata: Metadata { get }
-
-  /// This property allows the service host name to be overridden.
-  /// For example, it can be used to make calls to "localhost:8080"
-  /// appear to be to "example.com".
-  var host : String { get }
-
-  /// This property allows the service timeout to be overridden.
-  var timeout : TimeInterval { get }
-  
+{{ access }} protocol {{ .|serviceclass:file,service }}: ServiceClient {
   //-{% for method in service.methods %}
   //-{% if method|methodIsUnary %}
   /// Synchronous. Unary.
@@ -59,35 +46,7 @@
   //-{% endfor %}
 }
 
-{{ access }} final class {{ .|serviceclass:file,service }}Client: {{ .|serviceclass:file,service }} {
-  {{ access }} private(set) var channel: Channel
-
-  {{ access }} var metadata : Metadata
-
-  {{ access }} var host : String {
-    get { return self.channel.host }
-    set { self.channel.host = newValue }
-  }
-
-  {{ access }} var timeout : TimeInterval {
-    get { return self.channel.timeout }
-    set { self.channel.timeout = newValue }
-  }
-
-  /// Create a client.
-  {{ access }} init(address: String, secure: Bool = true) {
-    gRPC.initialize()
-    channel = Channel(address:address, secure:secure)
-    metadata = Metadata()
-  }
-
-  /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
-  {{ access }} init(address: String, certificates: String, host: String?) {
-    gRPC.initialize()
-    channel = Channel(address:address, certificates:certificates, host:host)
-    metadata = Metadata()
-  }
-
+{{ access }} final class {{ .|serviceclass:file,service }}Client: ServiceClientBase, {{ .|serviceclass:file,service }} {
   //-{% for method in service.methods %}
   //-{% if method|methodIsUnary %}
   /// Synchronous. Unary.
@@ -134,15 +93,7 @@
 }
 
 //-{% if generateTestStubs %}
-/// Simple fake implementation of {{ .|serviceclass:file,service }} that returns a previously-defined set of results
-/// and stores request values passed into it for later verification.
-/// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
-class {{ .|serviceclass:file,service }}TestStub: {{ .|serviceclass:file,service }} {
-  var channel: Channel { fatalError("not implemented") }
-  var metadata = Metadata()
-  var host = ""
-  var timeout: TimeInterval = 0
-  
+class {{ .|serviceclass:file,service }}TestStub: ServiceClientTestStubBase, {{ .|serviceclass:file,service }} {
   //-{% for method in service.methods %}
   //-{% if method|methodIsUnary %}
   var {{ method|methodDescriptorName|lowercase }}Requests: [{{ method|input }}] = []

+ 97 - 0
Sources/gRPC/GenCodeSupport/ServiceClient.swift

@@ -0,0 +1,97 @@
+/*
+ * Copyright 2018, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import Dispatch
+import Foundation
+import SwiftProtobuf
+
+public protocol ServiceClient {
+  var channel: Channel { get }
+  
+  /// This metadata will be sent with all requests.
+  var metadata: Metadata { get }
+  
+  /// This property allows the service host name to be overridden.
+  /// For example, it can be used to make calls to "localhost:8080"
+  /// appear to be to "example.com".
+  var host : String { get }
+  
+  /// This property allows the service timeout to be overridden.
+  var timeout : TimeInterval { get }
+}
+
+open class ServiceClientBase: ServiceClient {
+  open private(set) var channel: Channel
+  
+  open var metadata : Metadata
+  
+  open var host : String {
+    get { return self.channel.host }
+    set { self.channel.host = newValue }
+  }
+  
+  open var timeout : TimeInterval {
+    get { return self.channel.timeout }
+    set { self.channel.timeout = newValue }
+  }
+  
+  /// Create a client.
+  public init(address: String, secure: Bool = true) {
+    gRPC.initialize()
+    channel = Channel(address: address, secure: secure)
+    metadata = Metadata()
+  }
+  
+  /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
+  public init(address: String, certificates: String, host: String?) {
+    gRPC.initialize()
+    channel = Channel(address: address, certificates: certificates, host: host)
+    metadata = Metadata()
+  }
+}
+
+/// Simple fake implementation of ServiceClient that returns a previously-defined set of results
+/// and stores request values passed into it for later verification.
+/// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
+open class ServiceClientTestStubBase: ServiceClient {
+  open private(set) var channel: Channel
+  
+  open var metadata : Metadata
+  
+  open var host : String {
+    get { return self.channel.host }
+    set { self.channel.host = newValue }
+  }
+  
+  open var timeout : TimeInterval {
+    get { return self.channel.timeout }
+    set { self.channel.timeout = newValue }
+  }
+  
+  /// Create a client.
+  public init(address: String, secure: Bool = true) {
+    gRPC.initialize()
+    channel = Channel(address: address, secure: secure)
+    metadata = Metadata()
+  }
+  
+  /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
+  public init(address: String, certificates: String, host: String?) {
+    gRPC.initialize()
+    channel = Channel(address: address, certificates: certificates, host: host)
+    metadata = Metadata()
+  }
+}