Browse Source

Update generated code to use service descriptors (#2004)

Motivation:

The generated code refers to services literally using strings. With the
addition of service descriptors, the generated code can now utilize them
to refer to services.

Modifications:

- Update the generated code to use service descriptors.
- For each service in the generated code, extend `ServiceDescriptor` to
include a `static` property that holds the service descriptor for that
service. (I.e. for a service `Foo` in the `bar` package, the service
descriptor for `Foo` can be accessed using `ServiceDescriptor.bar_Foo`.)

Result:

The generated code will utilize and provide easy access to service
descriptors.
Clinton Nkwocha 1 year ago
parent
commit
499a8208ee
1 changed files with 27 additions and 3 deletions
  1. 27 3
      Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

+ 27 - 3
Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

@@ -60,12 +60,13 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         import ExtraModule
 
         internal enum Hello_World_Greeter {
+            internal static let descriptor = ServiceDescriptor.hello_world_Greeter
             internal enum Method {
                 internal enum SayHello {
                     internal typealias Input = Hello_World_HelloRequest
                     internal typealias Output = Hello_World_HelloReply
                     internal static let descriptor = MethodDescriptor(
-                        service: "hello.world.Greeter",
+                        service: Hello_World_Greeter.descriptor.fullyQualifiedService,
                         method: "SayHello"
                     )
                 }
@@ -79,6 +80,13 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
             internal typealias Client = Hello_World_GreeterClient
         }
 
+        extension ServiceDescriptor {
+            internal static let hello_world_Greeter = Self(
+                package: "hello.world",
+                service: "Greeter"
+            )
+        }
+
         /// The greeting service definition.
         @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
         internal protocol Hello_World_GreeterClientProtocol: Sendable {
@@ -175,12 +183,13 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         import ExtraModule
 
         public enum Helloworld_Greeter {
+          public static let descriptor = ServiceDescriptor.helloworld_Greeter
           public enum Method {
             public enum SayHello {
               public typealias Input = Helloworld_HelloRequest
               public typealias Output = Helloworld_HelloReply
               public static let descriptor = MethodDescriptor(
-                service: "helloworld.Greeter",
+                service: Helloworld_Greeter.descriptor.fullyQualifiedService,
                 method: "SayHello"
               )
             }
@@ -194,6 +203,13 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
           public typealias ServiceProtocol = Helloworld_GreeterServiceProtocol
         }
 
+        extension ServiceDescriptor {
+          public static let helloworld_Greeter = Self(
+            package: "helloworld",
+            service: "Greeter"
+          )
+        }
+
         /// The greeting service definition.
         @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
         public protocol Helloworld_GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
@@ -270,12 +286,13 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         import ExtraModule
 
         package enum Greeter {
+          package static let descriptor = ServiceDescriptor.Greeter
           package enum Method {
             package enum SayHello {
               package typealias Input = HelloRequest
               package typealias Output = HelloReply
               package static let descriptor = MethodDescriptor(
-                service: "Greeter",
+                service: Greeter.descriptor.fullyQualifiedService,
                 method: "SayHello"
               )
             }
@@ -293,6 +310,13 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
           package typealias Client = GreeterClient
         }
 
+        extension ServiceDescriptor {
+          package static let Greeter = Self(
+            package: "",
+            service: "Greeter"
+          )
+        }
+
         /// The greeting service definition.
         @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
         package protocol GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {