Browse Source

Drop the '.txt' extension from reflection data (#1731)

Motivation:

Generated reflection data has a ".txt" extension. This isn't quite right
as the contents is base64 encdoded data. Moreover the generated
reflection data is intended as an implementation detail for the
reflection service.

Modifications:

- Change the generated reflection data extension from
  ".grpc.reflection.txt" to ".grpc.reflection".
- Fix a bug in the reflection server example where resources couldn't be
  loaded because no subdirectory was provided

Result:

Generated reflection data files don't have a ".txt" extension
George Barnett 2 years ago
parent
commit
a7b4240192

+ 7 - 7
Makefile

@@ -117,7 +117,7 @@ ${NORMALIZATION_GRPC}: ${NORMALIZATION_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 .PHONY:
 .PHONY:
 generate-normalization: ${NORMALIZATION_PB} ${NORMALIZATION_GRPC}
 generate-normalization: ${NORMALIZATION_PB} ${NORMALIZATION_GRPC}
 
 
-SERIALIZATION_GRPC_REFLECTION=Tests/GRPCTests/Codegen/Serialization/echo.grpc.reflection.txt
+SERIALIZATION_GRPC_REFLECTION=Tests/GRPCTests/Codegen/Serialization/echo.grpc.reflection
 
 
 # For serialization we'll set the ReflectionData option to true.
 # For serialization we'll set the ReflectionData option to true.
 ${SERIALIZATION_GRPC_REFLECTION}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 ${SERIALIZATION_GRPC_REFLECTION}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
@@ -154,7 +154,7 @@ ${REFLECTION_V1ALPHA_GRPC}: ${REFLECTION_V1ALPHA_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--grpc-swift_opt=Client=false \
 		--grpc-swift_opt=Client=false \
 		--grpc-swift_out=$(dir $<)
 		--grpc-swift_out=$(dir $<)
-		
+
 # Generates protobufs and gRPC server for the Reflection Service
 # Generates protobufs and gRPC server for the Reflection Service
 .PHONY:
 .PHONY:
 generate-reflection: ${REFLECTION_V1_PB} ${REFLECTION_V1_GRPC} ${REFLECTION_V1ALPHA_PB} ${REFLECTION_V1ALPHA_GRPC}
 generate-reflection: ${REFLECTION_V1_PB} ${REFLECTION_V1_GRPC} ${REFLECTION_V1ALPHA_PB} ${REFLECTION_V1ALPHA_GRPC}
@@ -191,12 +191,12 @@ ${TEST_REFLECTION_V1ALPHA_PB}: ${REFLECTION_V1ALPHA_PROTO} ${PROTOC_GEN_SWIFT}
 		--proto_path=$(dir $<) \
 		--proto_path=$(dir $<) \
 		--plugin=${PROTOC_GEN_SWIFT} \
 		--plugin=${PROTOC_GEN_SWIFT} \
 		--swift_out=$(dir ${TEST_REFLECTION_V1ALPHA_PB})
 		--swift_out=$(dir ${TEST_REFLECTION_V1ALPHA_PB})
-		
+
 # Generates protobufs and gRPC clients for the Reflection Service Tests
 # Generates protobufs and gRPC clients for the Reflection Service Tests
 .PHONY:
 .PHONY:
 generate-reflection-test-clients: ${TEST_REFLECTION_V1_PB} ${TEST_REFLECTION_V1_GRPC} ${TEST_REFLECTION_V1ALPHA_PB} ${TEST_REFLECTION_V1ALPHA_GRPC}
 generate-reflection-test-clients: ${TEST_REFLECTION_V1_PB} ${TEST_REFLECTION_V1_GRPC} ${TEST_REFLECTION_V1ALPHA_PB} ${TEST_REFLECTION_V1ALPHA_GRPC}
 
 
-HELLOWORLD_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/helloworld.grpc.reflection.txt
+HELLOWORLD_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/helloworld.grpc.reflection
 
 
 ${HELLOWORLD_SERIALIZED_PROTO_GRPC}: ${HELLOWORLD_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 ${HELLOWORLD_SERIALIZED_PROTO_GRPC}: ${HELLOWORLD_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 	protoc $< \
 	protoc $< \
@@ -204,11 +204,11 @@ ${HELLOWORLD_SERIALIZED_PROTO_GRPC}: ${HELLOWORLD_PROTO} ${PROTOC_GEN_GRPC_SWIFT
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
 		--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
 		--grpc-swift_out=$(dir ${HELLOWORLD_SERIALIZED_PROTO_GRPC})
 		--grpc-swift_out=$(dir ${HELLOWORLD_SERIALIZED_PROTO_GRPC})
-    
+
 .PHONY:
 .PHONY:
 generate-helloworld-reflection-data: ${HELLOWORLD_SERIALIZED_PROTO_GRPC}
 generate-helloworld-reflection-data: ${HELLOWORLD_SERIALIZED_PROTO_GRPC}
 
 
-ECHO_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/echo.grpc.reflection.txt
+ECHO_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/echo.grpc.reflection
 
 
 ${ECHO_SERIALIZED_PROTO_GRPC}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 ${ECHO_SERIALIZED_PROTO_GRPC}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 	protoc $< \
 	protoc $< \
@@ -216,7 +216,7 @@ ${ECHO_SERIALIZED_PROTO_GRPC}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
 		--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
 		--grpc-swift_out=$(dir ${ECHO_SERIALIZED_PROTO_GRPC})
 		--grpc-swift_out=$(dir ${ECHO_SERIALIZED_PROTO_GRPC})
-    
+
 .PHONY:
 .PHONY:
 generate-echo-reflection-data: ${ECHO_SERIALIZED_PROTO_GRPC}
 generate-echo-reflection-data: ${ECHO_SERIALIZED_PROTO_GRPC}
 
 

+ 1 - 1
Package.swift

@@ -230,7 +230,7 @@ extension Target {
     ),
     ),
     exclude: [
     exclude: [
       "Codegen/Normalization/normalization.proto",
       "Codegen/Normalization/normalization.proto",
-      "Codegen/Serialization/echo.grpc.reflection.txt",
+      "Codegen/Serialization/echo.grpc.reflection",
     ]
     ]
   )
   )
 
 

+ 0 - 0
Sources/Examples/ReflectionService/Generated/echo.grpc.reflection.txt → Sources/Examples/ReflectionService/Generated/echo.grpc.reflection


+ 0 - 0
Sources/Examples/ReflectionService/Generated/helloworld.grpc.reflection.txt → Sources/Examples/ReflectionService/Generated/helloworld.grpc.reflection


+ 7 - 2
Sources/Examples/ReflectionService/ReflectionServer.swift

@@ -31,9 +31,14 @@ struct ReflectionServer: AsyncParsableCommand {
     guard
     guard
       let greeterURL = Bundle.module.url(
       let greeterURL = Bundle.module.url(
         forResource: "helloworld",
         forResource: "helloworld",
-        withExtension: "grpc.reflection.txt"
+        withExtension: "grpc.reflection",
+        subdirectory: "Generated"
       ),
       ),
-      let echoURL = Bundle.module.url(forResource: "echo", withExtension: "grpc.reflection.txt")
+      let echoURL = Bundle.module.url(
+        forResource: "echo",
+        withExtension: "grpc.reflection",
+        subdirectory: "Generated"
+      )
     else {
     else {
       print("The resource could not be loaded.")
       print("The resource could not be loaded.")
       throw ExitCode.failure
       throw ExitCode.failure

+ 25 - 20
Sources/GRPCReflectionService/Documentation.docc/ReflectionServiceTutorial.md

@@ -1,21 +1,21 @@
 # Reflection service
 # Reflection service
 
 
-This tutorial goes through the steps of adding Reflection service to a 
-server, running it and testing it using gRPCurl. 
+This tutorial goes through the steps of adding Reflection service to a
+server, running it and testing it using gRPCurl.
 
 
- The server used in this example is implemented at 
+ The server used in this example is implemented at
  [Sources/Examples/ReflectionService/ReflectionServer.swift][reflection-server]
  [Sources/Examples/ReflectionService/ReflectionServer.swift][reflection-server]
- and it supports the "Greeter", "Echo", and "Reflection" services. 
+ and it supports the "Greeter", "Echo", and "Reflection" services.
 
 
 
 
 ## Overview
 ## Overview
 
 
-The Reflection service provides information about the public RPCs served by a server. 
+The Reflection service provides information about the public RPCs served by a server.
 It is specific to services defined using the Protocol Buffers IDL.
 It is specific to services defined using the Protocol Buffers IDL.
 By calling the Reflection service, clients can construct and send requests to services
 By calling the Reflection service, clients can construct and send requests to services
-without needing to generate code and types for them. 
+without needing to generate code and types for them.
 
 
-You can also use CLI clients such as [gRPCurl][grpcurl-setup] and the [gRPC command line tool][grpc-cli] to: 
+You can also use CLI clients such as [gRPCurl][grpcurl-setup] and the [gRPC command line tool][grpc-cli] to:
 - list services,
 - list services,
 - describe services and their methods,
 - describe services and their methods,
 - describe symbols,
 - describe symbols,
@@ -28,9 +28,9 @@ gRPC Swift supports both [v1][v1] and [v1alpha][v1alpha] of the reflection servi
 
 
 You can use the Reflection service by adding it as a provider when constructing your server.
 You can use the Reflection service by adding it as a provider when constructing your server.
 
 
-To initialise the Reflection service we will use 
+To initialise the Reflection service we will use
 ``GRPCReflectionService/ReflectionService/init(reflectionDataFileURLs:version:)``.
 ``GRPCReflectionService/ReflectionService/init(reflectionDataFileURLs:version:)``.
-It receives the URLs of the files containing the reflection data of the proto files 
+It receives the URLs of the files containing the reflection data of the proto files
 describing the services of the server and the version of the reflection service.
 describing the services of the server and the version of the reflection service.
 
 
 ### Generating the reflection data
 ### Generating the reflection data
@@ -38,7 +38,7 @@ describing the services of the server and the version of the reflection service.
 The server from this example uses the `GreeterProvider` and the `EchoProvider`,
 The server from this example uses the `GreeterProvider` and the `EchoProvider`,
 besides the `ReflectionService`.
 besides the `ReflectionService`.
 
 
-The associated proto files are located at `Sources/Examples/HelloWorld/Model/helloworld.proto`, and 
+The associated proto files are located at `Sources/Examples/HelloWorld/Model/helloworld.proto`, and
 `Sources/Examples/Echo/Model/echo.proto` respectively.
 `Sources/Examples/Echo/Model/echo.proto` respectively.
 
 
 In order to generate the reflection data for the `helloworld.proto`, you can run the following command:
 In order to generate the reflection data for the `helloworld.proto`, you can run the following command:
@@ -51,8 +51,8 @@ $ protoc Sources/Examples/HelloWorld/Model/helloworld.proto \
 ```
 ```
 
 
 Let's break the command down:
 Let's break the command down:
-- The first argument passed to `protoc` is the path 
-  to the `.proto` file to generate reflection data 
+- The first argument passed to `protoc` is the path
+  to the `.proto` file to generate reflection data
   for: [`Sources/Examples/HelloWorld/Model/helloworld.proto`][helloworld-proto].
   for: [`Sources/Examples/HelloWorld/Model/helloworld.proto`][helloworld-proto].
 - The `proto_path` flag is the path to search for imports: `Sources/Examples/HelloWorld/Model`.
 - The `proto_path` flag is the path to search for imports: `Sources/Examples/HelloWorld/Model`.
 - The 'grpc-swift_opt' flag allows us to list options for the Swift generator.
 - The 'grpc-swift_opt' flag allows us to list options for the Swift generator.
@@ -61,18 +61,18 @@ Let's break the command down:
   where the generated file will be located: `Sources/Examples/ReflectionService/Generated`.
   where the generated file will be located: `Sources/Examples/ReflectionService/Generated`.
 
 
 This command assumes that the `protoc-gen-grpc-swift` plugin is in your `$PATH` environment variable.
 This command assumes that the `protoc-gen-grpc-swift` plugin is in your `$PATH` environment variable.
-You can learn how to get the plugin from this section of the `grpc-swift` README: 
+You can learn how to get the plugin from this section of the `grpc-swift` README:
 https://github.com/grpc/grpc-swift#getting-the-protoc-plugins.
 https://github.com/grpc/grpc-swift#getting-the-protoc-plugins.
 
 
 The command for generating the reflection data for the `Echo` service is similar.
 The command for generating the reflection data for the `Echo` service is similar.
 
 
-You can use Swift Package Manager [resources][swiftpm-resources] to add the generated reflection data to your target. 
-In our example the reflection data is written into the "Generated" directory within the target 
+You can use Swift Package Manager [resources][swiftpm-resources] to add the generated reflection data to your target.
+In our example the reflection data is written into the "Generated" directory within the target
 so we include the `.copy("Generated")` rule in our target's resource list.
 so we include the `.copy("Generated")` rule in our target's resource list.
 
 
-### Instantiating the Reflection service 
+### Instantiating the Reflection service
 
 
-To instantiate the `ReflectionService` you need to pass the URLs of the files containing 
+To instantiate the `ReflectionService` you need to pass the URLs of the files containing
 the generated reflection data and the version to use, in our case `.v1`.
 the generated reflection data and the version to use, in our case `.v1`.
 
 
 Depending on the version of [gRPCurl][grpcurl] you are using you might need to use the `.v1alpha` instead.
 Depending on the version of [gRPCurl][grpcurl] you are using you might need to use the `.v1alpha` instead.
@@ -84,9 +84,14 @@ reflection.
 guard
 guard
   let greeterURL = Bundle.module.url(
   let greeterURL = Bundle.module.url(
     forResource: "helloworld",
     forResource: "helloworld",
-    withExtension: "grpc.reflection.txt"
+    withExtension: "grpc.reflection",
+    subdirectory: "Generated"
   ),
   ),
-  let echoURL = Bundle.module.url(forResource: "echo", withExtension: "grpc.reflection.txt")
+  let echoURL = Bundle.module.url(
+    forResource: "echo",
+    withExtension: "grpc.reflection",
+    subdirectory: "Generated"
+  )
 else {
 else {
   print("The resource could not be loaded.")
   print("The resource could not be loaded.")
   throw ExitCode.failure
   throw ExitCode.failure
@@ -123,7 +128,7 @@ Please follow the instructions from the [gRPCurl README][grpcurl-setup] to set u
 From a different terminal than the one used for running the server, we will call gRPCurl commands,
 From a different terminal than the one used for running the server, we will call gRPCurl commands,
 following the format: `grpcurl [flags] [address] [list|describe] [symbol]`.
 following the format: `grpcurl [flags] [address] [list|describe] [symbol]`.
 
 
-We use the `-plaintext` flag, because the server isn't configured with TLS, and 
+We use the `-plaintext` flag, because the server isn't configured with TLS, and
 the address is set to `localhost:1234`.
 the address is set to `localhost:1234`.
 
 
 
 

+ 2 - 2
Sources/protoc-gen-grpc-swift/main.swift

@@ -144,11 +144,11 @@ func main(args: [String]) throws {
       if options.generateReflectionData {
       if options.generateReflectionData {
         var binaryFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
         var binaryFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
         let binaryFileName = uniqueOutputFileName(
         let binaryFileName = uniqueOutputFileName(
-          component: "grpc.reflection",
+          component: "grpc",
           fileDescriptor: fileDescriptor,
           fileDescriptor: fileDescriptor,
           fileNamingOption: options.fileNaming,
           fileNamingOption: options.fileNaming,
           generatedFiles: &generatedFiles,
           generatedFiles: &generatedFiles,
-          extension: "txt"
+          extension: "reflection"
         )
         )
         let serializedFileDescriptorProto = try fileDescriptor.proto.serializedData()
         let serializedFileDescriptorProto = try fileDescriptor.proto.serializedData()
           .base64EncodedString()
           .base64EncodedString()

+ 1 - 1
Tests/GRPCTests/Codegen/Serialization/SerializationTests.swift

@@ -25,7 +25,7 @@ final class SerializationTests: GRPCTestCase {
   override func setUp() {
   override func setUp() {
     super.setUp()
     super.setUp()
     let binaryFileURL = URL(fileURLWithPath: #filePath)
     let binaryFileURL = URL(fileURLWithPath: #filePath)
-      .deletingLastPathComponent().appendingPathComponent("echo.grpc.reflection.txt")
+      .deletingLastPathComponent().appendingPathComponent("echo.grpc.reflection")
     let base64EncodedData = try! Data(contentsOf: binaryFileURL)
     let base64EncodedData = try! Data(contentsOf: binaryFileURL)
     let binaryData = Data(base64Encoded: base64EncodedData)!
     let binaryData = Data(base64Encoded: base64EncodedData)!
     self
     self

+ 0 - 0
Tests/GRPCTests/Codegen/Serialization/echo.grpc.reflection.txt → Tests/GRPCTests/Codegen/Serialization/echo.grpc.reflection