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:
 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.
 ${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} \
 		--grpc-swift_opt=Client=false \
 		--grpc-swift_out=$(dir $<)
-		
+
 # Generates protobufs and gRPC server for the Reflection Service
 .PHONY:
 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 $<) \
 		--plugin=${PROTOC_GEN_SWIFT} \
 		--swift_out=$(dir ${TEST_REFLECTION_V1ALPHA_PB})
-		
+
 # Generates protobufs and gRPC clients for the Reflection Service Tests
 .PHONY:
 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}
 	protoc $< \
@@ -204,11 +204,11 @@ ${HELLOWORLD_SERIALIZED_PROTO_GRPC}: ${HELLOWORLD_PROTO} ${PROTOC_GEN_GRPC_SWIFT
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
 		--grpc-swift_out=$(dir ${HELLOWORLD_SERIALIZED_PROTO_GRPC})
-    
+
 .PHONY:
 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}
 	protoc $< \
@@ -216,7 +216,7 @@ ${ECHO_SERIALIZED_PROTO_GRPC}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
 		--plugin=${PROTOC_GEN_GRPC_SWIFT} \
 		--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
 		--grpc-swift_out=$(dir ${ECHO_SERIALIZED_PROTO_GRPC})
-    
+
 .PHONY:
 generate-echo-reflection-data: ${ECHO_SERIALIZED_PROTO_GRPC}
 

+ 1 - 1
Package.swift

@@ -230,7 +230,7 @@ extension Target {
     ),
     exclude: [
       "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
       let greeterURL = Bundle.module.url(
         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 {
       print("The resource could not be loaded.")
       throw ExitCode.failure

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

@@ -1,21 +1,21 @@
 # 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]
- and it supports the "Greeter", "Echo", and "Reflection" services. 
+ and it supports the "Greeter", "Echo", and "Reflection" services.
 
 
 ## 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.
 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,
 - describe services and their methods,
 - 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.
 
-To initialise the Reflection service we will use 
+To initialise the Reflection service we will use
 ``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.
 
 ### 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`,
 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.
 
 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:
-- 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].
 - 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.
@@ -61,18 +61,18 @@ Let's break the command down:
   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.
-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.
 
 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.
 
-### 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`.
 
 Depending on the version of [gRPCurl][grpcurl] you are using you might need to use the `.v1alpha` instead.
@@ -84,9 +84,14 @@ reflection.
 guard
   let greeterURL = Bundle.module.url(
     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 {
   print("The resource could not be loaded.")
   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,
 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`.
 
 

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

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

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

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

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