Skip to content

Commit

Permalink
Merge #43
Browse files Browse the repository at this point in the history
43: [protoc-gen-tonic] Add option to disable transport generation r=neoeinstein a=stan-irl

## Background
- #42
- Currently, transport is always generated for tonic grpc services which is a problem for consumers who want to compile for wasm. The latest version `tonic-build="0.8.4"` allows us to disable this 

## Changes
- Update tonic-build to v0.8.4
- Add `tonic_opt` option `no_transport`
- Migrate from deprecated `generate` APIs to `tonic_build::CodeGenBuilder`
- Update `prost-build` ti 0.11.4 in all crates
  - This was required for cargo crate resolution to succeed 
- Update README

## Tests
-[x] `cargo test` - everything is passing
-[x] Used this package to regenerate my own protos
  - [x] Confirmed that the `connect()` function wasnt generated
  - Just an FYI that there is now `#[allow(clippy::derive_partial_eq_without_eq)]` macros in the generated code 



Co-authored-by: Stan Tsouvallas <stan.tsouvallas@irl.com>
Co-authored-by: Marcus Griep <marcus@griep.us>
  • Loading branch information
3 people committed Feb 24, 2023
2 parents 92b203d + 93b2269 commit 83de843
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 62 deletions.
85 changes: 42 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ members = [
[profile.release]
codegen-units = 1
lto = "fat"
debug = true
debug = true
4 changes: 2 additions & 2 deletions protoc-gen-tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ prost = { version = "0.11.0", default-features = false, features = ["std"] }
protoc-gen-prost = { version = "0.2.0", path = "../protoc-gen-prost" }
regex = { version = "1.5.5", default-features = false }
syn = { version = "1.0.89", features = ["parsing", "full"] }
tonic-build = { version = "0.8", features = [] }
tonic-build = { version = "0.8.4", features = [] }

[profile.release]
codegen-units = 1
lto = "fat"
debug = true
debug = true
1 change: 1 addition & 0 deletions protoc-gen-tonic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ In addition, the following options can also be specified:

* `no_server(=<boolean>)`: Disables generation of the server modules
* `no_client(=<boolean>)`: Disables generation of the client modules
* `no_transport(=<boolean>)`: Disables generation of connect method using `tonic::transport::Channel`
* `no_include(=<boolean>)`: Skips adding an include into the file generated
by `protoc-gen-prost`. This behavior may be desired if this plugin is run
in a separate `protoc` invocation and you encounter a `Tried to insert into
Expand Down
27 changes: 13 additions & 14 deletions protoc-gen-tonic/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) struct TonicGenerator {
pub(crate) resolver: Resolver,
pub(crate) generate_server: bool,
pub(crate) generate_client: bool,
pub(crate) generate_transport: bool,
pub(crate) server_attributes: Attributes,
pub(crate) client_attributes: Attributes,
pub(crate) emit_package: bool,
Expand Down Expand Up @@ -46,22 +47,20 @@ impl TonicGenerator {
})
.flat_map(|service| {
let client = self.generate_client.then(|| {
tonic_build::client::generate(
&service,
self.emit_package,
PROTO_PATH,
self.resolver.compile_well_known_types(),
&self.client_attributes,
)
tonic_build::CodeGenBuilder::new()
.emit_package(self.emit_package)
.build_transport(self.generate_transport)
.compile_well_known_types(self.resolver.compile_well_known_types())
.attributes(self.client_attributes.clone())
.generate_client(&service, PROTO_PATH)
});
let server = self.generate_server.then(|| {
tonic_build::server::generate(
&service,
self.emit_package,
PROTO_PATH,
self.resolver.compile_well_known_types(),
&self.server_attributes,
)
tonic_build::CodeGenBuilder::new()
.emit_package(self.emit_package)
.build_transport(self.generate_transport)
.compile_well_known_types(self.resolver.compile_well_known_types())
.attributes(self.server_attributes.clone())
.generate_server(&service, PROTO_PATH)
});

client.into_iter().chain(server)
Expand Down
13 changes: 13 additions & 0 deletions protoc-gen-tonic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn execute(raw_request: &[u8]) -> protoc_gen_prost::Result {
resolver,
generate_server: !params.no_server,
generate_client: !params.no_client,
generate_transport: !params.no_transport,
server_attributes: params.server_attributes,
client_attributes: params.client_attributes,
emit_package: !params.disable_package_emission,
Expand All @@ -54,6 +55,7 @@ struct Parameters {
disable_package_emission: bool,
no_server: bool,
no_client: bool,
no_transport: bool,
no_include: bool,
}

Expand Down Expand Up @@ -115,6 +117,17 @@ impl str::FromStr for Parameters {
param: "no_client",
value: "false",
} => (),
Param::Parameter {
param: "no_transport",
}
| Param::Value {
param: "no_transport",
value: "true",
} => ret_val.no_transport = true,
Param::Value {
param: "no_transport",
value: "false",
} => (),
Param::Parameter {
param: "no_include",
}
Expand Down
Loading

0 comments on commit 83de843

Please sign in to comment.