Skip to content

Commit 632b9e5

Browse files
committed
Improve nanming of request/response types
Motivation: The names of the request/response types are quite stilted. We can improve them by removing the namespacing and by removing the explicit "Single". Modifications: - `ServerRequest.Single` -> `ServerRequest` - `ServerRequest.Stream` -> `StreamingServerRequest` - `ServerResponse.Single` -> `ServerResponse` - `ServerResponse.Stream` -> `StreamingServerResponse` - `ClientRequest.Single` -> `ClientRequest` - `ClientRequest.Stream` -> `StreamingClientRequest` - `ClientResponse.Single` -> `ClientResponse` - `ClientResponse.Stream` -> `StreamingClientResponse` Result: Better naming
1 parent cfa6743 commit 632b9e5

File tree

47 files changed

+973
-1001
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+973
-1001
lines changed

Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@
2525
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2626
/// public protocol Foo_BarClientProtocol: Sendable {
2727
/// func baz<R>(
28-
/// request: GRPCCore.ClientRequest.Single<Foo_Bar_Input>,
28+
/// request: GRPCCore.ClientRequest<Foo_Bar_Input>,
2929
/// serializer: some GRPCCore.MessageSerializer<Foo_Bar_Input>,
3030
/// deserializer: some GRPCCore.MessageDeserializer<Foo_Bar_Output>,
3131
/// options: GRPCCore.CallOptions = .defaults,
32-
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<Foo_Bar_Output>) async throws -> R
32+
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse<Foo_Bar_Output>) async throws -> R
3333
/// ) async throws -> R where R: Sendable
3434
/// }
3535
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
3636
/// extension Foo_Bar.ClientProtocol {
3737
/// public func baz<R>(
38-
/// request: GRPCCore.ClientRequest.Single<Foo_Bar_Input>,
38+
/// request: GRPCCore.ClientRequest<Foo_Bar_Input>,
3939
/// options: GRPCCore.CallOptions = .defaults,
40-
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<Foo_Bar_Output>) async throws -> R = {
40+
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse<Foo_Bar_Output>) async throws -> R = {
4141
/// try $0.message
4242
/// }
4343
/// ) async throws -> R where R: Sendable {
@@ -56,11 +56,11 @@
5656
/// self.client = client
5757
/// }
5858
/// public func methodA<R>(
59-
/// request: GRPCCore.ClientRequest.Stream<Foo_Bar_Input>,
59+
/// request: GRPCCore.StreamingClientRequest<Foo_Bar_Input>,
6060
/// serializer: some GRPCCore.MessageSerializer<Foo_Bar_Input>,
6161
/// deserializer: some GRPCCore.MessageDeserializer<Foo_Bar_Output>,
6262
/// options: GRPCCore.CallOptions = .defaults,
63-
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<Foo_Bar_Output>) async throws -> R = {
63+
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse<Foo_Bar_Output>) async throws -> R = {
6464
/// try $0.message
6565
/// }
6666
/// ) async throws -> R where R: Sendable {
@@ -299,12 +299,12 @@ extension ClientCodeTranslator {
299299
) -> [CodeBlock] {
300300
// Produces the following:
301301
//
302-
// let request = GRPCCore.ClientRequest.Single<Input>(message: message, metadata: metadata)
302+
// let request = GRPCCore.ClientRequest<Input>(message: message, metadata: metadata)
303303
// return try await method(request: request, options: options, responseHandler: responseHandler)
304304
//
305305
// or:
306306
//
307-
// let request = GRPCCore.ClientRequest.Stream<Input>(metadata: metadata, producer: writer)
307+
// let request = GRPCCore.StreamingClientRequest<Input>(metadata: metadata, producer: writer)
308308
// return try await method(request: request, options: options, responseHandler: responseHandler)
309309

310310
// First, make the init for the ClientRequest

Sources/GRPCCodeGen/Internal/Translator/ServerCodeTranslator.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2626
/// public protocol Foo_BarStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
2727
/// func baz(
28-
/// request: GRPCCore.ServerRequest.Stream<Foo_Bar_Input>
29-
/// ) async throws -> GRPCCore.ServerResponse.Stream<Foo_Bar_Output>
28+
/// request: GRPCCore.StreamingServerRequest<Foo_Bar_Input>
29+
/// ) async throws -> GRPCCore.StreamingServerResponse<Foo_Bar_Output>
3030
/// }
3131
/// // Conformance to `GRPCCore.RegistrableRPCService`.
3232
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
@@ -43,17 +43,17 @@
4343
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4444
/// public protocol Foo_BarServiceProtocol: Foo_Bar.StreamingServiceProtocol {
4545
/// func baz(
46-
/// request: GRPCCore.ServerRequest.Single<Foo_Bar_Input>
47-
/// ) async throws -> GRPCCore.ServerResponse.Single<Foo_Bar_Output>
46+
/// request: GRPCCore.ServerRequest<Foo_Bar_Input>
47+
/// ) async throws -> GRPCCore.ServerResponse<Foo_Bar_Output>
4848
/// }
4949
/// // Partial conformance to `Foo_BarStreamingServiceProtocol`.
5050
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
5151
/// extension Foo_Bar.ServiceProtocol {
5252
/// public func baz(
53-
/// request: GRPCCore.ServerRequest.Stream<Foo_Bar_Input>
54-
/// ) async throws -> GRPCCore.ServerResponse.Stream<Foo_Bar_Output> {
55-
/// let response = try await self.baz(request: GRPCCore.ServerRequest.Single(stream: request))
56-
/// return GRPCCore.ServerResponse.Stream(single: response)
53+
/// request: GRPCCore.StreamingServerRequest<Foo_Bar_Input>
54+
/// ) async throws -> GRPCCore.StreamingServerResponse<Foo_Bar_Output> {
55+
/// let response = try await self.baz(request: GRPCCore.ServerRequest(stream: request))
56+
/// return GRPCCore.StreamingServerResponse(single: response)
5757
/// }
5858
/// }
5959
///```

Sources/GRPCCore/Call/Client/ClientInterceptor.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
/// let fetchMetadata: @Sendable () async -> String
4141
///
4242
/// func intercept<Input: Sendable, Output: Sendable>(
43-
/// request: ClientRequest.Stream<Input>,
43+
/// request: StreamingClientRequest<Input>,
4444
/// context: ClientContext,
4545
/// next: @Sendable (
46-
/// _ request: ClientRequest.Stream<Input>,
46+
/// _ request: StreamingClientRequest<Input>,
4747
/// _ context: ClientContext
48-
/// ) async throws -> ClientResponse.Stream<Output>
49-
/// ) async throws -> ClientResponse.Stream<Output> {
48+
/// ) async throws -> StreamingClientResponse<Output>
49+
/// ) async throws -> StreamingClientResponse<Output> {
5050
/// // Fetch the metadata value and attach it.
5151
/// let value = await self.fetchMetadata()
5252
/// var request = request
@@ -65,13 +65,13 @@
6565
/// ```swift
6666
/// struct LoggingClientInterceptor: ClientInterceptor {
6767
/// func intercept<Input: Sendable, Output: Sendable>(
68-
/// request: ClientRequest.Stream<Input>,
68+
/// request: StreamingClientRequest<Input>,
6969
/// context: ClientContext,
7070
/// next: @Sendable (
71-
/// _ request: ClientRequest.Stream<Input>,
71+
/// _ request: StreamingClientRequest<Input>,
7272
/// _ context: ClientContext
73-
/// ) async throws -> ClientResponse.Stream<Output>
74-
/// ) async throws -> ClientResponse.Stream<Output> {
73+
/// ) async throws -> StreamingClientResponse<Output>
74+
/// ) async throws -> StreamingClientResponse<Output> {
7575
/// print("Invoking method '\(context.descriptor)'")
7676
/// let response = try await next(request, context)
7777
///
@@ -100,11 +100,11 @@ public protocol ClientInterceptor: Sendable {
100100
/// interceptor in the chain.
101101
/// - Returns: A response object.
102102
func intercept<Input: Sendable, Output: Sendable>(
103-
request: ClientRequest.Stream<Input>,
103+
request: StreamingClientRequest<Input>,
104104
context: ClientContext,
105105
next: (
106-
_ request: ClientRequest.Stream<Input>,
106+
_ request: StreamingClientRequest<Input>,
107107
_ context: ClientContext
108-
) async throws -> ClientResponse.Stream<Output>
109-
) async throws -> ClientResponse.Stream<Output>
108+
) async throws -> StreamingClientResponse<Output>
109+
) async throws -> StreamingClientResponse<Output>
110110
}

Sources/GRPCCore/Call/Client/ClientRequest.swift

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,91 +14,84 @@
1414
* limitations under the License.
1515
*/
1616

17-
/// A namespace for request message types used by clients.
18-
public enum ClientRequest {}
19-
20-
extension ClientRequest {
21-
/// A request created by the client for a single message.
22-
///
23-
/// This is used for unary and server-streaming RPCs.
24-
///
25-
/// See ``ClientRequest/Stream`` for streaming requests and ``ServerRequest/Single`` for the
26-
/// servers representation of a single-message request.
27-
///
28-
/// ## Creating ``Single`` requests
17+
/// A request created by the client for a single message.
18+
///
19+
/// This is used for unary and server-streaming RPCs.
20+
///
21+
/// See ``StreamingClientRequest`` for streaming requests and ``ServerRequest`` for the
22+
/// servers representation of a single-message request.
23+
///
24+
/// ## Creating ``Single`` requests
25+
///
26+
/// ```swift
27+
/// let request = ClientRequest<String>(message: "Hello, gRPC!")
28+
/// print(request.metadata) // prints '[:]'
29+
/// print(request.message) // prints 'Hello, gRPC!'
30+
/// ```
31+
public struct ClientRequest<Message: Sendable>: Sendable {
32+
/// Caller-specified metadata to send to the server at the start of the RPC.
2933
///
30-
/// ```swift
31-
/// let request = ClientRequest.Single<String>(message: "Hello, gRPC!")
32-
/// print(request.metadata) // prints '[:]'
33-
/// print(request.message) // prints 'Hello, gRPC!'
34-
/// ```
35-
public struct Single<Message: Sendable>: Sendable {
36-
/// Caller-specified metadata to send to the server at the start of the RPC.
37-
///
38-
/// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
39-
/// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
40-
/// their own metadata, you should avoid using key names which may clash with transport specific
41-
/// metadata. Note that transports may also impose limits in the amount of metadata which may
42-
/// be sent.
43-
public var metadata: Metadata
34+
/// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
35+
/// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
36+
/// their own metadata, you should avoid using key names which may clash with transport specific
37+
/// metadata. Note that transports may also impose limits in the amount of metadata which may
38+
/// be sent.
39+
public var metadata: Metadata
4440

45-
/// The message to send to the server.
46-
public var message: Message
41+
/// The message to send to the server.
42+
public var message: Message
4743

48-
/// Create a new single client request.
49-
///
50-
/// - Parameters:
51-
/// - message: The message to send to the server.
52-
/// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
53-
public init(
54-
message: Message,
55-
metadata: Metadata = [:]
56-
) {
57-
self.metadata = metadata
58-
self.message = message
59-
}
44+
/// Create a new single client request.
45+
///
46+
/// - Parameters:
47+
/// - message: The message to send to the server.
48+
/// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
49+
public init(
50+
message: Message,
51+
metadata: Metadata = [:]
52+
) {
53+
self.metadata = metadata
54+
self.message = message
6055
}
6156
}
6257

63-
extension ClientRequest {
64-
/// A request created by the client for a stream of messages.
65-
///
66-
/// This is used for client-streaming and bidirectional-streaming RPCs.
58+
/// A request created by the client for a stream of messages.
59+
///
60+
/// This is used for client-streaming and bidirectional-streaming RPCs.
61+
///
62+
/// See ``ClientRequest`` for single-message requests and ``StreamingServerRequest`` for the
63+
/// servers representation of a streaming-message request.
64+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
65+
public struct StreamingClientRequest<Message: Sendable>: Sendable {
66+
/// Caller-specified metadata sent to the server at the start of the RPC.
6767
///
68-
/// See ``ClientRequest/Single`` for single-message requests and ``ServerRequest/Stream`` for the
69-
/// servers representation of a streaming-message request.
70-
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
71-
public struct Stream<Message: Sendable>: Sendable {
72-
/// Caller-specified metadata sent to the server at the start of the RPC.
73-
///
74-
/// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
75-
/// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
76-
/// their own metadata, you should avoid using key names which may clash with transport specific
77-
/// metadata. Note that transports may also impose limits in the amount of metadata which may
78-
/// be sent.
79-
public var metadata: Metadata
68+
/// Both gRPC Swift and its transport layer may insert additional metadata. Keys prefixed with
69+
/// "grpc-" are prohibited and may result in undefined behaviour. Transports may also insert
70+
/// their own metadata, you should avoid using key names which may clash with transport specific
71+
/// metadata. Note that transports may also impose limits in the amount of metadata which may
72+
/// be sent.
73+
public var metadata: Metadata
8074

81-
/// A closure which, when called, writes messages in the writer.
82-
///
83-
/// The producer will only be consumed once by gRPC and therefore isn't required to be
84-
/// idempotent. If the producer throws an error then the RPC will be cancelled. Once the
85-
/// producer returns the request stream is closed.
86-
public var producer: @Sendable (RPCWriter<Message>) async throws -> Void
75+
/// A closure which, when called, writes messages in the writer.
76+
///
77+
/// The producer will only be consumed once by gRPC and therefore isn't required to be
78+
/// idempotent. If the producer throws an error then the RPC will be cancelled. Once the
79+
/// producer returns the request stream is closed.
80+
public var producer: @Sendable (RPCWriter<Message>) async throws -> Void
8781

88-
/// Create a new streaming client request.
89-
///
90-
/// - Parameters:
91-
/// - messageType: The type of message contained in this request, defaults to `Message.self`.
92-
/// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
93-
/// - producer: A closure which writes messages to send to the server. The closure is called
94-
/// at most once and may not be called.
95-
public init(
96-
of messageType: Message.Type = Message.self,
97-
metadata: Metadata = [:],
98-
producer: @escaping @Sendable (RPCWriter<Message>) async throws -> Void
99-
) {
100-
self.metadata = metadata
101-
self.producer = producer
102-
}
82+
/// Create a new streaming client request.
83+
///
84+
/// - Parameters:
85+
/// - messageType: The type of message contained in this request, defaults to `Message.self`.
86+
/// - metadata: Metadata to send to the server at the start of the request. Defaults to empty.
87+
/// - producer: A closure which writes messages to send to the server. The closure is called
88+
/// at most once and may not be called.
89+
public init(
90+
of messageType: Message.Type = Message.self,
91+
metadata: Metadata = [:],
92+
producer: @escaping @Sendable (RPCWriter<Message>) async throws -> Void
93+
) {
94+
self.metadata = metadata
95+
self.producer = producer
10396
}
10497
}

0 commit comments

Comments
 (0)