Skip to content

Commit a7b4240

Browse files
authored
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
1 parent 2e1114a commit a7b4240

File tree

9 files changed

+43
-33
lines changed

9 files changed

+43
-33
lines changed

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ ${NORMALIZATION_GRPC}: ${NORMALIZATION_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
117117
.PHONY:
118118
generate-normalization: ${NORMALIZATION_PB} ${NORMALIZATION_GRPC}
119119

120-
SERIALIZATION_GRPC_REFLECTION=Tests/GRPCTests/Codegen/Serialization/echo.grpc.reflection.txt
120+
SERIALIZATION_GRPC_REFLECTION=Tests/GRPCTests/Codegen/Serialization/echo.grpc.reflection
121121

122122
# For serialization we'll set the ReflectionData option to true.
123123
${SERIALIZATION_GRPC_REFLECTION}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
@@ -154,7 +154,7 @@ ${REFLECTION_V1ALPHA_GRPC}: ${REFLECTION_V1ALPHA_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
154154
--plugin=${PROTOC_GEN_GRPC_SWIFT} \
155155
--grpc-swift_opt=Client=false \
156156
--grpc-swift_out=$(dir $<)
157-
157+
158158
# Generates protobufs and gRPC server for the Reflection Service
159159
.PHONY:
160160
generate-reflection: ${REFLECTION_V1_PB} ${REFLECTION_V1_GRPC} ${REFLECTION_V1ALPHA_PB} ${REFLECTION_V1ALPHA_GRPC}
@@ -191,32 +191,32 @@ ${TEST_REFLECTION_V1ALPHA_PB}: ${REFLECTION_V1ALPHA_PROTO} ${PROTOC_GEN_SWIFT}
191191
--proto_path=$(dir $<) \
192192
--plugin=${PROTOC_GEN_SWIFT} \
193193
--swift_out=$(dir ${TEST_REFLECTION_V1ALPHA_PB})
194-
194+
195195
# Generates protobufs and gRPC clients for the Reflection Service Tests
196196
.PHONY:
197197
generate-reflection-test-clients: ${TEST_REFLECTION_V1_PB} ${TEST_REFLECTION_V1_GRPC} ${TEST_REFLECTION_V1ALPHA_PB} ${TEST_REFLECTION_V1ALPHA_GRPC}
198198

199-
HELLOWORLD_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/helloworld.grpc.reflection.txt
199+
HELLOWORLD_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/helloworld.grpc.reflection
200200

201201
${HELLOWORLD_SERIALIZED_PROTO_GRPC}: ${HELLOWORLD_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
202202
protoc $< \
203203
--proto_path=$(dir $<) \
204204
--plugin=${PROTOC_GEN_GRPC_SWIFT} \
205205
--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
206206
--grpc-swift_out=$(dir ${HELLOWORLD_SERIALIZED_PROTO_GRPC})
207-
207+
208208
.PHONY:
209209
generate-helloworld-reflection-data: ${HELLOWORLD_SERIALIZED_PROTO_GRPC}
210210

211-
ECHO_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/echo.grpc.reflection.txt
211+
ECHO_SERIALIZED_PROTO_GRPC=Sources/Examples/ReflectionService/Generated/echo.grpc.reflection
212212

213213
${ECHO_SERIALIZED_PROTO_GRPC}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
214214
protoc $< \
215215
--proto_path=$(dir $<) \
216216
--plugin=${PROTOC_GEN_GRPC_SWIFT} \
217217
--grpc-swift_opt=Client=false,Server=false,ReflectionData=true \
218218
--grpc-swift_out=$(dir ${ECHO_SERIALIZED_PROTO_GRPC})
219-
219+
220220
.PHONY:
221221
generate-echo-reflection-data: ${ECHO_SERIALIZED_PROTO_GRPC}
222222

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ extension Target {
230230
),
231231
exclude: [
232232
"Codegen/Normalization/normalization.proto",
233-
"Codegen/Serialization/echo.grpc.reflection.txt",
233+
"Codegen/Serialization/echo.grpc.reflection",
234234
]
235235
)
236236

Sources/Examples/ReflectionService/ReflectionServer.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ struct ReflectionServer: AsyncParsableCommand {
3131
guard
3232
let greeterURL = Bundle.module.url(
3333
forResource: "helloworld",
34-
withExtension: "grpc.reflection.txt"
34+
withExtension: "grpc.reflection",
35+
subdirectory: "Generated"
3536
),
36-
let echoURL = Bundle.module.url(forResource: "echo", withExtension: "grpc.reflection.txt")
37+
let echoURL = Bundle.module.url(
38+
forResource: "echo",
39+
withExtension: "grpc.reflection",
40+
subdirectory: "Generated"
41+
)
3742
else {
3843
print("The resource could not be loaded.")
3944
throw ExitCode.failure

Sources/GRPCReflectionService/Documentation.docc/ReflectionServiceTutorial.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Reflection service
22

3-
This tutorial goes through the steps of adding Reflection service to a
4-
server, running it and testing it using gRPCurl.
3+
This tutorial goes through the steps of adding Reflection service to a
4+
server, running it and testing it using gRPCurl.
55

6-
The server used in this example is implemented at
6+
The server used in this example is implemented at
77
[Sources/Examples/ReflectionService/ReflectionServer.swift][reflection-server]
8-
and it supports the "Greeter", "Echo", and "Reflection" services.
8+
and it supports the "Greeter", "Echo", and "Reflection" services.
99

1010

1111
## Overview
1212

13-
The Reflection service provides information about the public RPCs served by a server.
13+
The Reflection service provides information about the public RPCs served by a server.
1414
It is specific to services defined using the Protocol Buffers IDL.
1515
By calling the Reflection service, clients can construct and send requests to services
16-
without needing to generate code and types for them.
16+
without needing to generate code and types for them.
1717

18-
You can also use CLI clients such as [gRPCurl][grpcurl-setup] and the [gRPC command line tool][grpc-cli] to:
18+
You can also use CLI clients such as [gRPCurl][grpcurl-setup] and the [gRPC command line tool][grpc-cli] to:
1919
- list services,
2020
- describe services and their methods,
2121
- describe symbols,
@@ -28,17 +28,17 @@ gRPC Swift supports both [v1][v1] and [v1alpha][v1alpha] of the reflection servi
2828

2929
You can use the Reflection service by adding it as a provider when constructing your server.
3030

31-
To initialise the Reflection service we will use
31+
To initialise the Reflection service we will use
3232
``GRPCReflectionService/ReflectionService/init(reflectionDataFileURLs:version:)``.
33-
It receives the URLs of the files containing the reflection data of the proto files
33+
It receives the URLs of the files containing the reflection data of the proto files
3434
describing the services of the server and the version of the reflection service.
3535

3636
### Generating the reflection data
3737

3838
The server from this example uses the `GreeterProvider` and the `EchoProvider`,
3939
besides the `ReflectionService`.
4040

41-
The associated proto files are located at `Sources/Examples/HelloWorld/Model/helloworld.proto`, and
41+
The associated proto files are located at `Sources/Examples/HelloWorld/Model/helloworld.proto`, and
4242
`Sources/Examples/Echo/Model/echo.proto` respectively.
4343

4444
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 \
5151
```
5252

5353
Let's break the command down:
54-
- The first argument passed to `protoc` is the path
55-
to the `.proto` file to generate reflection data
54+
- The first argument passed to `protoc` is the path
55+
to the `.proto` file to generate reflection data
5656
for: [`Sources/Examples/HelloWorld/Model/helloworld.proto`][helloworld-proto].
5757
- The `proto_path` flag is the path to search for imports: `Sources/Examples/HelloWorld/Model`.
5858
- The 'grpc-swift_opt' flag allows us to list options for the Swift generator.
@@ -61,18 +61,18 @@ Let's break the command down:
6161
where the generated file will be located: `Sources/Examples/ReflectionService/Generated`.
6262

6363
This command assumes that the `protoc-gen-grpc-swift` plugin is in your `$PATH` environment variable.
64-
You can learn how to get the plugin from this section of the `grpc-swift` README:
64+
You can learn how to get the plugin from this section of the `grpc-swift` README:
6565
https://github.com/grpc/grpc-swift#getting-the-protoc-plugins.
6666

6767
The command for generating the reflection data for the `Echo` service is similar.
6868

69-
You can use Swift Package Manager [resources][swiftpm-resources] to add the generated reflection data to your target.
70-
In our example the reflection data is written into the "Generated" directory within the target
69+
You can use Swift Package Manager [resources][swiftpm-resources] to add the generated reflection data to your target.
70+
In our example the reflection data is written into the "Generated" directory within the target
7171
so we include the `.copy("Generated")` rule in our target's resource list.
7272

73-
### Instantiating the Reflection service
73+
### Instantiating the Reflection service
7474

75-
To instantiate the `ReflectionService` you need to pass the URLs of the files containing
75+
To instantiate the `ReflectionService` you need to pass the URLs of the files containing
7676
the generated reflection data and the version to use, in our case `.v1`.
7777

7878
Depending on the version of [gRPCurl][grpcurl] you are using you might need to use the `.v1alpha` instead.
@@ -84,9 +84,14 @@ reflection.
8484
guard
8585
let greeterURL = Bundle.module.url(
8686
forResource: "helloworld",
87-
withExtension: "grpc.reflection.txt"
87+
withExtension: "grpc.reflection",
88+
subdirectory: "Generated"
8889
),
89-
let echoURL = Bundle.module.url(forResource: "echo", withExtension: "grpc.reflection.txt")
90+
let echoURL = Bundle.module.url(
91+
forResource: "echo",
92+
withExtension: "grpc.reflection",
93+
subdirectory: "Generated"
94+
)
9095
else {
9196
print("The resource could not be loaded.")
9297
throw ExitCode.failure
@@ -123,7 +128,7 @@ Please follow the instructions from the [gRPCurl README][grpcurl-setup] to set u
123128
From a different terminal than the one used for running the server, we will call gRPCurl commands,
124129
following the format: `grpcurl [flags] [address] [list|describe] [symbol]`.
125130

126-
We use the `-plaintext` flag, because the server isn't configured with TLS, and
131+
We use the `-plaintext` flag, because the server isn't configured with TLS, and
127132
the address is set to `localhost:1234`.
128133

129134

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ func main(args: [String]) throws {
144144
if options.generateReflectionData {
145145
var binaryFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
146146
let binaryFileName = uniqueOutputFileName(
147-
component: "grpc.reflection",
147+
component: "grpc",
148148
fileDescriptor: fileDescriptor,
149149
fileNamingOption: options.fileNaming,
150150
generatedFiles: &generatedFiles,
151-
extension: "txt"
151+
extension: "reflection"
152152
)
153153
let serializedFileDescriptorProto = try fileDescriptor.proto.serializedData()
154154
.base64EncodedString()

Tests/GRPCTests/Codegen/Serialization/SerializationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class SerializationTests: GRPCTestCase {
2525
override func setUp() {
2626
super.setUp()
2727
let binaryFileURL = URL(fileURLWithPath: #filePath)
28-
.deletingLastPathComponent().appendingPathComponent("echo.grpc.reflection.txt")
28+
.deletingLastPathComponent().appendingPathComponent("echo.grpc.reflection")
2929
let base64EncodedData = try! Data(contentsOf: binaryFileURL)
3030
let binaryData = Data(base64Encoded: base64EncodedData)!
3131
self

0 commit comments

Comments
 (0)