Browse Source

[CodeGenLib] Add avialability gurads in generated code (#1791)

Motivation:

The protocols from GRPCCore that structs and protocols from the generated code conform to
can be used only for some OSes, so we need to add the same availability guards in the generated
code.

Modifications:

- created the AvailabilityDescription struct in StructuredSwiftRepresentation
- created the declaraion for a guarded declaration
- created the rendering functions for guarded declarations and the availability guard
- added the guarded declaration in the translators where needed
- changed the tests accordingly

Result:

The generated code will contain the necessary availability guards.
Stefana-Ioana Dranca 2 years ago
parent
commit
5f792b0503
1 changed files with 6 additions and 0 deletions
  1. 6 0
      Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

+ 6 - 0
Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

@@ -102,6 +102,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         }
 
         /// The greeting service definition.
+        @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
         internal struct Helloworld_GreeterClient: Helloworld.Greeter.ClientProtocol {
             private let client: GRPCCore.GRPCClient
             
@@ -181,6 +182,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         }
 
         /// The greeting service definition.
+        @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
         public protocol Helloworld_GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
           /// Sends a greeting.
           func sayHello(request: ServerRequest.Stream<Helloworld.Greeter.Method.SayHello.Input>) async throws -> ServerResponse.Stream<Helloworld.Greeter.Method.SayHello.Output>
@@ -188,6 +190,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
 
         /// Conformance to `GRPCCore.RegistrableRPCService`.
         extension Helloworld.Greeter.StreamingServiceProtocol {
+          @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
           public func registerMethods(with router: inout GRPCCore.RPCRouter) {
             router.registerHandler(
               forMethod: Helloworld.Greeter.Method.SayHello.descriptor,
@@ -270,6 +273,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         }
 
         /// The greeting service definition.
+        @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
         package protocol Helloworld_GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
           /// Sends a greeting.
           func sayHello(request: ServerRequest.Stream<Helloworld.Greeter.Method.SayHello.Input>) async throws -> ServerResponse.Stream<Helloworld.Greeter.Method.SayHello.Output>
@@ -277,6 +281,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
 
         /// Conformance to `GRPCCore.RegistrableRPCService`.
         extension Helloworld.Greeter.StreamingServiceProtocol {
+          @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
           package func registerMethods(with router: inout GRPCCore.RPCRouter) {
             router.registerHandler(
               forMethod: Helloworld.Greeter.Method.SayHello.descriptor,
@@ -329,6 +334,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         }
 
         /// The greeting service definition.
+        @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
         package struct Helloworld_GreeterClient: Helloworld.Greeter.ClientProtocol {
           private let client: GRPCCore.GRPCClient