Skip to content

Commit

Permalink
Merge pull request #72 from rnarubin/proto_field_name
Browse files Browse the repository at this point in the history
feat: accept proto field name when deserializing
  • Loading branch information
kodiakhq[bot] authored Oct 4, 2022
2 parents 6f05959 + a822422 commit 464915c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
45 changes: 34 additions & 11 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,14 +610,26 @@ fn write_deserialize_field_name<W: Write>(
) -> Result<()> {
let fields: Vec<_> = message
.all_fields()
.map(|field| (field.json_name(), field.rust_type_name()))
.map(|field| {
let json_name = field.json_name();
// only carry the original proto name if it's different from the provided json name
let proto_name =
Some(field.name.as_str()).filter(|proto_name| proto_name != &json_name);
(json_name, field.rust_type_name(), proto_name)
})
.collect();

write_fields_array(writer, indent, fields.iter().map(|(name, _)| name.as_str()))?;
write_fields_array(
writer,
indent,
fields.iter().flat_map(|(json_name, _, proto_name)| {
proto_name.iter().copied().chain([json_name.as_str()])
}),
)?;
write_fields_enum(
writer,
indent,
fields.iter().map(|(_, name)| name.as_str()),
fields.iter().map(|(_, type_name, _)| type_name.as_str()),
ignore_unknown_fields,
)?;

Expand Down Expand Up @@ -647,14 +659,25 @@ fn write_deserialize_field_name<W: Write>(

if !fields.is_empty() {
writeln!(writer, "{}match value {{", Indent(indent + 4))?;
for (json_name, type_name) in &fields {
writeln!(
writer,
"{}\"{}\" => Ok(GeneratedField::{}),",
Indent(indent + 5),
json_name,
type_name
)?;
for (json_name, type_name, proto_name) in &fields {
if let Some(proto_name) = proto_name {
writeln!(
writer,
"{}\"{}\" | \"{}\" => Ok(GeneratedField::{}),",
Indent(indent + 5),
json_name,
proto_name,
type_name
)?;
} else {
writeln!(
writer,
"{}\"{}\" => Ok(GeneratedField::{}),",
Indent(indent + 5),
json_name,
type_name
)?;
}
}
if ignore_unknown_fields {
writeln!(
Expand Down
10 changes: 10 additions & 0 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ mod tests {

// Can also decode from string
verify_decode(&decoded, r#"{"optionalI32":"0"}"#);
verify_decode(&decoded, r#"{"optional_i32":"0"}"#);

decoded.optional_i32 = None;
verify_decode(&decoded, "{}");
Expand All @@ -177,6 +178,7 @@ mod tests {

// Can also decode from non-string
verify_decode(&decoded, r#"{"optionalI64":0}"#);
verify_decode(&decoded, r#"{"optional_i64":0}"#);

decoded.optional_i64 = None;
verify_decode(&decoded, "{}");
Expand All @@ -200,6 +202,7 @@ mod tests {
verify(&decoded, r#"{"repeatedI32":[0,23,5,6,2,34]}"#);
// Can also mix in some strings
verify_decode(&decoded, r#"{"repeatedI32":[0,"23",5,6,"2",34]}"#);
verify_decode(&decoded, r#"{"repeated_i32":[0,"23",5,6,"2",34]}"#);

decoded.repeated_i32 = vec![];
verify_decode(&decoded, "{}");
Expand All @@ -208,6 +211,7 @@ mod tests {
verify(&decoded, r#"{"repeatedU64":["0","532","2"]}"#);
// Can also mix in some non-strings
verify_decode(&decoded, r#"{"repeatedU64":["0",532,"2"]}"#);
verify_decode(&decoded, r#"{"repeated_u64":["0",532,"2"]}"#);

decoded.repeated_u64 = vec![];
verify_decode(&decoded, "{}");
Expand All @@ -227,6 +231,7 @@ mod tests {

// Can also use variant number
verify_decode(&decoded, r#"{"optionalValue":0}"#);
verify_decode(&decoded, r#"{"optional_value":0}"#);

decoded.optional_value = None;
verify_decode(&decoded, "{}");
Expand All @@ -247,6 +252,7 @@ mod tests {
verify(&decoded, r#"{"int32Dict":{"343":"A"}}"#);
// Enum dictionary values can be decoded from integers
verify_decode(&decoded, r#"{"int32Dict":{"343":66}}"#);
verify_decode(&decoded, r#"{"int32_dict":{"343":66}}"#);

decoded.int32_dict = Default::default();
verify_decode(&decoded, "{}");
Expand All @@ -256,6 +262,7 @@ mod tests {
verify(&decoded, r#"{"integerDict":{"12":"13"}}"#);
// 64-bit dictionary values can be decoded from numeric types
verify_decode(&decoded, r#"{"integerDict":{"12":13}}"#);
verify_decode(&decoded, r#"{"integer_dict":{"12":13}}"#);

decoded.integer_dict = Default::default();
verify_decode(&decoded, "{}");
Expand All @@ -264,6 +271,7 @@ mod tests {
verify(&decoded, r#"{"oneOfI32":0}"#);
// Can also specify string
verify_decode(&decoded, r#"{"oneOfI32":"0"}"#);
verify_decode(&decoded, r#"{"one_of_i32":"0"}"#);

decoded.one_of = Some(kitchen_sink::OneOf::OneOfI32(12));
verify(&decoded, r#"{"oneOfI32":12}"#);
Expand All @@ -280,6 +288,7 @@ mod tests {
verify(&decoded, r#"{"oneOfValue":"VALUE_B"}"#);
// Can also specify enum variant
verify_decode(&decoded, r#"{"oneOfValue":63}"#);
verify_decode(&decoded, r#"{"one_of_value":63}"#);

decoded.one_of = None;
verify_decode(&decoded, "{}");
Expand All @@ -294,6 +303,7 @@ mod tests {
r#"{"repeatedValue":["VALUE_B","VALUE_B","VALUE_A"]}"#,
);
verify_decode(&decoded, r#"{"repeatedValue":[63,"VALUE_B","VALUE_A"]}"#);
verify_decode(&decoded, r#"{"repeated_value":[63,"VALUE_B","VALUE_A"]}"#);

decoded.repeated_value = Default::default();
verify_decode(&decoded, "{}");
Expand Down

0 comments on commit 464915c

Please sign in to comment.