Skip to content

Commit

Permalink
Merge pull request #67 from mbrobbel/escape-keywords
Browse files Browse the repository at this point in the history
fix: escape keywords in TypeName
  • Loading branch information
kodiakhq[bot] authored Sep 20, 2022
2 parents f8fa4a2 + c608e46 commit dfbff3b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
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"
));
}
}

#[cfg(test)]
mod tests {
use std::error::Error;

use super::*;
use crate::test::syntax3::kitchen_sink::MixedCase;
use chrono::TimeZone;
Expand Down Expand Up @@ -416,4 +428,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(())
}
}

0 comments on commit dfbff3b

Please sign in to comment.