Skip to content

Commit b6db162

Browse files
committed
compiler: Signal that ttrpc-compiler supports proto3 optionals
Got error: protoc --rust_out=. --ttrpc_out=. --plugin=protoc-gen-ttrpc=`which ttrpc_rust_plugin` test.proto test.proto: is a proto3 file that contains optional fields, but code generator protoc-gen-ttrpc hasn't been updated to support optional fields in proto3. Please ask the owner of this code generator to support proto3 optional.--ttrpc_out: According to the doc: https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md#signaling-that-your-code-generator-supports-proto3-optional Although ttrpc-compiler have already supported `proto3 optionals`, code can't be generated using `protoc` unless we signal that we support `proto3 optionals` Signed-off-by: Tim Zhang <tim@hyper.sh>
1 parent c5984fd commit b6db162

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

compiler/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ homepage = "https://github.com/containerd/ttrpc-rust/tree/master/compiler"
1212
readme = "README.md"
1313

1414
[dependencies]
15-
protobuf = "2.0"
16-
protobuf-codegen = "2.14.0"
15+
protobuf = "2.27.1"
16+
protobuf-codegen = "2.27.1"
1717
prost = "0.8"
1818
prost-build = "0.8"
1919
prost-types = "0.8"

compiler/src/codegen.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@
3939
use std::collections::HashMap;
4040

4141
use crate::Customize;
42-
use protobuf::compiler_plugin;
43-
use protobuf::descriptor::*;
44-
use protobuf::descriptorx::*;
42+
use protobuf::{
43+
compiler_plugin::{GenRequest, GenResult},
44+
descriptor::*,
45+
descriptorx::*,
46+
plugin::{
47+
CodeGeneratorRequest, CodeGeneratorResponse, CodeGeneratorResponse_Feature,
48+
CodeGeneratorResponse_File,
49+
},
50+
Message,
51+
};
4552
use protobuf_codegen::code_writer::CodeWriter;
4653
use std::fs::File;
47-
use std::io::{self, Write};
54+
use std::io::{self, stdin, stdout, Write};
4855
use std::path::Path;
4956

5057
use super::util::{
@@ -659,7 +666,7 @@ fn gen_file(
659666
file: &FileDescriptorProto,
660667
root_scope: &RootScope,
661668
customize: &Customize,
662-
) -> Option<compiler_plugin::GenResult> {
669+
) -> Option<GenResult> {
663670
if file.get_service().is_empty() {
664671
return None;
665672
}
@@ -685,7 +692,7 @@ fn gen_file(
685692
}
686693
}
687694

688-
Some(compiler_plugin::GenResult {
695+
Some(GenResult {
689696
name: base + "_ttrpc.rs",
690697
content: v,
691698
})
@@ -695,7 +702,7 @@ pub fn gen(
695702
file_descriptors: &[FileDescriptorProto],
696703
files_to_generate: &[String],
697704
customize: &Customize,
698-
) -> Vec<compiler_plugin::GenResult> {
705+
) -> Vec<GenResult> {
699706
let files_map: HashMap<&str, &FileDescriptorProto> =
700707
file_descriptors.iter().map(|f| (f.get_name(), f)).collect();
701708

@@ -736,7 +743,7 @@ pub fn gen_and_write(
736743
}
737744

738745
pub fn protoc_gen_grpc_rust_main() {
739-
compiler_plugin::plugin_main(|file_descriptors, files_to_generate| {
746+
plugin_main(|file_descriptors, files_to_generate| {
740747
gen(
741748
file_descriptors,
742749
files_to_generate,
@@ -746,3 +753,40 @@ pub fn protoc_gen_grpc_rust_main() {
746753
)
747754
});
748755
}
756+
757+
fn plugin_main<F>(gen: F)
758+
where
759+
F: Fn(&[FileDescriptorProto], &[String]) -> Vec<GenResult>,
760+
{
761+
plugin_main_2(|r| gen(r.file_descriptors, r.files_to_generate))
762+
}
763+
764+
fn plugin_main_2<F>(gen: F)
765+
where
766+
F: Fn(&GenRequest) -> Vec<GenResult>,
767+
{
768+
let req = CodeGeneratorRequest::parse_from_reader(&mut stdin()).unwrap();
769+
let result = gen(&GenRequest {
770+
file_descriptors: &req.get_proto_file(),
771+
files_to_generate: &req.get_file_to_generate(),
772+
parameter: req.get_parameter(),
773+
});
774+
let mut resp = CodeGeneratorResponse::new();
775+
resp.set_supported_features(CodeGeneratorResponse_Feature::FEATURE_PROTO3_OPTIONAL as u64);
776+
resp.set_file(
777+
result
778+
.iter()
779+
.map(|file| {
780+
let mut r = CodeGeneratorResponse_File::new();
781+
r.set_name(file.name.to_string());
782+
r.set_content(
783+
std::str::from_utf8(file.content.as_ref())
784+
.unwrap()
785+
.to_string(),
786+
);
787+
r
788+
})
789+
.collect(),
790+
);
791+
resp.write_to_writer(&mut stdout()).unwrap();
792+
}

0 commit comments

Comments
 (0)