Browse Source

rename "create[X]" methods to "make[X]" to follow Swift API guidelines

Tim Burks 9 years ago
parent
commit
9e7887504f

+ 3 - 3
Examples/Datastore/Datastore/DatastoreViewController.swift

@@ -92,9 +92,9 @@ class DatastoreViewController : NSViewController, NSTextFieldDelegate {
 
     let requestMetadata = Metadata(["authorization":authorization])
 
-    if let requestMessage = self.fileDescriptorSet.createMessage("RunQueryRequest") {
+    if let requestMessage = self.fileDescriptorSet.makeMessage("RunQueryRequest") {
       requestMessage.addField("project_id", value:"hello-86")
-      let gqlQuery = self.fileDescriptorSet.createMessage("GqlQuery")
+      let gqlQuery = self.fileDescriptorSet.makeMessage("GqlQuery")
 
       var query = self.messageField.stringValue
       if query == "" {
@@ -112,7 +112,7 @@ class DatastoreViewController : NSViewController, NSTextFieldDelegate {
       guard let client = client else {
         return
       }
-      call = client.createCall(host: requestHost,
+      call = client.makeCall(host: requestHost,
                                method: "/google.datastore.v1.Datastore/RunQuery",
                                timeout: 30.0)
       guard let call = call else {

+ 2 - 2
Examples/Echo/Swift/Echo/EchoServer.swift

@@ -70,7 +70,7 @@ class EchoServer {
             let requestMessage =
             fileDescriptorSet.readMessage("EchoRequest", data: requestData) {
             requestMessage.forOneField("text") {(field) in
-              let replyMessage = fileDescriptorSet.createMessage("EchoResponse")!
+              let replyMessage = fileDescriptorSet.makeMessage("EchoResponse")!
               replyMessage.addField("text", value:"Swift nonstreaming echo " + field.string())
               requestHandler.sendResponse(message:replyMessage.data(),
                                           trailingMetadata:Metadata())
@@ -109,7 +109,7 @@ class EchoServer {
         if let requestData = requestData,
           let requestMessage = fileDescriptorSet.readMessage("EchoRequest", data:requestData) {
           requestMessage.forOneField("text") {(field) in
-            let replyMessage = fileDescriptorSet.createMessage("EchoResponse")!
+            let replyMessage = fileDescriptorSet.makeMessage("EchoResponse")!
             replyMessage.addField("text", value:"Swift streaming echo " + field.string())
             requestHandler.sendResponse(message:replyMessage.data()) {
               // after we've sent our response, prepare to handle another message

+ 11 - 11
Examples/Echo/Swift/Echo/EchoViewController.swift

@@ -82,7 +82,7 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
     }
   }
 
-  func createClient(address: String, host: String) {
+  func prepareClient(address: String, host: String) {
     if (TLSButton.intValue == 0) {
       client = Client(address:address)
     } else {
@@ -99,16 +99,16 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
 
     if (self.streamingButton.intValue == 0) {
       // NONSTREAMING
-      if let requestMessage = self.fileDescriptorSet.createMessage("EchoRequest") {
+      if let requestMessage = self.fileDescriptorSet.makeMessage("EchoRequest") {
         requestMessage.addField("text", value:self.messageField.stringValue)
         let requestMessageData = requestMessage.data()
-        createClient(address:address, host:requestHost)
+        prepareClient(address:address, host:requestHost)
         guard let client = client else {
           return
         }
-        call = client.createCall(host: requestHost,
-                                 method: "/echo.Echo/Get",
-                                 timeout: 30.0)
+        call = client.makeCall(host: requestHost,
+                               method: "/echo.Echo/Get",
+                               timeout: 30.0)
         guard let call = call else {
           return
         }
@@ -135,13 +135,13 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
     else {
       // STREAMING
       if (!nowStreaming) {
-        createClient(address:address, host:requestHost)
+        prepareClient(address:address, host:requestHost)
         guard let client = client else {
           return
         }
-        call = client.createCall(host: requestHost,
-                                 method: "/echo.Echo/Update",
-                                 timeout: 30.0)
+        call = client.makeCall(host: requestHost,
+                               method: "/echo.Echo/Update",
+                               timeout: 30.0)
         guard let call = call else {
           return
         }
@@ -154,7 +154,7 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
   }
 
   func sendMessage() {
-    let requestMessage = self.fileDescriptorSet.createMessage("EchoRequest")!
+    let requestMessage = self.fileDescriptorSet.makeMessage("EchoRequest")!
     requestMessage.addField("text", value:self.messageField.stringValue)
     let messageData = requestMessage.data()
     if let call = call {

+ 1 - 1
Examples/Sessions/Sessions/Document.swift

@@ -165,7 +165,7 @@ class Document: NSDocument {
           break
         }
         let method = (i < steps) ? "/hello" : "/quit"
-        let call = self.client.createCall(host: host, method: method, timeout: 30)
+        let call = self.client.makeCall(host: host, method: method, timeout: 30)
 
         let metadata = Metadata([["x": "xylophone"],
                                  ["y": "yu"],

+ 2 - 2
Examples/StickyNotes/StickyNotes/StickyNoteViewController.swift

@@ -68,7 +68,7 @@ class StickyNoteViewController : NSViewController, NSTextFieldDelegate {
     let text = self.messageField.stringValue
 
     // build the message
-    if let requestMessage = fileDescriptorSet.createMessage("StickyNoteRequest") {
+    if let requestMessage = fileDescriptorSet.makeMessage("StickyNoteRequest") {
       requestMessage.addField("message", value:text)
 
       let requestHost = "foo.test.google.fr"
@@ -78,7 +78,7 @@ class StickyNoteViewController : NSViewController, NSTextFieldDelegate {
                                       ["z":"zither"]])
 
       client = Client(address:address)
-      let call = client.createCall(host: requestHost, method: requestMethod, timeout: 600)
+      let call = client.makeCall(host: requestHost, method: requestMethod, timeout: 600)
       _ = call.performNonStreamingCall(messageData: requestMessage.data(),
                                        metadata: requestMetadata,
                                        completion:

+ 1 - 1
Examples/StickyNotes/StickyNotes/StickyServer.swift

@@ -75,7 +75,7 @@ class StickyServer {
               requestMessage.forOneField("message") {(field) in
                 let imageData = self.drawImage(message: field.string())
 
-                let replyMessage = fileDescriptorSet.createMessage("StickyNoteResponse")!
+                let replyMessage = fileDescriptorSet.makeMessage("StickyNoteResponse")!
                 replyMessage.addField("image", value:imageData)
                 requestHandler.sendResponse(message:replyMessage.data(),
                                             trailingMetadata:Metadata())

+ 1 - 1
Packages/QuickProto/Sources/FileDescriptorSet.swift

@@ -77,7 +77,7 @@ public class FileDescriptorSet {
     return nil
   }
 
-  public func createMessage(_ name: String) -> Message! {
+  public func makeMessage(_ name: String) -> Message! {
     if let messageDescriptor = self.messageDescriptor(name: name) {
       return Message(descriptor:messageDescriptor, fields:[])
     } else {

+ 1 - 1
Packages/gRPC/Sources/Client.swift

@@ -82,7 +82,7 @@ public class Client {
   /// - Parameter method: the gRPC method name for the call
   /// - Parameter timeout: a timeout value in seconds
   /// - Returns: a Call object that can be used to perform the request
-  public func createCall(host:String, method:String, timeout:Double) -> Call {
+  public func makeCall(host:String, method:String, timeout:Double) -> Call {
     let call = cgrpc_client_create_call(underlyingClient, method, host, timeout)!
     return Call(call:call, owned:true, completionQueue:self.completionQueue)
   }

+ 12 - 17
Packages/gRPC/Sources/Handler.swift

@@ -46,6 +46,9 @@ public class Handler {
   /// Metadata received with the request
   public var requestMetadata: Metadata
 
+  /// A Call object that can be used to respond to the request
+  var call: Call!
+
   /// Initializes a Handler
   ///
   /// - Parameter h: the underlying C representation
@@ -81,16 +84,7 @@ public class Handler {
     return String(cString:cgrpc_handler_call_peer(underlyingHandler), encoding:String.Encoding.utf8)!;
   }
 
-  /// Creates a call object associated with the handler
-  ///
-  /// - Returns: a Call object that can be used to respond to the request
-  func call() -> Call {
-    return Call(call: cgrpc_handler_get_call(underlyingHandler),
-                owned: false,
-                completionQueue: self.completionQueue)
-  }
-
-  /// Request a call for the handler
+  /// Requests a call for the handler
   ///
   /// Fills the handler properties with information about the received request
   ///
@@ -100,12 +94,19 @@ public class Handler {
     return CallError.callError(grpcCallError: error)
   }
 
+  /// Prepares the handler's call object for response handling
+  ///
+  func prepareCall() -> Void {
+    self.call = Call(call: cgrpc_handler_get_call(underlyingHandler),
+                     owned: false,
+                     completionQueue: self.completionQueue)
+  }
+
   /// Receive the message sent with a call
   ///
   /// - Returns: a tuple containing status codes and a message (if available)
   public func receiveMessage(initialMetadata: Metadata,
                              completion:@escaping ((Data?) -> Void)) -> Void {
-    let call = self.call()
     let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:initialMetadata);
     let operation_receiveMessage = Operation_ReceiveMessage()
     let operations = OperationGroup(
@@ -130,7 +131,6 @@ public class Handler {
   /// - Returns: a tuple containing status codes
   public func sendResponse(message: Data,
                            trailingMetadata: Metadata) -> Void {
-    let call = self.call()
     let operation_receiveCloseOnServer = Operation_ReceiveCloseOnServer();
     let operation_sendStatusFromServer = Operation_SendStatusFromServer(status:0,
                                                                         statusDetails:"OK",
@@ -158,7 +158,6 @@ public class Handler {
   /// Send initial metadata in response to a connection
   public func sendMetadata(initialMetadata: Metadata,
                            completion:@escaping (() -> Void)) {
-    let call = self.call()
     let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:initialMetadata);
     let operations = OperationGroup(call:call, operations:[operation_sendInitialMetadata])
     {(success) in
@@ -176,7 +175,6 @@ public class Handler {
   ///
   /// - Returns: a tuple containing status codes and a message (if available)
   public func receiveMessage(completion:(@escaping (Data?) -> Void)) -> Void {
-    let call = self.call()
     let operation_receiveMessage = Operation_ReceiveMessage()
     let operations = OperationGroup(call:call, operations:[operation_receiveMessage])
     {(success) in
@@ -202,7 +200,6 @@ public class Handler {
   /// - Returns: a tuple containing status codes
   public func sendResponse(message: Data,
                            completion: @escaping () -> Void) -> Void {
-    let call = self.call()
     let operation_sendMessage = Operation_SendMessage(message:ByteBuffer(data:message))
     let operations = OperationGroup(call:call, operations:[operation_sendMessage])
     {(event) in
@@ -215,7 +212,6 @@ public class Handler {
 
   /// Recognize when the client has closed a request
   public func receiveClose(completion: @escaping () -> Void) -> Void {
-    let call = self.call()
     let operation_receiveClose = Operation_ReceiveCloseOnServer()
     let operations = OperationGroup(call:call, operations:[operation_receiveClose])
     {(event) in
@@ -230,7 +226,6 @@ public class Handler {
   /// Send final status to the client
   public func sendStatus(trailingMetadata: Metadata,
                          completion:@escaping (() -> Void)) -> Void {
-    let call = self.call()
     let operation_sendStatusFromServer = Operation_SendStatusFromServer(status:0,
                                                                         statusDetails:"OK",
                                                                         metadata:trailingMetadata)

+ 2 - 0
Packages/gRPC/Sources/Server.swift

@@ -87,6 +87,7 @@ public class Server {
         let event = self.completionQueue.waitForCompletion(timeout:600)
         if (event.type == .complete) {
           if event.tag == 101 {
+            handler.prepareCall()
             // run the handler and remove it when it finishes
             if event.success != 0 {
               self.handlers.add(handler)
@@ -133,6 +134,7 @@ public class Server {
         handler.completionQueue.run() {
           self.handlers.remove(handler)
         }
+        handler.prepareCall()
         self.handlers.add(handler)
         return (.ok, .complete, handler)
       } else {

+ 3 - 3
QuickProto/QuickProtoTests/QuickProtoTests.swift

@@ -76,7 +76,7 @@ class QuickProtoTests: XCTestCase {
       let sampleProtoData = try! Data(contentsOf: sampleProtoURL)
       let fileDescriptorSet = FileDescriptorSet(data:sampleProtoData)
       // construct an internal representation of a message
-      let message = fileDescriptorSet.createMessage("SampleMessage")
+      let message = fileDescriptorSet.makeMessage("SampleMessage")
       XCTAssert(message != nil)
       if let message = message {
         message.addField("d") {(field) in field.setDouble(-12.34)}
@@ -89,11 +89,11 @@ class QuickProtoTests: XCTestCase {
         message.addField("b") {(field) in field.setBool(true)}
         message.addField("text") {(field) in field.setString("hello, world")}
         message.addField("message") {(field) in
-          let innerMessage = fileDescriptorSet.createMessage("SampleMessage")!
+          let innerMessage = fileDescriptorSet.makeMessage("SampleMessage")!
           innerMessage.addField("text") {(field) in field.setString("inner message")}
           innerMessage.addField("i32") {(field) in field.setInt(54321)}
           innerMessage.addField("message") {(field) in
-            let innermostMessage = fileDescriptorSet.createMessage("SampleMessage")!
+            let innermostMessage = fileDescriptorSet.makeMessage("SampleMessage")!
             innermostMessage.addField("text") {(field) in field.setString("innermost message")}
             innermostMessage.addField("i32") {(field) in field.setInt(12345)}
             field.setMessage(innermostMessage)