Skip to content

Commit 2fa9d9a

Browse files
[CodeGenLib] Fixes for the generated code (#1790)
Motivation: - the protocol name for the streaming service used in the typealias does not match the actual streaming service protocol name - we have already replaced the Methods enum name with "Method" so this change has to be reflected in all places where properties of the enum are used - the first argument label of the registerHandler() method is "forMethod" and not "for" Modifications: Modified all the translators that handle these methods or names and their tests. Result: The generated code will be compiled.
1 parent 757e135 commit 2fa9d9a

File tree

8 files changed

+227
-225
lines changed

8 files changed

+227
-225
lines changed

Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
/// ```swift
2525
/// public protocol Foo_BarClientProtocol: Sendable {
2626
/// func baz<R: Sendable>(
27-
/// request: ClientRequest.Single<foo.Bar.Methods.baz.Input>,
28-
/// serializer: some MessageSerializer<foo.Bar.Methods.baz.Input>,
29-
/// deserializer: some MessageDeserializer<foo.Bar.Methods.baz.Output>,
30-
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Methods.Baz.Output>) async throws -> R
31-
/// ) async throws -> ServerResponse.Stream<foo.Bar.Methods.Baz.Output>
27+
/// request: ClientRequest.Single<foo.Bar.Method.baz.Input>,
28+
/// serializer: some MessageSerializer<foo.Bar.Method.baz.Input>,
29+
/// deserializer: some MessageDeserializer<foo.Bar.Method.baz.Output>,
30+
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Method.Baz.Output>) async throws -> R
31+
/// ) async throws -> ServerResponse.Stream<foo.Bar.Method.Baz.Output>
3232
/// }
3333
/// extension Foo.Bar.ClientProtocol {
3434
/// public func get<R: Sendable>(
35-
/// request: ClientRequest.Single<Foo.Bar.Methods.Baz.Input>,
36-
/// _ body: @Sendable @escaping (ClientResponse.Single<Foo.Bar.Methods.Baz.Output>) async throws -> R
35+
/// request: ClientRequest.Single<Foo.Bar.Method.Baz.Input>,
36+
/// _ body: @Sendable @escaping (ClientResponse.Single<Foo.Bar.Method.Baz.Output>) async throws -> R
3737
/// ) async rethrows -> R {
3838
/// try await self.baz(
3939
/// request: request,
40-
/// serializer: ProtobufSerializer<Foo.Bar.Methods.Baz.Input>(),
41-
/// deserializer: ProtobufDeserializer<Foo.Bar.Methods.Baz.Output>(),
40+
/// serializer: ProtobufSerializer<Foo.Bar.Method.Baz.Input>(),
41+
/// deserializer: ProtobufDeserializer<Foo.Bar.Method.Baz.Output>(),
4242
/// body
4343
/// )
4444
/// }
@@ -48,14 +48,14 @@
4848
/// self.client = client
4949
/// }
5050
/// public func methodA<R: Sendable>(
51-
/// request: ClientRequest.Stream<namespaceA.ServiceA.Methods.methodA.Input>,
52-
/// serializer: some MessageSerializer<namespaceA.ServiceA.Methods.methodA.Input>,
53-
/// deserializer: some MessageDeserializer<namespaceA.ServiceA.Methods.methodA.Output>,
54-
/// _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Methods.methodA.Output>) async throws -> R
51+
/// request: ClientRequest.Stream<namespaceA.ServiceA.Method.methodA.Input>,
52+
/// serializer: some MessageSerializer<namespaceA.ServiceA.Method.methodA.Input>,
53+
/// deserializer: some MessageDeserializer<namespaceA.ServiceA.Method.methodA.Output>,
54+
/// _ body: @Sendable @escaping (ClientResponse.Single<namespaceA.ServiceA.Method.methodA.Output>) async throws -> R
5555
/// ) async rethrows -> R {
5656
/// try await self.client.clientStreaming(
5757
/// request: request,
58-
/// descriptor: NamespaceA.ServiceA.Methods.MethodA.descriptor,
58+
/// descriptor: NamespaceA.ServiceA.Method.MethodA.descriptor,
5959
/// serializer: serializer,
6060
/// deserializer: deserializer,
6161
/// handler: body
@@ -399,7 +399,7 @@ extension ClientCodeTranslator {
399399
.init(
400400
label: "descriptor",
401401
expression: .identifierPattern(
402-
"\(service.namespacedTypealiasGeneratedName).Methods.\(method.name.generatedUpperCase).descriptor"
402+
"\(service.namespacedTypealiasGeneratedName).Method.\(method.name.generatedUpperCase).descriptor"
403403
)
404404
),
405405
.init(label: "serializer", expression: .identifierPattern("serializer")),
@@ -439,7 +439,7 @@ extension ClientCodeTranslator {
439439
type: InputOutputType
440440
) -> String {
441441
var components: String =
442-
"\(service.namespacedTypealiasGeneratedName).Methods.\(method.name.generatedUpperCase)"
442+
"\(service.namespacedTypealiasGeneratedName).Method.\(method.name.generatedUpperCase)"
443443

444444
switch type {
445445
case .input:

Sources/GRPCCodeGen/Internal/Translator/ServerCodeTranslator.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@
3131
/// extension foo.Bar.StreamingServiceProtocol {
3232
/// public func registerRPCs(with router: inout RPCRouter) {
3333
/// router.registerHandler(
34-
/// for: foo.Method.baz.descriptor,
35-
/// deserializer: ProtobufDeserializer<foo.Methods.baz.Input>(),
36-
/// serializer: ProtobufSerializer<foo.Methods.baz.Output>(),
34+
/// forMethod: foo.Method.baz.descriptor,
35+
/// deserializer: ProtobufDeserializer<foo.Method.baz.Input>(),
36+
/// serializer: ProtobufSerializer<foo.Method.baz.Output>(),
3737
/// handler: { request in try await self.baz(request: request) }
3838
/// )
3939
/// }
4040
/// }
4141
/// public protocol foo_BarServiceProtocol: foo.Bar.StreamingServiceProtocol {
4242
/// func baz(
43-
/// request: ServerRequest.Single<foo.Bar.Methods.baz.Input>
44-
/// ) async throws -> ServerResponse.Single<foo.Bar.Methods.baz.Output>
43+
/// request: ServerRequest.Single<foo.Bar.Method.baz.Input>
44+
/// ) async throws -> ServerResponse.Single<foo.Bar.Method.baz.Output>
4545
/// }
4646
/// // Generated partial conformance to `foo_BarStreamingServiceProtocol`.
4747
/// extension foo.Bar.ServiceProtocol {
4848
/// public func baz(
49-
/// request: ServerRequest.Stream<foo.Bar.Methods.baz.Input>
50-
/// ) async throws -> ServerResponse.Stream<foo.Bar.Methods.baz.Output> {
49+
/// request: ServerRequest.Stream<foo.Bar.Method.baz.Input>
50+
/// ) async throws -> ServerResponse.Stream<foo.Bar.Method.baz.Output> {
5151
/// let response = try await self.baz(request: ServerRequest.Single(stream: request)
5252
/// return ServerResponse.Stream(single: response)
5353
/// }
@@ -221,7 +221,7 @@ extension ServerCodeTranslator {
221221
var arguments = [FunctionArgumentDescription]()
222222
arguments.append(
223223
.init(
224-
label: "for",
224+
label: "forMethod",
225225
expression: .identifierPattern(
226226
self.methodDescriptorPath(for: method, service: service)
227227
)
@@ -460,7 +460,7 @@ extension ServerCodeTranslator {
460460
type: InputOutputType
461461
) -> String {
462462
var components: String =
463-
"\(service.namespacedTypealiasGeneratedName).Methods.\(method.name.generatedUpperCase)"
463+
"\(service.namespacedTypealiasGeneratedName).Method.\(method.name.generatedUpperCase)"
464464

465465
switch type {
466466
case .input:
@@ -478,7 +478,7 @@ extension ServerCodeTranslator {
478478
service: CodeGenerationRequest.ServiceDescriptor
479479
) -> String {
480480
return
481-
"\(service.namespacedTypealiasGeneratedName).Methods.\(method.name.generatedUpperCase).descriptor"
481+
"\(service.namespacedTypealiasGeneratedName).Method.\(method.name.generatedUpperCase).descriptor"
482482
}
483483

484484
/// Generates the fully qualified name of the type alias for a service protocol.

Sources/GRPCCodeGen/Internal/Translator/TypealiasTranslator.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
/// // ...
3939
///
4040
/// public static let descriptors: [MethodDescriptor] = [
41-
/// echo.Echo.Get.descriptor,
42-
/// echo.Echo.Collect.descriptor,
41+
/// Echo.Echo.Method.Get.descriptor,
42+
/// Echo.Echo.Method.Collect.descriptor,
4343
/// // ...
4444
/// ]
4545
/// }
@@ -222,7 +222,9 @@ extension TypealiasTranslator {
222222
for methodName in methodNames {
223223
let methodDescriptorPath = Expression.memberAccess(
224224
MemberAccessDescription(
225-
left: .identifierType(.member(["Methods", methodName])),
225+
left: .identifierType(
226+
.member([service.namespacedTypealiasGeneratedName, "Method", methodName])
227+
),
226228
right: "descriptor"
227229
)
228230
)
@@ -245,7 +247,7 @@ extension TypealiasTranslator {
245247
let streamingServiceProtocolTypealias = Declaration.typealias(
246248
accessModifier: self.accessModifier,
247249
name: "StreamingServiceProtocol",
248-
existingType: .member("\(service.namespacedGeneratedName)ServiceStreamingProtocol")
250+
existingType: .member("\(service.namespacedGeneratedName)StreamingServiceProtocol")
249251
)
250252
let serviceProtocolTypealias = Declaration.typealias(
251253
accessModifier: self.accessModifier,

0 commit comments

Comments
 (0)