Skip to content

Commit

Permalink
Generated code fixtures: availability guards and descriptor paths (#1797
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
stefanadranca authored Feb 7, 2024
1 parent 25c33fd commit 8d3341a
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/// a representation for the following generated code:
///
/// ```swift
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public protocol Foo_BarClientProtocol: Sendable {
/// func baz<R: Sendable>(
/// request: ClientRequest.Single<foo.Bar.Method.baz.Input>,
Expand All @@ -30,6 +31,7 @@
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Method.Baz.Output>) async throws -> R
/// ) async throws -> ServerResponse.Stream<foo.Bar.Method.Baz.Output>
/// }
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// extension Foo.Bar.ClientProtocol {
/// public func get<R: Sendable>(
/// request: ClientRequest.Single<Foo.Bar.Method.Baz.Input>,
Expand All @@ -42,6 +44,7 @@
/// body
/// )
/// }
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public struct foo_BarClient: foo.Bar.ClientProtocol {
/// private let client: GRPCCore.GRPCClient
/// public init(client: GRPCCore.GRPCClient) {
Expand Down Expand Up @@ -120,7 +123,7 @@ extension ClientCodeTranslator {
members: methods
)
)
return clientProtocol
return .guarded(self.availabilityGuard, clientProtocol)
}

private func makeExtensionProtocol(
Expand All @@ -142,7 +145,10 @@ extension ClientCodeTranslator {
declarations: methods
)
)
return clientProtocolExtension
return .guarded(
self.availabilityGuard,
clientProtocolExtension
)
}

private func makeClientProtocolMethod(
Expand Down
37 changes: 25 additions & 12 deletions Sources/GRPCCodeGen/Internal/Translator/ServerCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
/// a representation for the following generated code:
///
/// ```swift
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public protocol foo_BarServiceStreamingProtocol: GRPCCore.RegistrableRPCService {
/// func baz(
/// request: ServerRequest.Stream<foo.Method.baz.Input>
/// ) async throws -> ServerResponse.Stream<foo.Method.baz.Output>
/// }
/// // Generated conformance to `RegistrableRPCService`.
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// extension foo.Bar.StreamingServiceProtocol {
/// public func registerRPCs(with router: inout RPCRouter) {
/// router.registerHandler(
Expand All @@ -38,12 +40,14 @@
/// )
/// }
/// }
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public protocol foo_BarServiceProtocol: foo.Bar.StreamingServiceProtocol {
/// func baz(
/// request: ServerRequest.Single<foo.Bar.Method.baz.Input>
/// ) async throws -> ServerResponse.Single<foo.Bar.Method.baz.Output>
/// }
/// // Generated 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: ServerRequest.Stream<foo.Bar.Method.baz.Input>
Expand Down Expand Up @@ -168,9 +172,12 @@ extension ServerCodeTranslator {
) -> Declaration {
let streamingProtocol = self.protocolNameTypealias(service: service, streaming: true)
let registerRPCMethod = self.makeRegisterRPCsMethod(for: service, in: codeGenerationRequest)
return .extension(
onType: streamingProtocol,
declarations: [registerRPCMethod]
return .guarded(
self.availabilityGuard,
.extension(
onType: streamingProtocol,
declarations: [registerRPCMethod]
)
)
}

Expand Down Expand Up @@ -297,12 +304,15 @@ extension ServerCodeTranslator {

return .commentable(
.preFormatted(service.documentation),
.protocol(
ProtocolDescription(
accessModifier: self.accessModifier,
name: protocolName,
conformances: [streamingProtocol],
members: methods
.guarded(
self.availabilityGuard,
.protocol(
ProtocolDescription(
accessModifier: self.accessModifier,
name: protocolName,
conformances: [streamingProtocol],
members: methods
)
)
)
)
Expand Down Expand Up @@ -363,9 +373,12 @@ extension ServerCodeTranslator {
}

let protocolName = self.protocolNameTypealias(service: service, streaming: false)
return .extension(
onType: protocolName,
declarations: methods
return .guarded(
self.availabilityGuard,
.extension(
onType: protocolName,
declarations: methods
)
)
}

Expand Down
41 changes: 29 additions & 12 deletions Sources/GRPCCodeGen/Internal/Translator/TypealiasTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
/// // ...
///
/// public static let descriptors: [MethodDescriptor] = [
/// Echo.Echo.Method.Get.descriptor,
/// Echo.Echo.Method.Collect.descriptor,
/// Get.descriptor,
/// Collect.descriptor,
/// // ...
/// ]
/// }
///
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public typealias StreamingServiceProtocol = echo_EchoServiceStreamingProtocol
/// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
/// public typealias ServiceProtocol = echo_EchoServiceProtocol
///
/// }
Expand Down Expand Up @@ -223,7 +225,7 @@ extension TypealiasTranslator {
let methodDescriptorPath = Expression.memberAccess(
MemberAccessDescription(
left: .identifierType(
.member([service.namespacedTypealiasGeneratedName, "Method", methodName])
.member([methodName])
),
right: "descriptor"
)
Expand Down Expand Up @@ -255,26 +257,41 @@ extension TypealiasTranslator {
existingType: .member("\(service.namespacedGeneratedName)ServiceProtocol")
)

return [streamingServiceProtocolTypealias, serviceProtocolTypealias]
return [
.guarded(
self.availabilityGuard,
streamingServiceProtocolTypealias
),
.guarded(
self.availabilityGuard,
serviceProtocolTypealias
),
]
}

private func makeClientProtocolTypealias(
for service: CodeGenerationRequest.ServiceDescriptor
) -> Declaration {
return .typealias(
accessModifier: self.accessModifier,
name: "ClientProtocol",
existingType: .member("\(service.namespacedGeneratedName)ClientProtocol")
return .guarded(
self.availabilityGuard,
.typealias(
accessModifier: self.accessModifier,
name: "ClientProtocol",
existingType: .member("\(service.namespacedGeneratedName)ClientProtocol")
)
)
}

private func makeClientStructTypealias(
for service: CodeGenerationRequest.ServiceDescriptor
) -> Declaration {
return .typealias(
accessModifier: self.accessModifier,
name: "Client",
existingType: .member("\(service.namespacedGeneratedName)Client")
return .guarded(
self.availabilityGuard,
.typealias(
accessModifier: self.accessModifier,
name: "Client",
existingType: .member("\(service.namespacedGeneratedName)Client")
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
/// Documentation for MethodA
func methodA<R>(
Expand All @@ -52,6 +53,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
_ body: @Sendable @escaping (ClientResponse.Single<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
) async throws -> R where R: Sendable
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ClientProtocol {
public func methodA<R>(
request: ClientRequest.Single<NamespaceA.ServiceA.Method.MethodA.Input>,
Expand Down Expand Up @@ -117,6 +119,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
/// Documentation for MethodA
func methodA<R>(
Expand All @@ -126,6 +129,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
_ body: @Sendable @escaping (ClientResponse.Single<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
) async throws -> R where R: Sendable
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ClientProtocol {
public func methodA<R>(
request: ClientRequest.Stream<NamespaceA.ServiceA.Method.MethodA.Input>,
Expand Down Expand Up @@ -191,6 +195,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
/// Documentation for MethodA
func methodA<R>(
Expand All @@ -200,6 +205,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
_ body: @Sendable @escaping (ClientResponse.Stream<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
) async throws -> R where R: Sendable
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ClientProtocol {
public func methodA<R>(
request: ClientRequest.Single<NamespaceA.ServiceA.Method.MethodA.Input>,
Expand Down Expand Up @@ -265,6 +271,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol NamespaceA_ServiceAClientProtocol: Sendable {
/// Documentation for MethodA
func methodA<R>(
Expand All @@ -274,6 +281,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
_ body: @Sendable @escaping (ClientResponse.Stream<NamespaceA.ServiceA.Method.MethodA.Output>) async throws -> R
) async throws -> R where R: Sendable
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ClientProtocol {
public func methodA<R>(
request: ClientRequest.Stream<NamespaceA.ServiceA.Method.MethodA.Input>,
Expand Down Expand Up @@ -347,6 +355,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
package protocol NamespaceA_ServiceAClientProtocol: Sendable {
/// Documentation for MethodA
func methodA<R>(
Expand All @@ -364,6 +373,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
_ body: @Sendable @escaping (ClientResponse.Stream<NamespaceA.ServiceA.Method.MethodB.Output>) async throws -> R
) async throws -> R where R: Sendable
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ClientProtocol {
package func methodA<R>(
request: ClientRequest.Stream<NamespaceA.ServiceA.Method.MethodA.Input>,
Expand Down Expand Up @@ -457,6 +467,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
internal protocol ServiceAClientProtocol: Sendable {
/// Documentation for MethodA
func methodA<R>(
Expand All @@ -466,6 +477,7 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
_ body: @Sendable @escaping (ClientResponse.Single<ServiceA.Method.MethodA.Output>) async throws -> R
) async throws -> R where R: Sendable
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension ServiceA.ClientProtocol {
internal func methodA<R>(
request: ClientRequest.Single<ServiceA.Method.MethodA.Input>,
Expand Down Expand Up @@ -537,7 +549,9 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
let expectedSwift =
"""
/// Documentation for ServiceA
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol NamespaceA_ServiceAClientProtocol: Sendable {}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ClientProtocol {
}
/// Documentation for ServiceA
Expand All @@ -552,7 +566,9 @@ final class ClientCodeTranslatorSnippetBasedTests: XCTestCase {
/// Documentation for ServiceB
///
/// Line 2
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol ServiceBClientProtocol: Sendable {}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension ServiceB.ClientProtocol {
}
/// Documentation for ServiceB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
public enum Method {
public static let descriptors: [MethodDescriptor] = []
}
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public typealias StreamingServiceProtocol = NamespaceA_ServiceAStreamingServiceProtocol
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public typealias ServiceProtocol = NamespaceA_ServiceAServiceProtocol
}
}
Expand All @@ -185,15 +187,18 @@ final class IDLToStructuredSwiftTranslatorSnippetBasedTests: XCTestCase {
public protocol NamespaceA_ServiceAStreamingServiceProtocol: GRPCCore.RegistrableRPCService {}
/// Conformance to `GRPCCore.RegistrableRPCService`.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.StreamingServiceProtocol {
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public func registerMethods(with router: inout GRPCCore.RPCRouter) {}
}
/// Documentation for AService
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol NamespaceA_ServiceAServiceProtocol: NamespaceA.ServiceA.StreamingServiceProtocol {}
/// Partial conformance to `NamespaceA_ServiceAStreamingServiceProtocol`.
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension NamespaceA.ServiceA.ServiceProtocol {
}
"""
Expand Down
Loading

0 comments on commit 8d3341a

Please sign in to comment.