Skip to content

Commit

Permalink
Merge branch 'main' into fix/case-insensitive-split-mode
Browse files Browse the repository at this point in the history
# Conflicts:
#	pilota-build/src/codegen/mod.rs
#	pilota-build/src/test/mod.rs
  • Loading branch information
missingdays committed Oct 31, 2024
2 parents 0acf82e + 9aca81f commit cceb31a
Show file tree
Hide file tree
Showing 33 changed files with 672 additions and 120 deletions.
48 changes: 44 additions & 4 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
@@ -1,5 +1,5 @@
[workspace]
members = ["pilota", "pilota-build", "pilota-thrift-parser"]
members = ["pilota", "pilota-build", "pilota-thrift-parser", "examples"]
resolver = "2"

[profile.bench]
Expand Down
58 changes: 58 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "examples"
version = "0.1.0"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
homepage = "https://cloudwego.io/docs/pilota/"
repository = "https://github.com/cloudwego/pilota"
license = "MIT OR Apache-2.0"
authors = ["Pilota Team <[email protected]>"]
keywords = ["serialization", "thrift", "protobuf", "volo"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[badges]
maintenance = { status = "actively-developed" }

[dependencies]
pilota = { path = "../pilota", features = ["pb-encode-default-value"] }
pilota-build = { path = "../pilota-build" }
pilota-thrift-parser = { path = "../pilota-thrift-parser", version = "0.11" }

ahash = "0.8"
anyhow = "1"
dashmap = "5"
heck = "0.5"
itertools = "0.13"
normpath = "1"
paste = "1"
petgraph = "0.6"
phf = { version = "0.11", features = ["macros"] }
proc-macro2 = "1"
quote = "1"
rayon = "1"
rustc-hash = "1"
salsa = { version = "0.17.0-pre.2" }
scoped-tls = "1"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.9"
syn = "2"
toml = "0.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

# The official rust-protobuf parser currently has some bug.
# We will switch to the official one when https://github.com/stepancheg/rust-protobuf/pull/646 is fixed.
protobuf-parse = { package = "protobuf-parse2", version = "4.0.0-alpha.4" }
protobuf = { package = "protobuf2", version = "4.0.0-alpha.2" }
faststr = "0.2"

[dev-dependencies]

tokio = { version = "1", features = ["io-util"] }
derivative = "2"
tempfile = "3"
diffy = "0.4"
criterion = { version = "0.5", features = ["html_reports"] }
rand = "0.8"
linkedbytes = "0.1"
5 changes: 5 additions & 0 deletions examples/idl/zero_value.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message A {
map<string, string> str_map = 1;
required string s1 = 2;
optional string s2 = 3;
}
36 changes: 36 additions & 0 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use pilota::prost::Message as _;

mod zero_value;

#[test]
fn test_pb_encode_zero_value() {
let test_data = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("idl")
.join("zero_value.proto");

let out_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("src")
.join("zero_value.rs");

pilota_build::Builder::protobuf()
.ignore_unused(false)
.include_dirs(vec![test_data.parent().unwrap().to_path_buf()])
.compile_with_config(
vec![pilota_build::IdlService::from_path(test_data.to_path_buf())],
pilota_build::Output::File(out_path.into()),
);

let mut a = zero_value::zero_value::A::default();

a.str_map.insert("key1".into(), "value".into());
a.str_map.insert("key2".into(), "".into());

let mut buf = pilota::BytesMut::new();
a.encode(&mut buf).unwrap();

println!("{:?}", buf);
// println!("{:?}", buf.freeze().as_ref());

let parsed_a = zero_value::zero_value::A::decode(buf).unwrap();
println!("{:?}", parsed_a);
}
102 changes: 102 additions & 0 deletions examples/src/zero_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
pub mod zero_value {
#![allow(warnings, clippy::all)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct A {
pub str_map: ::pilota::AHashMap<::pilota::FastStr, ::pilota::FastStr>,

pub s1: ::pilota::FastStr,

pub s2: ::std::option::Option<::pilota::FastStr>,
}
impl ::pilota::prost::Message for A {
#[inline]
fn encoded_len(&self) -> usize {
0 + ::pilota::prost::encoding::hash_map::encoded_len(
::pilota::prost::encoding::faststr::encoded_len,
::pilota::prost::encoding::faststr::encoded_len,
1,
&self.str_map,
) + ::pilota::prost::encoding::faststr::encoded_len(2, &self.s1)
+ self.s2.as_ref().map_or(0, |value| {
::pilota::prost::encoding::faststr::encoded_len(3, value)
})
}

#[allow(unused_variables)]
fn encode_raw<B>(&self, buf: &mut B)
where
B: ::pilota::prost::bytes::BufMut,
{
::pilota::prost::encoding::hash_map::encode(
::pilota::prost::encoding::faststr::encode,
::pilota::prost::encoding::faststr::encoded_len,
::pilota::prost::encoding::faststr::encode,
::pilota::prost::encoding::faststr::encoded_len,
1,
&self.str_map,
buf,
);
::pilota::prost::encoding::faststr::encode(2, &self.s1, buf);
if let Some(_pilota_inner_value) = self.s2.as_ref() {
::pilota::prost::encoding::faststr::encode(3, _pilota_inner_value, buf);
};
}

#[allow(unused_variables)]
fn merge_field<B>(
&mut self,
tag: u32,
wire_type: ::pilota::prost::encoding::WireType,
buf: &mut B,
ctx: ::pilota::prost::encoding::DecodeContext,
) -> ::core::result::Result<(), ::pilota::prost::DecodeError>
where
B: ::pilota::prost::bytes::Buf,
{
const STRUCT_NAME: &'static str = stringify!(A);
match tag {
1 => {
let mut _inner_pilota_value = &mut self.str_map;
::pilota::prost::encoding::hash_map::merge(
::pilota::prost::encoding::faststr::merge,
::pilota::prost::encoding::faststr::merge,
&mut _inner_pilota_value,
buf,
ctx,
)
.map_err(|mut error| {
error.push(STRUCT_NAME, stringify!(str_map));
error
})
}
2 => {
let mut _inner_pilota_value = &mut self.s1;
::pilota::prost::encoding::faststr::merge(
wire_type,
_inner_pilota_value,
buf,
ctx,
)
.map_err(|mut error| {
error.push(STRUCT_NAME, stringify!(s1));
error
})
}
3 => {
let mut _inner_pilota_value = &mut self.s2;
::pilota::prost::encoding::faststr::merge(
wire_type,
_inner_pilota_value.get_or_insert_with(::core::default::Default::default),
buf,
ctx,
)
.map_err(|mut error| {
error.push(STRUCT_NAME, stringify!(s2));
error
})
}
_ => ::pilota::prost::encoding::skip_field(wire_type, tag, buf, ctx),
}
}
}
}
2 changes: 1 addition & 1 deletion pilota-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pilota-build"
version = "0.11.21"
version = "0.11.22"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
documentation = "https://docs.rs/pilota-build"
Expand Down
14 changes: 8 additions & 6 deletions pilota-build/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,14 @@ where
this: &mut Codegen<B>,
base_dir: &Path,
p: &Arc<[FastStr]>,
def_ids: &Vec<CodegenItem>,
def_ids: &[CodegenItem],
stream: &mut RefMut<Arc<[FastStr]>, String>,
mut dup: &mut AHashMap<FastStr, Vec<DefId>>,
dup: &mut AHashMap<FastStr, Vec<DefId>>,
) {
let base_mod_name = p.iter().map(|s| s.to_string()).join("/");
let mod_file_name = format!("{}/mod.rs", base_mod_name);
let mut mod_stream = String::new();

let mut existing_file_names: AHashSet<String> = AHashSet::new();

for def_id in def_ids.iter() {
Expand All @@ -561,11 +561,12 @@ where
let unique_name = Self::generate_unique_name(&existing_file_names, &simple_name);
existing_file_names.insert(unique_name.to_ascii_lowercase().clone());
let file_name = format!("{}.rs", unique_name);
this.write_item(&mut item_stream, *def_id, &mut dup);
this.write_item(&mut item_stream, *def_id, dup);

let full_path = mod_dir.join(file_name.clone());
std::fs::create_dir_all(mod_dir).unwrap();

let item_stream = item_stream.lines().map(|s| s.trim_end()).join("\n");
let mut file =
std::io::BufWriter::new(std::fs::File::create(full_path.clone()).unwrap());
file.write_all(item_stream.as_bytes()).unwrap();
Expand All @@ -576,17 +577,18 @@ where
}

let mod_path = base_dir.join(&mod_file_name);
let mod_stream = mod_stream.lines().map(|s| s.trim_end()).join("\n");
let mut mod_file = std::io::BufWriter::new(std::fs::File::create(&mod_path).unwrap());
mod_file.write_all(mod_stream.as_bytes()).unwrap();
mod_file.flush().unwrap();
fmt_file(&mod_path);

stream.push_str(format!("include!(\"{}\");\n", mod_file_name).as_str());
}

/**
On Windows and macOS, files names are case-insensitive
To avoid problems when generating files for services with similar names, e.g.
To avoid problems when generating files for services with similar names, e.g.
testService and TestService, such names are de-duplicated by adding a number to their nam5e
*/
fn generate_unique_name(existing_names: &AHashSet<String>, simple_name: &String) -> String {
Expand Down
11 changes: 4 additions & 7 deletions pilota-build/src/codegen/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ where

let members = entry_map
.keys()
.filter_map(|k| {
if let DefLocation::Fixed(..) = k {
let name = self.cx().crate_name(k);
Some(format!(" \"{name}\""))
} else {
None
}
.map(|k| {
let name = self.cx().crate_name(k);
format!(" \"{name}\"")
})
.dedup()
.sorted()
.join(",\n");

Expand Down
2 changes: 1 addition & 1 deletion pilota-build/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub fn fmt_file<P: AsRef<Path>>(file: P) {
Err(e) => eprintln!("{}", e),
Ok(output) => {
if !output.status.success() {
eprintln!("rustfmt failed to format {}", file.display());
std::io::stderr().write_all(&output.stderr).unwrap();
exit(output.status.code().unwrap_or(1))
}
}
}
Expand Down
Loading

0 comments on commit cceb31a

Please sign in to comment.