瀏覽代碼

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 年之前
父節點
當前提交
3a4df8c59a
共有 1 個文件被更改,包括 12 次插入4 次删除
  1. 12 4
      Tests/GRPCHTTP2TransportTests/Generated/control.grpc.swift

+ 12 - 4
Tests/GRPCHTTP2TransportTests/Generated/control.grpc.swift

@@ -217,7 +217,9 @@ extension Control.ClientProtocol {
     internal func unary<R>(
         request: ClientRequest.Single<ControlInput>,
         options: CallOptions = .defaults,
-        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R
+        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R = {
+            try $0.message
+        }
     ) async throws -> R where R: Sendable {
         try await self.unary(
             request: request,
@@ -245,7 +247,9 @@ extension Control.ClientProtocol {
     internal func clientStream<R>(
         request: ClientRequest.Stream<ControlInput>,
         options: CallOptions = .defaults,
-        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R
+        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R = {
+            try $0.message
+        }
     ) async throws -> R where R: Sendable {
         try await self.clientStream(
             request: request,
@@ -288,7 +292,9 @@ internal struct ControlClient: Control.ClientProtocol {
         serializer: some MessageSerializer<ControlInput>,
         deserializer: some MessageDeserializer<ControlOutput>,
         options: CallOptions = .defaults,
-        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R
+        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R = {
+            try $0.message
+        }
     ) async throws -> R where R: Sendable {
         try await self.client.unary(
             request: request,
@@ -322,7 +328,9 @@ internal struct ControlClient: Control.ClientProtocol {
         serializer: some MessageSerializer<ControlInput>,
         deserializer: some MessageDeserializer<ControlOutput>,
         options: CallOptions = .defaults,
-        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R
+        _ body: @Sendable @escaping (ClientResponse.Single<ControlOutput>) async throws -> R = {
+            try $0.message
+        }
     ) async throws -> R where R: Sendable {
         try await self.client.clientStreaming(
             request: request,