Browse Source

Generate default response handlers (#2007)

Motivation:

The client stub for all RPCs accepts a response handler and can return
the result of that response. For unary and client-streaming RPCs, the
most common handler will just return the response message. This handler
should be generated as the default for those RPC types.

Modifications:

- Default the response handler parameter (`body`) to `{ try $0.message
}`.

Result:

As an example, part of the generated code for the Echo service which
looks like:

```swift
extension Echo_Echo.ClientProtocol {
    internal func get<R>(
        request: ClientRequest.Single<Echo_EchoRequest>,
        options: CallOptions = .defaults,
        _ body: @Sendable @escaping (ClientResponse.Single<Echo_EchoResponse>) async throws -> R
    ) async throws -> R where R: Sendable {
        try await self.get(...)
    }
```

Becomes:

```swift
extension Echo_Echo.ClientProtocol {
    internal func get<R>(
        request: ClientRequest.Single<Echo_EchoRequest>,
        options: CallOptions = .defaults,
        _ body: @Sendable @escaping (ClientResponse.Single<Echo_EchoResponse>) async throws -> R = {
	    try $0.message
        }
    ) async throws -> R where R: Sendable {
        try await self.get(...)
    }
```

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
Clinton Nkwocha 1 year ago
parent
commit
efb9e8f0e0
1 changed files with 12 additions and 4 deletions
  1. 12 4
      Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

+ 12 - 4
Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

@@ -105,7 +105,9 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
             internal func sayHello<R>(
                 request: ClientRequest.Single<Hello_World_HelloRequest>,
                 options: CallOptions = .defaults,
-                _ body: @Sendable @escaping (ClientResponse.Single<Hello_World_HelloReply>) async throws -> R
+                _ body: @Sendable @escaping (ClientResponse.Single<Hello_World_HelloReply>) async throws -> R = {
+                    try $0.message
+                }
             ) async throws -> R where R: Sendable {
                 try await self.sayHello(
                     request: request,
@@ -132,7 +134,9 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
                 serializer: some MessageSerializer<Hello_World_HelloRequest>,
                 deserializer: some MessageDeserializer<Hello_World_HelloReply>,
                 options: CallOptions = .defaults,
-                _ body: @Sendable @escaping (ClientResponse.Single<Hello_World_HelloReply>) async throws -> R
+                _ body: @Sendable @escaping (ClientResponse.Single<Hello_World_HelloReply>) async throws -> R = {
+                    try $0.message
+                }
             ) async throws -> R where R: Sendable {
                 try await self.client.unary(
                     request: request,
@@ -374,7 +378,9 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
           package func sayHello<R>(
             request: ClientRequest.Single<HelloRequest>,
             options: CallOptions = .defaults,
-            _ body: @Sendable @escaping (ClientResponse.Single<HelloReply>) async throws -> R
+            _ body: @Sendable @escaping (ClientResponse.Single<HelloReply>) async throws -> R = {
+              try $0.message
+            }
           ) async throws -> R where R: Sendable {
             try await self.sayHello(
               request: request,
@@ -401,7 +407,9 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
             serializer: some MessageSerializer<HelloRequest>,
             deserializer: some MessageDeserializer<HelloReply>,
             options: CallOptions = .defaults,
-            _ body: @Sendable @escaping (ClientResponse.Single<HelloReply>) async throws -> R
+            _ body: @Sendable @escaping (ClientResponse.Single<HelloReply>) async throws -> R = {
+              try $0.message
+            }
           ) async throws -> R where R: Sendable {
             try await self.client.unary(
               request: request,