Skip to content

Commit

Permalink
Drop the '.txt' extension from reflection data (#1731)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
glbrntt authored Dec 4, 2023
1 parent 2e1114a commit a7b4240
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 33 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -191,32 +191,32 @@ ${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 $< \
--proto_path=$(dir $<) \
--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 $< \
--proto_path=$(dir $<) \
--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}

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ extension Target {
),
exclude: [
"Codegen/Normalization/normalization.proto",
"Codegen/Serialization/echo.grpc.reflection.txt",
"Codegen/Serialization/echo.grpc.reflection",
]
)

Expand Down
9 changes: 7 additions & 2 deletions Sources/Examples/ReflectionService/ReflectionServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,17 +28,17 @@ 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

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:
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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`.


Expand Down
4 changes: 2 additions & 2 deletions Sources/protoc-gen-grpc-swift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a7b4240

Please sign in to comment.