Skip to content

Commit

Permalink
fix: escape keywords when generate code (#213)
Browse files Browse the repository at this point in the history
* fix: escape keywords when generate code
  • Loading branch information
Ggiggle authored Nov 13, 2023
1 parent f854364 commit 73351f6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion 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 pilota-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pilota-build"
version = "0.9.7"
version = "0.9.8"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
documentation = "https://docs.rs/pilota-build"
Expand Down
12 changes: 10 additions & 2 deletions pilota-build/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ where
};
}
CodegenKind::RePub => {
let path = self.item_path(item.def_id).join("::");
let path = self
.item_path(item.def_id)
.iter()
.map(|item| item.to_string())
.join("::");
stream.push_str(format!("pub use ::{};", path).as_str());
}
})
Expand Down Expand Up @@ -336,7 +340,11 @@ where
pub fn get_init_service(&self, def_id: DefId) -> (String, String) {
let service_name = self.rust_name(def_id);
let mod_prefix = self.mod_path(def_id);
let service_path = format!("{}::{}", mod_prefix.join("::"), service_name);
let service_path = format!(
"{}::{}",
mod_prefix.iter().map(|item| item.to_string()).join("::"),
service_name
);
tracing::debug!("service_path: {}", service_path);
let methods = self.service_methods(def_id);

Expand Down
2 changes: 1 addition & 1 deletion pilota-build/src/codegen/thrift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl ThriftBackend {

fn codegen_impl_message(
&self,
def_id: DefId,
_def_id: DefId,
name: Symbol,
encode: String,
size: String,
Expand Down
5 changes: 4 additions & 1 deletion pilota-build/src/codegen/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ where
})),
);
if let Some(main_mod_path) = info.main_mod_path {
gen_rs_stream.push_str(&format!("pub use {}::*;", main_mod_path.join("::")));
gen_rs_stream.push_str(&format!(
"pub use {}::*;",
main_mod_path.iter().map(|item| item.to_string()).join("::")
));
}
gen_rs_stream = format! {r#"pub mod gen {{
#![allow(warnings, clippy::all)]
Expand Down
24 changes: 12 additions & 12 deletions pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,18 +871,18 @@ impl Context {
info
}

pub fn def_id_info(&self, def_id: DefId) -> FastStr {
let file_path = self
.file(self.node(def_id).unwrap().file_id)
.unwrap()
.package
.clone();
file_path
.iter()
.chain(&[self.node(def_id).unwrap().name()])
.join("::")
.into()
}
// pub fn def_id_info(&self, def_id: DefId) -> FastStr {
// let file_path = self
// .file(self.node(def_id).unwrap().file_id)
// .unwrap()
// .package
// .clone();
// file_path
// .iter()
// .chain(&[self.node(def_id).unwrap().name()])
// .join("::")
// .into()
// }

pub fn config(&self, crate_id: &CrateId) -> &serde_yaml::Value {
let main_file = crate_id.main_file;
Expand Down
2 changes: 0 additions & 2 deletions pilota-build/src/middle/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub trait PathResolver: Sync + Send {
_ => panic!(),
};

// keyword escape
let name = name.to_string().into();
segs.push(name);
}

Expand Down
7 changes: 6 additions & 1 deletion pilota-build/src/middle/ty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Display;
pub use std::sync::Arc;

use itertools::Itertools;
pub use TyKind::*;

use super::context::tls::with_cx;
Expand Down Expand Up @@ -141,7 +142,11 @@ impl CodegenTy {
.into()
}
CodegenTy::Adt(def) => with_cx(|cx| {
let path = cx.item_path(def.did).join("::");
let path = cx
.item_path(def.did)
.iter()
.map(|item| item.to_string())
.join("::");

format!("::{path}").into()
}),
Expand Down

0 comments on commit 73351f6

Please sign in to comment.