Skip to content

Commit

Permalink
fix: escape keywords in typename
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel committed Sep 16, 2022
1 parent 9d550f2 commit 3b717dc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pbjson-build/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use prost_types::{
FileDescriptorProto, FileDescriptorSet, MessageOptions, OneofDescriptorProto,
};

use crate::escape::escape_ident;

#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Package {
path: Vec<TypeName>,
Expand Down Expand Up @@ -68,7 +70,7 @@ impl TypeName {

pub fn to_snake_case(&self) -> String {
use heck::ToSnakeCase;
self.0.to_snake_case()
escape_ident(self.0.to_snake_case())
}

pub fn to_upper_camel_case(&self) -> String {
Expand Down Expand Up @@ -299,4 +301,12 @@ mod tests {
String::from("foo_bar.baz.boo")
)
}

#[test]
fn escape_keywords() {
assert_eq!(
Package::new("type.abstract").to_string(),
"r#type.r#abstract"
);
}
}
1 change: 1 addition & 0 deletions pbjson-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() -> Result<()> {
root.join("syntax3.proto"),
root.join("common.proto"),
root.join("duplicate_name.proto"),
root.join("escape.proto"),
];

// Tell cargo to recompile if any of these proto files are changed
Expand Down
14 changes: 14 additions & 0 deletions pbjson-test/protos/escape.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package test.abstract.type.escape;

message Target {
Abstract abstract = 1;
}

message Abstract {
Type type = 1;
}

message Type {
bool example = 1;
}
10 changes: 10 additions & 0 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ pub mod test {
include!(concat!(env!("OUT_DIR"), "/test.duplicate_name.rs"));
include!(concat!(env!("OUT_DIR"), "/test.duplicate_name.serde.rs"));
}
pub mod escape {
include!(concat!(
env!("OUT_DIR"),
"/test.r#abstract.r#type.escape.rs"
));
include!(concat!(
env!("OUT_DIR"),
"/test.r#abstract.r#type.escape.serde.rs"
));
}
}

#[cfg(test)]
Expand Down

0 comments on commit 3b717dc

Please sign in to comment.