Skip to content

Commit 8d3341a

Browse files
Generated code fixtures: availability guards and descriptor paths (#1797)
Motivation: The generated code must contain the availability guards on top of all protocols, extensions and typealiases for protocols in order to be compiled.The full descriptors path caused errors for namespaces and services with the same name, so we will use the relative paths to them in the descriptors array from the 'Method' enum. Modifications: - added the availability guards in the translators - modified the descriptors path in the typealias translator (for the descriptors array) - modified all tests accordingly Result: The generated code will be compiled.
1 parent 25c33fd commit 8d3341a

File tree

8 files changed

+188
-33
lines changed

8 files changed

+188
-33
lines changed

Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/// a representation for the following generated code:
2323
///
2424
/// ```swift
25+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2526
/// public protocol Foo_BarClientProtocol: Sendable {
2627
/// func baz<R: Sendable>(
2728
/// request: ClientRequest.Single<foo.Bar.Method.baz.Input>,
@@ -30,6 +31,7 @@
3031
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Method.Baz.Output>) async throws -> R
3132
/// ) async throws -> ServerResponse.Stream<foo.Bar.Method.Baz.Output>
3233
/// }
34+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
3335
/// extension Foo.Bar.ClientProtocol {
3436
/// public func get<R: Sendable>(
3537
/// request: ClientRequest.Single<Foo.Bar.Method.Baz.Input>,
@@ -42,6 +44,7 @@
4244
/// body
4345
/// )
4446
/// }
47+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4548
/// public struct foo_BarClient: foo.Bar.ClientProtocol {
4649
/// private let client: GRPCCore.GRPCClient
4750
/// public init(client: GRPCCore.GRPCClient) {
@@ -120,7 +123,7 @@ extension ClientCodeTranslator {
120123
members: methods
121124
)
122125
)
123-
return clientProtocol
126+
return .guarded(self.availabilityGuard, clientProtocol)
124127
}
125128

126129
private func makeExtensionProtocol(
@@ -142,7 +145,10 @@ extension ClientCodeTranslator {
142145
declarations: methods
143146
)
144147
)
145-
return clientProtocolExtension
148+
return .guarded(
149+
self.availabilityGuard,
150+
clientProtocolExtension
151+
)
146152
}
147153

148154
private func makeClientProtocolMethod(

Sources/GRPCCodeGen/Internal/Translator/ServerCodeTranslator.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
/// a representation for the following generated code:
2323
///
2424
/// ```swift
25+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2526
/// public protocol foo_BarServiceStreamingProtocol: GRPCCore.RegistrableRPCService {
2627
/// func baz(
2728
/// request: ServerRequest.Stream<foo.Method.baz.Input>
2829
/// ) async throws -> ServerResponse.Stream<foo.Method.baz.Output>
2930
/// }
3031
/// // Generated conformance to `RegistrableRPCService`.
32+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
3133
/// extension foo.Bar.StreamingServiceProtocol {
3234
/// public func registerRPCs(with router: inout RPCRouter) {
3335
/// router.registerHandler(
@@ -38,12 +40,14 @@
3840
/// )
3941
/// }
4042
/// }
43+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4144
/// public protocol foo_BarServiceProtocol: foo.Bar.StreamingServiceProtocol {
4245
/// func baz(
4346
/// request: ServerRequest.Single<foo.Bar.Method.baz.Input>
4447
/// ) async throws -> ServerResponse.Single<foo.Bar.Method.baz.Output>
4548
/// }
4649
/// // Generated partial conformance to `foo_BarStreamingServiceProtocol`.
50+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4751
/// extension foo.Bar.ServiceProtocol {
4852
/// public func baz(
4953
/// request: ServerRequest.Stream<foo.Bar.Method.baz.Input>
@@ -168,9 +172,12 @@ extension ServerCodeTranslator {
168172
) -> Declaration {
169173
let streamingProtocol = self.protocolNameTypealias(service: service, streaming: true)
170174
let registerRPCMethod = self.makeRegisterRPCsMethod(for: service, in: codeGenerationRequest)
171-
return .extension(
172-
onType: streamingProtocol,
173-
declarations: [registerRPCMethod]
175+
return .guarded(
176+
self.availabilityGuard,
177+
.extension(
178+
onType: streamingProtocol,
179+
declarations: [registerRPCMethod]
180+
)
174181
)
175182
}
176183

@@ -297,12 +304,15 @@ extension ServerCodeTranslator {
297304

298305
return .commentable(
299306
.preFormatted(service.documentation),
300-
.protocol(
301-
ProtocolDescription(
302-
accessModifier: self.accessModifier,
303-
name: protocolName,
304-
conformances: [streamingProtocol],
305-
members: methods
307+
.guarded(
308+
self.availabilityGuard,
309+
.protocol(
310+
ProtocolDescription(
311+
accessModifier: self.accessModifier,
312+
name: protocolName,
313+
conformances: [streamingProtocol],
314+
members: methods
315+
)
306316
)
307317
)
308318
)
@@ -363,9 +373,12 @@ extension ServerCodeTranslator {
363373
}
364374

365375
let protocolName = self.protocolNameTypealias(service: service, streaming: false)
366-
return .extension(
367-
onType: protocolName,
368-
declarations: methods
376+
return .guarded(
377+
self.availabilityGuard,
378+
.extension(
379+
onType: protocolName,
380+
declarations: methods
381+
)
369382
)
370383
}
371384

Sources/GRPCCodeGen/Internal/Translator/TypealiasTranslator.swift

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
/// // ...
3939
///
4040
/// public static let descriptors: [MethodDescriptor] = [
41-
/// Echo.Echo.Method.Get.descriptor,
42-
/// Echo.Echo.Method.Collect.descriptor,
41+
/// Get.descriptor,
42+
/// Collect.descriptor,
4343
/// // ...
4444
/// ]
4545
/// }
4646
///
47+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4748
/// public typealias StreamingServiceProtocol = echo_EchoServiceStreamingProtocol
49+
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4850
/// public typealias ServiceProtocol = echo_EchoServiceProtocol
4951
///
5052
/// }
@@ -223,7 +225,7 @@ extension TypealiasTranslator {
223225
let methodDescriptorPath = Expression.memberAccess(
224226
MemberAccessDescription(
225227
left: .identifierType(
226-
.member([service.namespacedTypealiasGeneratedName, "Method", methodName])
228+
.member([methodName])
227229
),
228230
right: "descriptor"
229231
)
@@ -255,26 +257,41 @@ extension TypealiasTranslator {
255257
existingType: .member("\(service.namespacedGeneratedName)ServiceProtocol")
256258
)
257259

258-
return [streamingServiceProtocolTypealias, serviceProtocolTypealias]
260+
return [
261+
.guarded(
262+
self.availabilityGuard,
263+
streamingServiceProtocolTypealias
264+
),
265+
.guarded(
266+
self.availabilityGuard,
267+
serviceProtocolTypealias
268+
),
269+
]
259270
}
260271

261272
private func makeClientProtocolTypealias(
262273
for service: CodeGenerationRequest.ServiceDescriptor
263274
) -> Declaration {
264-
return .typealias(
265-
accessModifier: self.accessModifier,
266-
name: "ClientProtocol",
267-
existingType: .member("\(service.namespacedGeneratedName)ClientProtocol")
275+
return .guarded(
276+
self.availabilityGuard,
277+
.typealias(
278+
accessModifier: self.accessModifier,
279+
name: "ClientProtocol",
280+
existingType: .member("\(service.namespacedGeneratedName)ClientProtocol")
281+
)
268282
)
269283
}
270284

271285
private func makeClientStructTypealias(
272286
for service: CodeGenerationRequest.ServiceDescriptor
273287
) -> Declaration {
274-
return .typealias(
275-
accessModifier: self.accessModifier,
276-
name: "Client",
277-
existingType: .member("\(service.namespacedGeneratedName)Client")
288+
return .guarded(
289+
self.availabilityGuard,
290+
.typealias(
291+
accessModifier: self.accessModifier,
292+
name: "Client",
293+
existingType: .member("\(service.namespacedGeneratedName)Client")
294+
)
278295
)
279296
}
280297
}

Tests/GRPCCodeGenTests/Internal/Translator/ClientCodeTranslatorSnippetBasedTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
4343
let expectedSwift =
4444
"""
4545
/// Documentation for ServiceA
46+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4647
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
4748
/// Documentation for MethodA
4849
func methodA<R>(
@@ -52,6 +53,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
5253
_ body: @Sendable @escaping (ClientResponse.Single<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
5354
) async throws -> R where R: Sendable
5455
}
56+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
5557
extension NamespaceA.ServiceA.ClientProtocol {
5658
public func methodA<R>(
5759
request: ClientRequest.Single<NamespaceA.ServiceA.Method.MethodA.Input>,
@@ -117,6 +119,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
117119
let expectedSwift =
118120
"""
119121
/// Documentation for ServiceA
122+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
120123
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
121124
/// Documentation for MethodA
122125
func methodA<R>(
@@ -126,6 +129,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
126129
_ body: @Sendable @escaping (ClientResponse.Single<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
127130
) async throws -> R where R: Sendable
128131
}
132+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
129133
extension NamespaceA.ServiceA.ClientProtocol {
130134
public func methodA<R>(
131135
request: ClientRequest.Stream<NamespaceA.ServiceA.Method.MethodA.Input>,
@@ -191,6 +195,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
191195
let expectedSwift =
192196
"""
193197
/// Documentation for ServiceA
198+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
194199
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
195200
/// Documentation for MethodA
196201
func methodA<R>(
@@ -200,6 +205,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
200205
_ body: @Sendable @escaping (ClientResponse.Stream<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
201206
) async throws -> R where R: Sendable
202207
}
208+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
203209
extension NamespaceA.ServiceA.ClientProtocol {
204210
public func methodA<R>(
205211
request: ClientRequest.Single<NamespaceA.ServiceA.Method.MethodA.Input>,
@@ -265,6 +271,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
265271
let expectedSwift =
266272
"""
267273
/// Documentation for ServiceA
274+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
268275
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
269276
/// Documentation for MethodA
270277
func methodA<R>(
@@ -274,6 +281,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
274281
_ body: @Sendable @escaping (ClientResponse.Stream<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
275282
) async throws -> R where R: Sendable
276283
}
284+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
277285
extension NamespaceA.ServiceA.ClientProtocol {
278286
public func methodA<R>(
279287
request: ClientRequest.Stream<NamespaceA.ServiceA.Method.MethodA.Input>,
@@ -347,6 +355,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
347355
let expectedSwift =
348356
"""
349357
/// Documentation for ServiceA
358+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
350359
package protocol NamespaceA_ServiceAClientProtocol: Sendable {
351360
/// Documentation for MethodA
352361
func methodA<R>(
@@ -364,6 +373,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
364373
_ body: @Sendable @escaping (ClientResponse.Stream<NamespaceA.ServiceA.Method.MethodB.Output>) async throws -> R
365374
) async throws -> R where R: Sendable
366375
}
376+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
367377
extension NamespaceA.ServiceA.ClientProtocol {
368378
package func methodA<R>(
369379
request: ClientRequest.Stream<NamespaceA.ServiceA.Method.MethodA.Input>,
@@ -457,6 +467,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
457467
let expectedSwift =
458468
"""
459469
/// Documentation for ServiceA
470+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
460471
internal protocol ServiceAClientProtocol: Sendable {
461472
/// Documentation for MethodA
462473
func methodA<R>(
@@ -466,6 +477,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
466477
_ body: @Sendable @escaping (ClientResponse.Single<ServiceA.Method.MethodA.Output>) async throws -> R
467478
) async throws -> R where R: Sendable
468479
}
480+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
469481
extension ServiceA.ClientProtocol {
470482
internal func methodA<R>(
471483
request: ClientRequest.Single<ServiceA.Method.MethodA.Input>,
@@ -537,7 +549,9 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
537549
let expectedSwift =
538550
"""
539551
/// Documentation for ServiceA
552+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
540553
public protocol NamespaceA_ServiceAClientProtocol: Sendable {}
554+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
541555
extension NamespaceA.ServiceA.ClientProtocol {
542556
}
543557
/// Documentation for ServiceA
@@ -552,7 +566,9 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
552566
/// Documentation for ServiceB
553567
///
554568
/// Line 2
569+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
555570
public protocol ServiceBClientProtocol: Sendable {}
571+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
556572
extension ServiceB.ClientProtocol {
557573
}
558574
/// Documentation for ServiceB

Tests/GRPCCodeGenTests/Internal/Translator/IDLToStructuredSwiftTranslatorSnippetBasedTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
175175
public enum Method {
176176
public static let descriptors: [MethodDescriptor] = []
177177
}
178+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
178179
public typealias StreamingServiceProtocol = NamespaceA_ServiceAStreamingServiceProtocol
180+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
179181
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
180182
}
181183
}
@@ -185,15 +187,18 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
185187
public protocol NamespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {}
186188
187189
/// Conformance to `GRPCCore.RegistrableRPCService`.
190+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
188191
extension NamespaceA.ServiceA.StreamingServiceProtocol {
189192
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
190193
public func registerMethods(with router: inout GRPCCore.RPCRouter) {}
191194
}
192195
193196
/// Documentation for AService
197+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
194198
public protocol NamespaceA_ServiceAServiceProtocol: NamespaceA.ServiceA.StreamingServiceProtocol {}
195199
196200
/// Partial conformance to `NamespaceA_ServiceAStreamingServiceProtocol`.
201+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
197202
extension NamespaceA.ServiceA.ServiceProtocol {
198203
}
199204
"""

0 commit comments

Comments
 (0)