Skip to content

Commit 6145631

Browse files
[CodeGenLib] Typealias translator configuration for client and server (#1750)
Motivation: The typealias translator should generate typealias for the client and server protocols only if the associated code (client/server code) will be generated as well. Also, at the moment the Typealias translator doesn't generate the ClientProtocol typealias. Modifications: - added a new initialiser for the TypealiasTranslator that sets the client and server property. - Based on the client and server properties' values the typealias translator generates or not the protocol typealiases. - created method gor client protocol typalias generation. - modified tests, so they include the client code. - added new tests for generating only client or server code. Result: The typealias translator will generate typealiases for client and server protocols only when the associated code is also generated, getting rid of the possibility of an error (when the protocol doesn't exist).
1 parent 717e3f8 commit 6145631

File tree

3 files changed

+211
-34
lines changed

3 files changed

+211
-34
lines changed

Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
*/
1616

1717
struct IDLToStructuredSwiftTranslator: Translator {
18-
private let typealiasTranslator = TypealiasTranslator()
1918
private let serverCodeTranslator = ServerCodeTranslator()
2019

2120
func translate(
2221
codeGenerationRequest: CodeGenerationRequest,
2322
client: Bool,
2423
server: Bool
2524
) throws -> StructuredSwiftRepresentation {
25+
let typealiasTranslator = TypealiasTranslator(client: client, server: server)
2626
let topComment = Comment.doc(codeGenerationRequest.leadingTrivia)
2727
let imports: [ImportDescription] = [
2828
ImportDescription(moduleName: "GRPCCore")
2929
]
3030
var codeBlocks: [CodeBlock] = []
3131
codeBlocks.append(
32-
contentsOf: try self.typealiasTranslator.translate(from: codeGenerationRequest)
32+
contentsOf: try typealiasTranslator.translate(from: codeGenerationRequest)
3333
)
3434

3535
if server {

Sources/GRPCCodeGen/Internal/Translator/TypealiasTranslator.swift

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
/// A ``CodeGenerationRequest`` can contain multiple namespaces, so the TypealiasTranslator will create a ``CodeBlock``
5454
/// for each namespace.
5555
struct TypealiasTranslator: SpecializedTranslator {
56+
let client: Bool
57+
let server: Bool
58+
59+
init(client: Bool, server: Bool) {
60+
self.client = client
61+
self.server = server
62+
}
63+
5664
func translate(from codeGenerationRequest: CodeGenerationRequest) throws -> [CodeBlock] {
5765
var codeBlocks: [CodeBlock] = []
5866
let services = codeGenerationRequest.services
@@ -169,9 +177,21 @@ extension TypealiasTranslator {
169177
let methodDescriptorsDeclaration = self.makeMethodDescriptors(for: service)
170178
serviceEnum.members.append(methodDescriptorsDeclaration)
171179

172-
// Create the streaming and non-streaming service protocol type aliases.
173-
let serviceProtocols = self.makeServiceProtocolsTypealiases(for: service)
174-
serviceEnum.members.append(contentsOf: serviceProtocols)
180+
if self.server {
181+
// Create the streaming and non-streaming service protocol type aliases.
182+
let serviceProtocols = self.makeServiceProtocolsTypealiases(for: service)
183+
serviceEnum.members.append(contentsOf: serviceProtocols)
184+
}
185+
186+
if self.client {
187+
// Create the client protocol type alias.
188+
let clientProtocol = self.makeClientProtocolTypealias(for: service)
189+
serviceEnum.members.append(clientProtocol)
190+
191+
// Create type alias for Client struct.
192+
let clientStruct = self.makeClientStructTypealias(for: service)
193+
serviceEnum.members.append(clientStruct)
194+
}
175195

176196
return .enum(serviceEnum)
177197
}
@@ -236,7 +256,7 @@ extension TypealiasTranslator {
236256

237257
let descriptorDeclarationRight = Expression.functionCall(
238258
FunctionCallDescription(
239-
calledExpression: .identifierType(.member(["MethodDescriptor"])),
259+
calledExpression: .identifierType(.member("MethodDescriptor")),
240260
arguments: [
241261
FunctionArgumentDescription(
242262
label: "service",
@@ -277,34 +297,41 @@ extension TypealiasTranslator {
277297
isStatic: true,
278298
kind: .let,
279299
left: .identifier(.pattern("methods")),
280-
type: .array(.member(["MethodDescriptor"])),
300+
type: .array(.member("MethodDescriptor")),
281301
right: .literal(.array(methodDescriptors))
282302
)
283303
}
284304

285305
private func makeServiceProtocolsTypealiases(
286306
for service: CodeGenerationRequest.ServiceDescriptor
287307
) -> [Declaration] {
288-
let namespacedPrefix: String
289-
290-
if service.namespace.isEmpty {
291-
namespacedPrefix = service.name
292-
} else {
293-
namespacedPrefix = "\(service.namespace)_\(service.name)"
294-
}
295-
296-
let streamingServiceProtocolName = "\(namespacedPrefix)ServiceStreamingProtocol"
297308
let streamingServiceProtocolTypealias = Declaration.typealias(
298309
name: "StreamingServiceProtocol",
299-
existingType: .member([streamingServiceProtocolName])
310+
existingType: .member("\(service.namespacedPrefix)ServiceStreamingProtocol")
300311
)
301-
302-
let serviceProtocolName = "\(namespacedPrefix)ServiceProtocol"
303312
let serviceProtocolTypealias = Declaration.typealias(
304313
name: "ServiceProtocol",
305-
existingType: .member([serviceProtocolName])
314+
existingType: .member("\(service.namespacedPrefix)ServiceProtocol")
306315
)
307316

308317
return [streamingServiceProtocolTypealias, serviceProtocolTypealias]
309318
}
319+
320+
private func makeClientProtocolTypealias(
321+
for service: CodeGenerationRequest.ServiceDescriptor
322+
) -> Declaration {
323+
return .typealias(
324+
name: "ClientProtocol",
325+
existingType: .member("\(service.namespacedPrefix)ClientProtocol")
326+
)
327+
}
328+
329+
private func makeClientStructTypealias(
330+
for service: CodeGenerationRequest.ServiceDescriptor
331+
) -> Declaration {
332+
return .typealias(
333+
name: "Client",
334+
existingType: .member("\(service.namespacedPrefix)Client")
335+
)
336+
}
310337
}

0 commit comments

Comments
 (0)