Skip to content

Commit 40c10a8

Browse files
committed
Protobuf3: enable it in compiler/
Signed-off-by: Ludovic Barman <[email protected]>
1 parent 0fb160d commit 40c10a8

File tree

3 files changed

+25
-56
lines changed

3 files changed

+25
-56
lines changed

compiler/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ description = "gRPC compiler for grpcio"
1212
categories = ["network-programming"]
1313

1414
[features]
15-
default = ["protobuf-codec"]
15+
default = ["protobufv3-codec"]
1616
protobuf-codec = ["protobuf"]
17-
protobufv3-codec = ["protobufv3"]
17+
protobufv3-codec = ["protobufv3", "protobuf-codegen", "anyhow"]
1818
prost-codec = ["prost-build", "prost-types", "prost", "derive-new", "tempfile"]
1919

2020
[dependencies]
@@ -26,6 +26,7 @@ prost-types = { version = "0.11", optional = true }
2626
derive-new = { version = "0.5", optional = true }
2727
tempfile = { version = "3.0", optional = true }
2828
protobuf-codegen = { version = "3.2.0", optional = true }
29+
anyhow ={ version = "1.0.72", optional = true}
2930

3031
[[bin]]
3132
name = "grpc_rust_plugin"

compiler/src/codegen.rs

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,27 @@
2121
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2222
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323

24-
use std::collections::HashMap;
25-
use std::io::Write;
26-
24+
#[cfg(feature = "protobuf-codec")]
2725
use protobuf::compiler_plugin;
26+
#[cfg(feature = "protobuf-codec")]
2827
use protobuf::descriptor::*;
28+
#[cfg(feature = "protobuf-codec")]
2929
use protobuf::descriptorx::*;
30+
#[cfg(feature = "protobuf-codec")]
31+
use std::collections::HashMap;
32+
#[cfg(feature = "protobuf-codec")]
33+
use std::io::Write;
3034

3135
#[cfg(feature = "protobufv3-codec")]
3236
use protobuf_codegen;
3337

38+
#[cfg(feature = "protobuf-codec")]
3439
struct CodeWriter<'a> {
3540
writer: &'a mut (dyn Write + 'a),
3641
indent: String,
3742
}
3843

44+
#[cfg(feature = "protobuf-codec")]
3945
impl<'a> CodeWriter<'a> {
4046
pub fn new(writer: &'a mut dyn Write) -> CodeWriter<'a> {
4147
CodeWriter {
@@ -173,15 +179,18 @@ impl<'a> CodeWriter<'a> {
173179
}
174180
}
175181

182+
#[cfg(feature = "protobuf-codec")]
176183
use super::util::{self, fq_grpc, to_snake_case, MethodType};
177184

185+
#[cfg(feature = "protobuf-codec")]
178186
struct MethodGen<'a> {
179187
proto: &'a MethodDescriptorProto,
180188
service_name: String,
181189
service_path: String,
182190
root_scope: &'a RootScope<'a>,
183191
}
184192

193+
#[cfg(feature = "protobuf-codec")]
185194
impl<'a> MethodGen<'a> {
186195
fn new(
187196
proto: &'a MethodDescriptorProto,
@@ -241,16 +250,10 @@ impl<'a> MethodGen<'a> {
241250
to_snake_case(self.proto.get_name())
242251
}
243252

244-
#[cfg(feature = "protobuf-codec")]
245253
fn fq_name(&self) -> String {
246254
format!("\"{}/{}\"", self.service_path, &self.proto.get_name())
247255
}
248256

249-
#[cfg(feature = "protobufv3-codec")]
250-
fn fq_name(&self) -> String {
251-
format!("\"{}/{}\"", self.service_path, &self.proto.name)
252-
}
253-
254257
fn const_method_name(&self) -> String {
255258
format!(
256259
"METHOD_{}_{}",
@@ -541,6 +544,7 @@ impl<'a> MethodGen<'a> {
541544
}
542545
}
543546

547+
#[cfg(feature = "protobuf-codec")]
544548
struct ServiceGen<'a> {
545549
proto: &'a ServiceDescriptorProto,
546550
methods: Vec<MethodGen<'a>>,
@@ -555,15 +559,7 @@ fn service_path(proto: &ServiceDescriptorProto, file: &FileDescriptorProto) -> S
555559
}
556560
}
557561

558-
#[cfg(feature = "protobufv3-codec")]
559-
fn service_path(proto: &'a ServiceDescriptorProto, file: &FileDescriptorProto) -> String {
560-
if file.package.is_empty() {
561-
format!("/{}", proto.name)
562-
} else {
563-
format!("/{}.{}", file.package, proto.name)
564-
};
565-
}
566-
562+
#[cfg(feature = "protobuf-codec")]
567563
impl<'a> ServiceGen<'a> {
568564
fn new(
569565
proto: &'a ServiceDescriptorProto,
@@ -587,16 +583,10 @@ impl<'a> ServiceGen<'a> {
587583
ServiceGen { proto, methods }
588584
}
589585

590-
#[cfg(feature = "protobuf-codec")]
591586
fn service_name(&self) -> String {
592587
util::to_camel_case(self.proto.get_name())
593588
}
594589

595-
#[cfg(feature = "protobufv3-codec")]
596-
fn service_name(&self) -> String {
597-
util::to_camel_case(self.proto.name)
598-
}
599-
600590
fn client_name(&self) -> String {
601591
format!("{}Client", self.service_name())
602592
}
@@ -685,11 +675,7 @@ fn get_service(file: &FileDescriptorProto) -> &[ServiceDescriptorProto] {
685675
file.get_service()
686676
}
687677

688-
#[cfg(feature = "protobufv3-codec")]
689-
fn get_service(file: &FileDescriptorProto) -> &[ServiceDescriptorProto] {
690-
file.service
691-
}
692-
678+
#[cfg(feature = "protobuf-codec")]
693679
fn gen_file(
694680
file: &FileDescriptorProto,
695681
root_scope: &RootScope,
@@ -743,40 +729,21 @@ pub fn gen(
743729
}
744730

745731
#[cfg(feature = "protobufv3-codec")]
746-
pub fn gen(
747-
file_descriptors: &[FileDescriptorProto],
748-
files_to_generate: &[String],
749-
) -> Vec<compiler_plugin::GenResult> {
750-
let files_map: HashMap<&str, &FileDescriptorProto> =
751-
file_descriptors.iter().map(|f| (f.get_name(), f)).collect();
752-
732+
pub fn gen(input_files: &[String]) -> anyhow::Result<()> {
753733
let res = protobuf_codegen::Codegen::new()
754734
.protoc()
755735
// All inputs and imports from the inputs must reside in `includes` directories.
756-
.includes(&["src/protos"])
736+
.includes(["src/protos"])
757737
// Inputs must reside in some of include paths.
758-
.input(file_descriptors)
738+
.inputs(input_files)
759739
// Specify output directory relative to Cargo soutput directory.
760740
.cargo_out_dir("protos")
761741
.run();
762742

763-
let root_scope = RootScope { file_descriptors };
764-
765-
let mut results = Vec::new();
766-
767-
for file_name in files_to_generate {
768-
let file = files_map[&file_name[..]];
769-
770-
if file.get_service().is_empty() {
771-
continue;
772-
}
773-
774-
results.extend(gen_file(file, &root_scope).into_iter());
775-
}
776-
777-
results
743+
res
778744
}
779745

746+
#[cfg(feature = "protobuf-codec")]
780747
pub fn protoc_gen_grpc_rust_main() {
781748
compiler_plugin::plugin_main(gen);
782749
}

compiler/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
#[cfg(feature = "protobuf-codec")]
3+
#[cfg(any(feature = "protobuf-codec", feature = "protobufv3-codec"))]
44
pub mod codegen;
55
#[cfg(feature = "prost-codec")]
66
pub mod prost_codegen;
77

8+
#[cfg(any(feature = "protobuf-codec", feature = "prost-codec"))]
89
mod util;

0 commit comments

Comments
 (0)