Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape keywords in TypeName #67

Merged
merged 3 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
mbrobbel marked this conversation as resolved.
Show resolved Hide resolved
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"
));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we possibly get some tests of serializing to/from these types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

#[cfg(test)]
Expand Down