Skip to content

Commit 757e135

Browse files
[CodeGenLib] Change location of method descriptor array in generated code (#1789)
Motivation: The method descriptor array should reside inside the Methods enum (which should be called Method). Modifications: - added the descriptor array as a member of the Methods enum - chenged the Methods enum name to Method - changed the tests accordingly Result: Better organized generated code.
1 parent d95ec7a commit 757e135

File tree

4 files changed

+79
-63
lines changed

4 files changed

+79
-63
lines changed

Sources/GRPCCodeGen/Internal/Translator/TypealiasTranslator.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/// ```swift
2424
/// public enum Echo {
2525
/// public enum Echo {
26-
/// public enum Methods {
26+
/// public enum Method {
2727
/// public enum Get {
2828
/// public typealias Input = Echo_EchoRequest
2929
/// public typealias Output = Echo_EchoResponse
@@ -36,12 +36,13 @@
3636
/// public static let descriptor = MethodDescriptor(service: "echo.Echo", method: "Collect")
3737
/// }
3838
/// // ...
39+
///
40+
/// public static let descriptors: [MethodDescriptor] = [
41+
/// echo.Echo.Get.descriptor,
42+
/// echo.Echo.Collect.descriptor,
43+
/// // ...
44+
/// ]
3945
/// }
40-
/// public static let methods: [MethodDescriptor] = [
41-
/// echo.Echo.Get.descriptor,
42-
/// echo.Echo.Collect.descriptor,
43-
/// // ...
44-
/// ]
4546
///
4647
/// public typealias StreamingServiceProtocol = echo_EchoServiceStreamingProtocol
4748
/// public typealias ServiceProtocol = echo_EchoServiceProtocol
@@ -119,19 +120,20 @@ extension TypealiasTranslator {
119120
accessModifier: self.accessModifier,
120121
name: service.name.generatedUpperCase
121122
)
122-
var methodsEnum = EnumDescription(accessModifier: self.accessModifier, name: "Methods")
123+
var methodsEnum = EnumDescription(accessModifier: self.accessModifier, name: "Method")
123124
let methods = service.methods
124125

125126
// Create the method specific enums.
126127
for method in methods {
127128
let methodEnum = self.makeMethodEnum(from: method, in: service)
128129
methodsEnum.members.append(methodEnum)
129130
}
130-
serviceEnum.members.append(.enum(methodsEnum))
131131

132132
// Create the method descriptor array.
133133
let methodDescriptorsDeclaration = self.makeMethodDescriptors(for: service)
134-
serviceEnum.members.append(methodDescriptorsDeclaration)
134+
methodsEnum.members.append(methodDescriptorsDeclaration)
135+
136+
serviceEnum.members.append(.enum(methodsEnum))
135137

136138
if self.server {
137139
// Create the streaming and non-streaming service protocol type aliases.
@@ -231,7 +233,7 @@ extension TypealiasTranslator {
231233
accessModifier: self.accessModifier,
232234
isStatic: true,
233235
kind: .let,
234-
left: .identifier(.pattern("methods")),
236+
left: .identifier(.pattern("descriptors")),
235237
type: .array(.member("MethodDescriptor")),
236238
right: .literal(.array(methodDescriptors))
237239
)

Tests/GRPCCodeGenTests/Internal/Translator/IDLToStructuredSwiftTranslatorSnippetBasedTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
172172
173173
public enum NamespaceA {
174174
public enum ServiceA {
175-
public enum Methods {}
176-
public static let methods: [MethodDescriptor] = []
175+
public enum Method {
176+
public static let descriptors: [MethodDescriptor] = []
177+
}
177178
public typealias StreamingServiceProtocol = NamespaceA_ServiceAServiceStreamingProtocol
178179
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
179180
}

Tests/GRPCCodeGenTests/Internal/Translator/TypealiasTranslatorSnippetBasedTests.swift

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
4848
"""
4949
public enum NamespaceA {
5050
public enum ServiceA {
51-
public enum Methods {
51+
public enum Method {
5252
public enum MethodA {
5353
public typealias Input = NamespaceA_ServiceARequest
5454
public typealias Output = NamespaceA_ServiceAResponse
@@ -57,10 +57,10 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
5757
method: "MethodA"
5858
)
5959
}
60+
public static let descriptors: [MethodDescriptor] = [
61+
Methods.MethodA.descriptor
62+
]
6063
}
61-
public static let methods: [MethodDescriptor] = [
62-
Methods.MethodA.descriptor
63-
]
6464
public typealias StreamingServiceProtocol = NamespaceA_ServiceAServiceStreamingProtocol
6565
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
6666
public typealias ClientProtocol = NamespaceA_ServiceAClientProtocol
@@ -93,8 +93,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
9393
"""
9494
public enum NamespaceA {
9595
public enum ServiceA {
96-
public enum Methods {}
97-
public static let methods: [MethodDescriptor] = []
96+
public enum Method {
97+
public static let descriptors: [MethodDescriptor] = []
98+
}
9899
public typealias StreamingServiceProtocol = NamespaceA_ServiceAServiceStreamingProtocol
99100
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
100101
public typealias ClientProtocol = NamespaceA_ServiceAClientProtocol
@@ -127,8 +128,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
127128
"""
128129
public enum NamespaceA {
129130
public enum ServiceA {
130-
public enum Methods {}
131-
public static let methods: [MethodDescriptor] = []
131+
public enum Method {
132+
public static let descriptors: [MethodDescriptor] = []
133+
}
132134
public typealias StreamingServiceProtocol = NamespaceA_ServiceAServiceStreamingProtocol
133135
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
134136
}
@@ -159,8 +161,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
159161
"""
160162
public enum NamespaceA {
161163
public enum ServiceA {
162-
public enum Methods {}
163-
public static let methods: [MethodDescriptor] = []
164+
public enum Method {
165+
public static let descriptors: [MethodDescriptor] = []
166+
}
164167
public typealias ClientProtocol = NamespaceA_ServiceAClientProtocol
165168
public typealias Client = NamespaceA_ServiceAClient
166169
}
@@ -191,8 +194,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
191194
"""
192195
public enum NamespaceA {
193196
public enum ServiceA {
194-
public enum Methods {}
195-
public static let methods: [MethodDescriptor] = []
197+
public enum Method {
198+
public static let descriptors: [MethodDescriptor] = []
199+
}
196200
}
197201
}
198202
"""
@@ -224,7 +228,7 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
224228
let expectedSwift =
225229
"""
226230
public enum ServiceA {
227-
public enum Methods {
231+
public enum Method {
228232
public enum MethodA {
229233
public typealias Input = ServiceARequest
230234
public typealias Output = ServiceAResponse
@@ -233,10 +237,10 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
233237
method: "MethodA"
234238
)
235239
}
240+
public static let descriptors: [MethodDescriptor] = [
241+
Methods.MethodA.descriptor
242+
]
236243
}
237-
public static let methods: [MethodDescriptor] = [
238-
Methods.MethodA.descriptor
239-
]
240244
public typealias StreamingServiceProtocol = ServiceAServiceStreamingProtocol
241245
public typealias ServiceProtocol = ServiceAServiceProtocol
242246
public typealias ClientProtocol = ServiceAClientProtocol
@@ -284,7 +288,7 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
284288
"""
285289
public enum NamespaceA {
286290
public enum ServiceA {
287-
public enum Methods {
291+
public enum Method {
288292
public enum MethodA {
289293
public typealias Input = NamespaceA_ServiceARequest
290294
public typealias Output = NamespaceA_ServiceAResponse
@@ -301,11 +305,11 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
301305
method: "MethodB"
302306
)
303307
}
308+
public static let descriptors: [MethodDescriptor] = [
309+
Methods.MethodA.descriptor,
310+
Methods.MethodB.descriptor
311+
]
304312
}
305-
public static let methods: [MethodDescriptor] = [
306-
Methods.MethodA.descriptor,
307-
Methods.MethodB.descriptor
308-
]
309313
public typealias StreamingServiceProtocol = NamespaceA_ServiceAServiceStreamingProtocol
310314
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
311315
public typealias ClientProtocol = NamespaceA_ServiceAClientProtocol
@@ -338,8 +342,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
338342
"""
339343
package enum NamespaceA {
340344
package enum ServiceA {
341-
package enum Methods {}
342-
package static let methods: [MethodDescriptor] = []
345+
package enum Method {
346+
package static let descriptors: [MethodDescriptor] = []
347+
}
343348
package typealias StreamingServiceProtocol = NamespaceA_ServiceAServiceStreamingProtocol
344349
package typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
345350
package typealias ClientProtocol = NamespaceA_ServiceAClientProtocol
@@ -384,16 +389,18 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
384389
"""
385390
public enum NamespaceA {
386391
public enum Aservice {
387-
public enum Methods {}
388-
public static let methods: [MethodDescriptor] = []
392+
public enum Method {
393+
public static let descriptors: [MethodDescriptor] = []
394+
}
389395
public typealias StreamingServiceProtocol = NamespaceA_AserviceServiceStreamingProtocol
390396
public typealias ServiceProtocol = NamespaceA_AserviceServiceProtocol
391397
public typealias ClientProtocol = NamespaceA_AserviceClientProtocol
392398
public typealias Client = NamespaceA_AserviceClient
393399
}
394400
public enum Bservice {
395-
public enum Methods {}
396-
public static let methods: [MethodDescriptor] = []
401+
public enum Method {
402+
public static let descriptors: [MethodDescriptor] = []
403+
}
397404
public typealias StreamingServiceProtocol = NamespaceA_BserviceServiceStreamingProtocol
398405
public typealias ServiceProtocol = NamespaceA_BserviceServiceProtocol
399406
public typealias ClientProtocol = NamespaceA_BserviceClientProtocol
@@ -429,16 +436,18 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
429436
let expectedSwift =
430437
"""
431438
package enum AService {
432-
package enum Methods {}
433-
package static let methods: [MethodDescriptor] = []
439+
package enum Method {
440+
package static let descriptors: [MethodDescriptor] = []
441+
}
434442
package typealias StreamingServiceProtocol = AServiceServiceStreamingProtocol
435443
package typealias ServiceProtocol = AServiceServiceProtocol
436444
package typealias ClientProtocol = AServiceClientProtocol
437445
package typealias Client = AServiceClient
438446
}
439447
package enum BService {
440-
package enum Methods {}
441-
package static let methods: [MethodDescriptor] = []
448+
package enum Method {
449+
package static let descriptors: [MethodDescriptor] = []
450+
}
442451
package typealias StreamingServiceProtocol = BServiceServiceStreamingProtocol
443452
package typealias ServiceProtocol = BServiceServiceProtocol
444453
package typealias ClientProtocol = BServiceClientProtocol
@@ -482,8 +491,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
482491
"""
483492
internal enum Anamespace {
484493
internal enum AService {
485-
internal enum Methods {}
486-
internal static let methods: [MethodDescriptor] = []
494+
internal enum Method {
495+
internal static let descriptors: [MethodDescriptor] = []
496+
}
487497
internal typealias StreamingServiceProtocol = Anamespace_AServiceServiceStreamingProtocol
488498
internal typealias ServiceProtocol = Anamespace_AServiceServiceProtocol
489499
internal typealias ClientProtocol = Anamespace_AServiceClientProtocol
@@ -492,8 +502,9 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
492502
}
493503
internal enum Bnamespace {
494504
internal enum BService {
495-
internal enum Methods {}
496-
internal static let methods: [MethodDescriptor] = []
505+
internal enum Method {
506+
internal static let descriptors: [MethodDescriptor] = []
507+
}
497508
internal typealias StreamingServiceProtocol = Bnamespace_BServiceServiceStreamingProtocol
498509
internal typealias ServiceProtocol = Bnamespace_BServiceServiceProtocol
499510
internal typealias ClientProtocol = Bnamespace_BServiceClientProtocol
@@ -531,17 +542,19 @@ final class TypealiasTranslatorSnippetBasedTests: XCTestCase {
531542
let expectedSwift =
532543
"""
533544
public enum BService {
534-
public enum Methods {}
535-
public static let methods: [MethodDescriptor] = []
545+
public enum Method {
546+
public static let descriptors: [MethodDescriptor] = []
547+
}
536548
public typealias StreamingServiceProtocol = BServiceServiceStreamingProtocol
537549
public typealias ServiceProtocol = BServiceServiceProtocol
538550
public typealias ClientProtocol = BServiceClientProtocol
539551
public typealias Client = BServiceClient
540552
}
541553
public enum Anamespace {
542554
public enum AService {
543-
public enum Methods {}
544-
public static let methods: [MethodDescriptor] = []
555+
public enum Method {
556+
public static let descriptors: [MethodDescriptor] = []
557+
}
545558
public typealias StreamingServiceProtocol = Anamespace_AServiceServiceStreamingProtocol
546559
public typealias ServiceProtocol = Anamespace_AServiceServiceProtocol
547560
public typealias ClientProtocol = Anamespace_AServiceClientProtocol

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
5858
5959
internal enum Helloworld {
6060
internal enum Greeter {
61-
internal enum Methods {
61+
internal enum Method {
6262
internal enum SayHello {
6363
internal typealias Input = HelloRequest
6464
internal typealias Output = HelloReply
@@ -67,10 +67,10 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
6767
method: "SayHello"
6868
)
6969
}
70+
internal static let descriptors: [MethodDescriptor] = [
71+
Methods.SayHello.descriptor
72+
]
7073
}
71-
internal static let methods: [MethodDescriptor] = [
72-
Methods.SayHello.descriptor
73-
]
7474
internal typealias ClientProtocol = Helloworld_GreeterClientProtocol
7575
internal typealias Client = Helloworld_GreeterClient
7676
}
@@ -162,7 +162,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
162162
163163
public enum Helloworld {
164164
public enum Greeter {
165-
public enum Methods {
165+
public enum Method {
166166
public enum SayHello {
167167
public typealias Input = HelloRequest
168168
public typealias Output = HelloReply
@@ -171,10 +171,10 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
171171
method: "SayHello"
172172
)
173173
}
174+
public static let descriptors: [MethodDescriptor] = [
175+
Methods.SayHello.descriptor
176+
]
174177
}
175-
public static let methods: [MethodDescriptor] = [
176-
Methods.SayHello.descriptor
177-
]
178178
public typealias StreamingServiceProtocol = Helloworld_GreeterServiceStreamingProtocol
179179
public typealias ServiceProtocol = Helloworld_GreeterServiceProtocol
180180
}
@@ -249,7 +249,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
249249
250250
package enum Helloworld {
251251
package enum Greeter {
252-
package enum Methods {
252+
package enum Method {
253253
package enum SayHello {
254254
package typealias Input = HelloRequest
255255
package typealias Output = HelloReply
@@ -258,10 +258,10 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
258258
method: "SayHello"
259259
)
260260
}
261+
package static let descriptors: [MethodDescriptor] = [
262+
Methods.SayHello.descriptor
263+
]
261264
}
262-
package static let methods: [MethodDescriptor] = [
263-
Methods.SayHello.descriptor
264-
]
265265
package typealias StreamingServiceProtocol = Helloworld_GreeterServiceStreamingProtocol
266266
package typealias ServiceProtocol = Helloworld_GreeterServiceProtocol
267267
package typealias ClientProtocol = Helloworld_GreeterClientProtocol

0 commit comments

Comments
 (0)