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 all commits
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
18 changes: 14 additions & 4 deletions pbjson-build/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ 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>,
}

impl Display for Package {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path[0].to_snake_case())?;
write!(f, "{}", self.path[0].to_snake_case_ident())?;
for element in &self.path[1..self.path.len()] {
write!(f, ".{}", element.to_snake_case())?;
write!(f, ".{}", element.to_snake_case_ident())?;
}
Ok(())
}
Expand Down Expand Up @@ -66,9 +68,9 @@ impl TypeName {
Self(s)
}

pub fn to_snake_case(&self) -> String {
pub fn to_snake_case_ident(&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"
);
}
}
2 changes: 1 addition & 1 deletion pbjson-build/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a> Resolver<'a> {
while let Some(i) = iter.next() {
match iter.peek() {
Some(_) => {
ret.push_str(i.to_snake_case().as_str());
ret.push_str(i.to_snake_case_ident().as_str());
ret.push_str("::");
}
None => {
Expand Down
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;
}
35 changes: 35 additions & 0 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ 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)]
mod tests {
use std::error::Error;

use super::*;
use crate::test::syntax3::kitchen_sink::MixedCase;
use chrono::TimeZone;
Expand Down Expand Up @@ -407,4 +419,27 @@ mod tests {
decoded.string_value = None;
verify(&decoded, r#"{}"#);
}

#[test]
fn test_escaped() -> Result<(), Box<dyn Error>> {
use super::test::escape::{Abstract, Target, Type};

let r#type = Type { example: true };
let r#abstract = Abstract {
r#type: Some(r#type),
};
let target = Target {
r#abstract: Some(r#abstract),
};

let encoded = serde_json::to_string(&target)?;

let expected = r#"{"abstract":{"type":{"example":true}}}"#;
assert_eq!(encoded, expected);

let decoded = serde_json::from_str::<Target>(&encoded)?;
assert_eq!(decoded, target);

Ok(())
}
}