Skip to content

Commit d054832

Browse files
authored
Merge pull request containerd#157 from Tim-Zhang/optional-support-3.x
Upgrade protobuf from 2.x to 3.x
2 parents fbe8cb9 + 007a513 commit d054832

File tree

23 files changed

+154
-82
lines changed

23 files changed

+154
-82
lines changed

.github/workflows/bvt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
matrix:
4141
checks:
4242
- advisories
43-
- bans licenses sources
43+
- bans sources
4444

4545
# Prevent sudden announcement of a new advisory from failing ci:
4646
continue-on-error: ${{ matrix.checks == 'advisories' }}

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ homepage = "https://github.com/containerd/ttrpc-rust"
1111
description = "A Rust version of ttrpc."
1212

1313
[dependencies]
14-
protobuf = { version = "2.0" }
14+
protobuf = { version = "3.1.0" }
1515
libc = { version = "0.2.59", features = [ "extra_traits" ] }
1616
nix = "0.23.0"
1717
log = "0.4"
@@ -26,7 +26,7 @@ futures = { version = "0.3", optional = true }
2626
tokio-vsock = { version = "0.3.1", optional = true }
2727

2828
[build-dependencies]
29-
protobuf-codegen-pure = "2.14.0"
29+
protobuf-codegen = "3.1.0"
3030

3131
[features]
3232
default = ["sync"]

build.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ fn main() {
77
let path: PathBuf = [out_dir.clone(), "mod.rs".to_string()].iter().collect();
88
fs::write(path, "pub mod ttrpc;").unwrap();
99

10-
protobuf_codegen_pure::Codegen::new()
10+
let customize = protobuf_codegen::Customize::default()
11+
.gen_mod_rs(false)
12+
.generate_accessors(true);
13+
14+
protobuf_codegen::Codegen::new()
15+
.pure()
1116
.out_dir(out_dir)
1217
.inputs(&["src/ttrpc.proto"])
1318
.include("src")
19+
.customize(customize)
1420
.run()
1521
.expect("Codegen failed.");
1622
}

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+
}

example/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ homepage = "https://github.com/alipay/ttrpc-rust"
1111
description = "An example of ttrpc."
1212

1313
[dev-dependencies]
14-
protobuf = "2.8.0"
14+
protobuf = "3.1.0"
1515
bytes = "0.4.11"
1616
libc = "0.2.79"
1717
byteorder = "1.3.2"

example/async-server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ impl agent_ttrpc::AgentService for AgentService {
6666
_ctx: &::ttrpc::r#async::TtrpcContext,
6767
_req: agent::ListInterfacesRequest,
6868
) -> ::ttrpc::Result<agent::Interfaces> {
69-
let mut rp = protobuf::RepeatedField::new();
69+
let mut rp = Vec::new();
7070

7171
let mut i = types::Interface::new();
72-
i.set_name("first".to_string());
72+
i.name = "first".to_string();
7373
rp.push(i);
7474
let mut i = types::Interface::new();
75-
i.set_name("second".to_string());
75+
i.name = "second".to_string();
7676
rp.push(i);
7777

7878
let mut i = agent::Interfaces::new();
79-
i.set_Interfaces(rp);
79+
i.Interfaces = rp;
8080

8181
Ok(i)
8282
}

example/build.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33
// SPDX-License-Identifier: Apache-2.0
44
//
55

6-
use std::fs::File;
7-
use std::io::{Read, Write};
8-
use ttrpc_codegen::Codegen;
9-
use ttrpc_codegen::Customize;
6+
use std::{
7+
fs::File,
8+
io::{Read, Write},
9+
};
10+
use ttrpc_codegen::{Codegen, Customize, ProtobufCustomize};
1011

1112
fn main() {
1213
let mut protos = vec![
14+
"protocols/protos/github.com/gogo/protobuf/gogoproto/gogo.proto",
1315
"protocols/protos/github.com/kata-containers/agent/pkg/types/types.proto",
1416
"protocols/protos/agent.proto",
1517
"protocols/protos/health.proto",
1618
"protocols/protos/google/protobuf/empty.proto",
1719
"protocols/protos/oci.proto",
1820
];
1921

22+
let protobuf_customized = ProtobufCustomize::default().gen_mod_rs(false);
23+
2024
Codegen::new()
2125
.out_dir("protocols/sync")
2226
.inputs(&protos)
@@ -25,6 +29,7 @@ fn main() {
2529
.customize(Customize {
2630
..Default::default()
2731
})
32+
.rust_protobuf_customize(protobuf_customized.clone())
2833
.run()
2934
.expect("Gen sync code failed.");
3035

@@ -40,6 +45,7 @@ fn main() {
4045
async_all: true,
4146
..Default::default()
4247
})
48+
.rust_protobuf_customize(protobuf_customized.clone())
4349
.run()
4450
.expect("Gen async code failed.");
4551

example/protocols/asynchronous/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
pub mod agent;
77
pub mod agent_ttrpc;
88
pub mod empty;
9+
mod gogo;
910
pub mod health;
1011
pub mod health_ttrpc;
1112
mod oci;
12-
pub mod types;
1313
pub mod streaming;
1414
pub mod streaming_ttrpc;
15+
pub mod types;

example/protocols/protos/health.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ option (gogoproto.benchgen_all) = true;
1717

1818
message CheckRequest {
1919
string service = 1;
20+
optional string option_val = 2;
2021
}
2122

2223
message HealthCheckResponse {

0 commit comments

Comments
 (0)