Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public protocol Foo_BarClientProtocol: Sendable {
/// func baz<R>(
/// request: GRPCCore.ClientRequest.Single<Foo_Bar_Input>,
/// request: GRPCCore.ClientRequest<Foo_Bar_Input>,
/// serializer: some GRPCCore.MessageSerializer<Foo_Bar_Input>,
/// deserializer: some GRPCCore.MessageDeserializer<Foo_Bar_Output>,
/// options: GRPCCore.CallOptions = .defaults,
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<Foo_Bar_Output>) async throws -> R
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse<Foo_Bar_Output>) async throws -> R
/// ) async throws -> R where R: Sendable
/// }
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// extension Foo_Bar.ClientProtocol {
/// public func baz<R>(
/// request: GRPCCore.ClientRequest.Single<Foo_Bar_Input>,
/// request: GRPCCore.ClientRequest<Foo_Bar_Input>,
/// options: GRPCCore.CallOptions = .defaults,
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<Foo_Bar_Output>) async throws -> R = {
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse<Foo_Bar_Output>) async throws -> R = {
/// try $0.message
/// }
/// ) async throws -> R where R: Sendable {
Expand All @@ -56,11 +56,11 @@
/// self.client = client
/// }
/// public func methodA<R>(
/// request: GRPCCore.ClientRequest.Stream<Foo_Bar_Input>,
/// request: GRPCCore.StreamingClientRequest<Foo_Bar_Input>,
/// serializer: some GRPCCore.MessageSerializer<Foo_Bar_Input>,
/// deserializer: some GRPCCore.MessageDeserializer<Foo_Bar_Output>,
/// options: GRPCCore.CallOptions = .defaults,
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<Foo_Bar_Output>) async throws -> R = {
/// _ body: @Sendable @escaping (GRPCCore.ClientResponse<Foo_Bar_Output>) async throws -> R = {
/// try $0.message
/// }
/// ) async throws -> R where R: Sendable {
Expand Down Expand Up @@ -263,13 +263,13 @@ extension ClientCodeTranslator {

// All methods have a response handler.
var responseHandler = ParameterDescription(label: "onResponse", name: "handleResponse")
let responseKind = method.isOutputStreaming ? "Stream" : "Single"
let responseKind = method.isOutputStreaming ? "Streaming" : ""
responseHandler.type = .closure(
ClosureSignatureDescription(
parameters: [
ParameterDescription(
type: .generic(
wrapper: .member(["GRPCCore", "ClientResponse", responseKind]),
wrapper: .member(["GRPCCore", "\(responseKind)ClientResponse"]),
wrapped: .member(method.outputType)
)
)
Expand Down Expand Up @@ -299,21 +299,21 @@ extension ClientCodeTranslator {
) -> [CodeBlock] {
// Produces the following:
//
// let request = GRPCCore.ClientRequest.Single<Input>(message: message, metadata: metadata)
// let request = GRPCCore.ClientRequest<Input>(message: message, metadata: metadata)
// return try await method(request: request, options: options, responseHandler: responseHandler)
//
// or:
//
// let request = GRPCCore.ClientRequest.Stream<Input>(metadata: metadata, producer: writer)
// let request = GRPCCore.StreamingClientRequest<Input>(metadata: metadata, producer: writer)
// return try await method(request: request, options: options, responseHandler: responseHandler)

// First, make the init for the ClientRequest
let requestType = method.isInputStreaming ? "Stream" : "Single"
let requestType = method.isInputStreaming ? "Streaming" : ""
var requestInit = FunctionCallDescription(
calledExpression: .identifier(
.type(
.generic(
wrapper: .member(["GRPCCore", "ClientRequest", requestType]),
wrapper: .member(["GRPCCore", "\(requestType)ClientRequest"]),
wrapped: .member(method.inputType)
)
)
Expand Down Expand Up @@ -490,9 +490,10 @@ extension ClientCodeTranslator {
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor
) -> ParameterDescription {
let requestType = method.isInputStreaming ? "Stream" : "Single"
let requestType = method.isInputStreaming ? "Streaming" : ""
let clientRequestType = ExistingTypeDescription.member([
"GRPCCore", "ClientRequest", requestType,
"GRPCCore",
"\(requestType)ClientRequest",
])
return ParameterDescription(
label: "request",
Expand Down Expand Up @@ -538,9 +539,9 @@ extension ClientCodeTranslator {
in service: CodeGenerationRequest.ServiceDescriptor,
includeDefaultResponseHandler: Bool
) -> ParameterDescription {
let clientStreaming = method.isOutputStreaming ? "Stream" : "Single"
let clientStreaming = method.isOutputStreaming ? "Streaming" : ""
let closureParameterType = ExistingTypeDescription.generic(
wrapper: .member(["GRPCCore", "ClientResponse", clientStreaming]),
wrapper: .member(["GRPCCore", "\(clientStreaming)ClientResponse"]),
wrapped: .member(method.outputType)
)

Expand Down
42 changes: 16 additions & 26 deletions Sources/GRPCCodeGen/Internal/Translator/ServerCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public protocol Foo_BarStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
/// func baz(
/// request: GRPCCore.ServerRequest.Stream<Foo_Bar_Input>
/// ) async throws -> GRPCCore.ServerResponse.Stream<Foo_Bar_Output>
/// request: GRPCCore.StreamingServerRequest<Foo_Bar_Input>
/// ) async throws -> GRPCCore.StreamingServerResponse<Foo_Bar_Output>
/// }
/// // Conformance to `GRPCCore.RegistrableRPCService`.
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
Expand All @@ -43,17 +43,17 @@
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public protocol Foo_BarServiceProtocol: Foo_Bar.StreamingServiceProtocol {
/// func baz(
/// request: GRPCCore.ServerRequest.Single<Foo_Bar_Input>
/// ) async throws -> GRPCCore.ServerResponse.Single<Foo_Bar_Output>
/// request: GRPCCore.ServerRequest<Foo_Bar_Input>
/// ) async throws -> GRPCCore.ServerResponse<Foo_Bar_Output>
/// }
/// // Partial conformance to `Foo_BarStreamingServiceProtocol`.
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// extension Foo_Bar.ServiceProtocol {
/// public func baz(
/// request: GRPCCore.ServerRequest.Stream<Foo_Bar_Input>
/// ) async throws -> GRPCCore.ServerResponse.Stream<Foo_Bar_Output> {
/// let response = try await self.baz(request: GRPCCore.ServerRequest.Single(stream: request))
/// return GRPCCore.ServerResponse.Stream(single: response)
/// request: GRPCCore.StreamingServerRequest<Foo_Bar_Input>
/// ) async throws -> GRPCCore.StreamingServerResponse<Foo_Bar_Output> {
/// let response = try await self.baz(request: GRPCCore.ServerRequest(stream: request))
/// return GRPCCore.StreamingServerResponse(single: response)
/// }
/// }
///```
Expand Down Expand Up @@ -147,7 +147,7 @@ extension ServerCodeTranslator {
.init(
label: "request",
type: .generic(
wrapper: .member(["GRPCCore", "ServerRequest", "Stream"]),
wrapper: .member(["GRPCCore", "StreamingServerRequest"]),
wrapped: .member(method.inputType)
)
),
Expand All @@ -156,7 +156,7 @@ extension ServerCodeTranslator {
keywords: [.async, .throws],
returnType: .identifierType(
.generic(
wrapper: .member(["GRPCCore", "ServerResponse", "Stream"]),
wrapper: .member(["GRPCCore", "StreamingServerResponse"]),
wrapped: .member(method.outputType)
)
)
Expand Down Expand Up @@ -313,8 +313,8 @@ extension ServerCodeTranslator {
in service: CodeGenerationRequest.ServiceDescriptor,
accessModifier: AccessModifier? = nil
) -> Declaration {
let inputStreaming = method.isInputStreaming ? "Stream" : "Single"
let outputStreaming = method.isOutputStreaming ? "Stream" : "Single"
let inputStreaming = method.isInputStreaming ? "Streaming" : ""
let outputStreaming = method.isOutputStreaming ? "Streaming" : ""

let functionSignature = FunctionSignatureDescription(
accessModifier: accessModifier,
Expand All @@ -324,7 +324,7 @@ extension ServerCodeTranslator {
label: "request",
type:
.generic(
wrapper: .member(["GRPCCore", "ServerRequest", inputStreaming]),
wrapper: .member(["GRPCCore", "\(inputStreaming)ServerRequest"]),
wrapped: .member(method.inputType)
)
),
Expand All @@ -333,7 +333,7 @@ extension ServerCodeTranslator {
keywords: [.async, .throws],
returnType: .identifierType(
.generic(
wrapper: .member(["GRPCCore", "ServerResponse", outputStreaming]),
wrapper: .member(["GRPCCore", "\(outputStreaming)ServerResponse"]),
wrapped: .member(method.outputType)
)
)
Expand Down Expand Up @@ -391,12 +391,7 @@ extension ServerCodeTranslator {
if !method.isInputStreaming {
// Transform the streaming request into a unary request.
serverRequest = Expression.functionCall(
calledExpression: .memberAccess(
MemberAccessDescription(
left: .identifierPattern("GRPCCore.ServerRequest"),
right: "Single"
)
),
calledExpression: .identifierType(.member(["GRPCCore", "ServerRequest"])),
arguments: [
FunctionArgumentDescription(label: "stream", expression: .identifierPattern("request"))
]
Expand Down Expand Up @@ -433,12 +428,7 @@ extension ServerCodeTranslator {
// Transforming the unary response into a streaming one.
if !method.isOutputStreaming {
returnValue = .functionCall(
calledExpression: .memberAccess(
MemberAccessDescription(
left: .identifierType(.member(["GRPCCore", "ServerResponse"])),
right: "Stream"
)
),
calledExpression: .identifier(.type(.member(["GRPCCore", "StreamingServerResponse"]))),
arguments: [
(FunctionArgumentDescription(label: "single", expression: .identifierPattern("response")))
]
Expand Down
24 changes: 12 additions & 12 deletions Sources/GRPCCore/Call/Client/ClientInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
/// let fetchMetadata: @Sendable () async -> String
///
/// func intercept<Input: Sendable, Output: Sendable>(
/// request: ClientRequest.Stream<Input>,
/// request: StreamingClientRequest<Input>,
/// context: ClientContext,
/// next: @Sendable (
/// _ request: ClientRequest.Stream<Input>,
/// _ request: StreamingClientRequest<Input>,
/// _ context: ClientContext
/// ) async throws -> ClientResponse.Stream<Output>
/// ) async throws -> ClientResponse.Stream<Output> {
/// ) async throws -> StreamingClientResponse<Output>
/// ) async throws -> StreamingClientResponse<Output> {
/// // Fetch the metadata value and attach it.
/// let value = await self.fetchMetadata()
/// var request = request
Expand All @@ -65,13 +65,13 @@
/// ```swift
/// struct LoggingClientInterceptor: ClientInterceptor {
/// func intercept<Input: Sendable, Output: Sendable>(
/// request: ClientRequest.Stream<Input>,
/// request: StreamingClientRequest<Input>,
/// context: ClientContext,
/// next: @Sendable (
/// _ request: ClientRequest.Stream<Input>,
/// _ request: StreamingClientRequest<Input>,
/// _ context: ClientContext
/// ) async throws -> ClientResponse.Stream<Output>
/// ) async throws -> ClientResponse.Stream<Output> {
/// ) async throws -> StreamingClientResponse<Output>
/// ) async throws -> StreamingClientResponse<Output> {
/// print("Invoking method '\(context.descriptor)'")
/// let response = try await next(request, context)
///
Expand Down Expand Up @@ -100,11 +100,11 @@ public protocol ClientInterceptor: Sendable {
/// interceptor in the chain.
/// - Returns: A response object.
func intercept<Input: Sendable, Output: Sendable>(
request: ClientRequest.Stream<Input>,
request: StreamingClientRequest<Input>,
context: ClientContext,
next: (
_ request: ClientRequest.Stream<Input>,
_ request: StreamingClientRequest<Input>,
_ context: ClientContext
) async throws -> ClientResponse.Stream<Output>
) async throws -> ClientResponse.Stream<Output>
) async throws -> StreamingClientResponse<Output>
) async throws -> StreamingClientResponse<Output>
}
Loading
Loading