Skip to content

Commit

Permalink
Escape keywords in TypeName
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel committed Sep 14, 2022
1 parent 9d550f2 commit 7054c75
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions 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 @@ -63,12 +65,12 @@ impl TypeName {
"type name cannot contain \'.\', got \"{}\"",
s
);
Self(s)
Self(escape_ident(s))
}

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,10 @@ mod tests {
String::from("foo_bar.baz.boo")
)
}

#[test]
fn escape_keywords() {
assert_eq!(TypeName::new("type").to_string(), "r#type");
assert_eq!(TypeName::new("Type").to_snake_case(), "r#type");
}
}

0 comments on commit 7054c75

Please sign in to comment.