Skip to content

Commit c59bcc3

Browse files
clintonpiglbrntt
andauthored
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 <[email protected]>
1 parent 2af8f48 commit c59bcc3

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

Sources/InteroperabilityTests/Generated/test.grpc.swift

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,9 @@ extension Grpc_Testing_TestService.ClientProtocol {
566566
public func emptyCall<R>(
567567
request: ClientRequest.Single<Grpc_Testing_Empty>,
568568
options: CallOptions = .defaults,
569-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
569+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
570+
try $0.message
571+
}
570572
) async throws -> R where R: Sendable {
571573
try await self.emptyCall(
572574
request: request,
@@ -580,7 +582,9 @@ extension Grpc_Testing_TestService.ClientProtocol {
580582
public func unaryCall<R>(
581583
request: ClientRequest.Single<Grpc_Testing_SimpleRequest>,
582584
options: CallOptions = .defaults,
583-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R
585+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R = {
586+
try $0.message
587+
}
584588
) async throws -> R where R: Sendable {
585589
try await self.unaryCall(
586590
request: request,
@@ -594,7 +598,9 @@ extension Grpc_Testing_TestService.ClientProtocol {
594598
public func cacheableUnaryCall<R>(
595599
request: ClientRequest.Single<Grpc_Testing_SimpleRequest>,
596600
options: CallOptions = .defaults,
597-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R
601+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R = {
602+
try $0.message
603+
}
598604
) async throws -> R where R: Sendable {
599605
try await self.cacheableUnaryCall(
600606
request: request,
@@ -622,7 +628,9 @@ extension Grpc_Testing_TestService.ClientProtocol {
622628
public func streamingInputCall<R>(
623629
request: ClientRequest.Stream<Grpc_Testing_StreamingInputCallRequest>,
624630
options: CallOptions = .defaults,
625-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_StreamingInputCallResponse>) async throws -> R
631+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_StreamingInputCallResponse>) async throws -> R = {
632+
try $0.message
633+
}
626634
) async throws -> R where R: Sendable {
627635
try await self.streamingInputCall(
628636
request: request,
@@ -664,7 +672,9 @@ extension Grpc_Testing_TestService.ClientProtocol {
664672
public func unimplementedCall<R>(
665673
request: ClientRequest.Single<Grpc_Testing_Empty>,
666674
options: CallOptions = .defaults,
667-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
675+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
676+
try $0.message
677+
}
668678
) async throws -> R where R: Sendable {
669679
try await self.unimplementedCall(
670680
request: request,
@@ -692,7 +702,9 @@ public struct Grpc_Testing_TestServiceClient: Grpc_Testing_TestService.ClientPro
692702
serializer: some MessageSerializer<Grpc_Testing_Empty>,
693703
deserializer: some MessageDeserializer<Grpc_Testing_Empty>,
694704
options: CallOptions = .defaults,
695-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
705+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
706+
try $0.message
707+
}
696708
) async throws -> R where R: Sendable {
697709
try await self.client.unary(
698710
request: request,
@@ -710,7 +722,9 @@ public struct Grpc_Testing_TestServiceClient: Grpc_Testing_TestService.ClientPro
710722
serializer: some MessageSerializer<Grpc_Testing_SimpleRequest>,
711723
deserializer: some MessageDeserializer<Grpc_Testing_SimpleResponse>,
712724
options: CallOptions = .defaults,
713-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R
725+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R = {
726+
try $0.message
727+
}
714728
) async throws -> R where R: Sendable {
715729
try await self.client.unary(
716730
request: request,
@@ -730,7 +744,9 @@ public struct Grpc_Testing_TestServiceClient: Grpc_Testing_TestService.ClientPro
730744
serializer: some MessageSerializer<Grpc_Testing_SimpleRequest>,
731745
deserializer: some MessageDeserializer<Grpc_Testing_SimpleResponse>,
732746
options: CallOptions = .defaults,
733-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R
747+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_SimpleResponse>) async throws -> R = {
748+
try $0.message
749+
}
734750
) async throws -> R where R: Sendable {
735751
try await self.client.unary(
736752
request: request,
@@ -768,7 +784,9 @@ public struct Grpc_Testing_TestServiceClient: Grpc_Testing_TestService.ClientPro
768784
serializer: some MessageSerializer<Grpc_Testing_StreamingInputCallRequest>,
769785
deserializer: some MessageDeserializer<Grpc_Testing_StreamingInputCallResponse>,
770786
options: CallOptions = .defaults,
771-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_StreamingInputCallResponse>) async throws -> R
787+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_StreamingInputCallResponse>) async throws -> R = {
788+
try $0.message
789+
}
772790
) async throws -> R where R: Sendable {
773791
try await self.client.clientStreaming(
774792
request: request,
@@ -828,7 +846,9 @@ public struct Grpc_Testing_TestServiceClient: Grpc_Testing_TestService.ClientPro
828846
serializer: some MessageSerializer<Grpc_Testing_Empty>,
829847
deserializer: some MessageDeserializer<Grpc_Testing_Empty>,
830848
options: CallOptions = .defaults,
831-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
849+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
850+
try $0.message
851+
}
832852
) async throws -> R where R: Sendable {
833853
try await self.client.unary(
834854
request: request,
@@ -860,7 +880,9 @@ extension Grpc_Testing_UnimplementedService.ClientProtocol {
860880
public func unimplementedCall<R>(
861881
request: ClientRequest.Single<Grpc_Testing_Empty>,
862882
options: CallOptions = .defaults,
863-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
883+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
884+
try $0.message
885+
}
864886
) async throws -> R where R: Sendable {
865887
try await self.unimplementedCall(
866888
request: request,
@@ -888,7 +910,9 @@ public struct Grpc_Testing_UnimplementedServiceClient: Grpc_Testing_Unimplemente
888910
serializer: some MessageSerializer<Grpc_Testing_Empty>,
889911
deserializer: some MessageDeserializer<Grpc_Testing_Empty>,
890912
options: CallOptions = .defaults,
891-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
913+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
914+
try $0.message
915+
}
892916
) async throws -> R where R: Sendable {
893917
try await self.client.unary(
894918
request: request,
@@ -926,7 +950,9 @@ extension Grpc_Testing_ReconnectService.ClientProtocol {
926950
public func start<R>(
927951
request: ClientRequest.Single<Grpc_Testing_ReconnectParams>,
928952
options: CallOptions = .defaults,
929-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
953+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
954+
try $0.message
955+
}
930956
) async throws -> R where R: Sendable {
931957
try await self.start(
932958
request: request,
@@ -940,7 +966,9 @@ extension Grpc_Testing_ReconnectService.ClientProtocol {
940966
public func stop<R>(
941967
request: ClientRequest.Single<Grpc_Testing_Empty>,
942968
options: CallOptions = .defaults,
943-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_ReconnectInfo>) async throws -> R
969+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_ReconnectInfo>) async throws -> R = {
970+
try $0.message
971+
}
944972
) async throws -> R where R: Sendable {
945973
try await self.stop(
946974
request: request,
@@ -966,7 +994,9 @@ public struct Grpc_Testing_ReconnectServiceClient: Grpc_Testing_ReconnectService
966994
serializer: some MessageSerializer<Grpc_Testing_ReconnectParams>,
967995
deserializer: some MessageDeserializer<Grpc_Testing_Empty>,
968996
options: CallOptions = .defaults,
969-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R
997+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_Empty>) async throws -> R = {
998+
try $0.message
999+
}
9701000
) async throws -> R where R: Sendable {
9711001
try await self.client.unary(
9721002
request: request,
@@ -983,7 +1013,9 @@ public struct Grpc_Testing_ReconnectServiceClient: Grpc_Testing_ReconnectService
9831013
serializer: some MessageSerializer<Grpc_Testing_Empty>,
9841014
deserializer: some MessageDeserializer<Grpc_Testing_ReconnectInfo>,
9851015
options: CallOptions = .defaults,
986-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_ReconnectInfo>) async throws -> R
1016+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Testing_ReconnectInfo>) async throws -> R = {
1017+
try $0.message
1018+
}
9871019
) async throws -> R where R: Sendable {
9881020
try await self.client.unary(
9891021
request: request,

Sources/Services/Health/Generated/health.grpc.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ extension Grpc_Health_V1_Health.ClientProtocol {
225225
package func check<R>(
226226
request: ClientRequest.Single<Grpc_Health_V1_HealthCheckRequest>,
227227
options: CallOptions = .defaults,
228-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Health_V1_HealthCheckResponse>) async throws -> R
228+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Health_V1_HealthCheckResponse>) async throws -> R = {
229+
try $0.message
230+
}
229231
) async throws -> R where R: Sendable {
230232
try await self.check(
231233
request: request,
@@ -276,7 +278,9 @@ package struct Grpc_Health_V1_HealthClient: Grpc_Health_V1_Health.ClientProtocol
276278
serializer: some MessageSerializer<Grpc_Health_V1_HealthCheckRequest>,
277279
deserializer: some MessageDeserializer<Grpc_Health_V1_HealthCheckResponse>,
278280
options: CallOptions = .defaults,
279-
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Health_V1_HealthCheckResponse>) async throws -> R
281+
_ body: @Sendable @escaping (ClientResponse.Single<Grpc_Health_V1_HealthCheckResponse>) async throws -> R = {
282+
try $0.message
283+
}
280284
) async throws -> R where R: Sendable {
281285
try await self.client.unary(
282286
request: request,

0 commit comments

Comments
 (0)