Skip to content

Commit

Permalink
chore: update with upstream changes in influxdata/pbjson (#5)
Browse files Browse the repository at this point in the history
* chore: fix CI builds (influxdata#104)

* chore: update prost to 0.12 (influxdata#111)

* chore: update prost to 0.12

* chore: fix lint

* chore: update descriptors

---------

Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>

* fix: parsing numbers more than i32 and u32 (influxdata#87)

* fix: parsing numbers more than i32 and u32

* fix: f64::MANTISSA_DIGITS instead of 53

---------

Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>

* fix: Resolve Envoy xDS proto issues (influxdata#103)

* fix: Escape Self since it is a reserved keyword

* fix: Allow duplicate numbers in enums when aliases are enabled

* fix: Avoid field name collisions with the map ident

* fix: follow cargo best practice in build.rs script (influxdata#90)

Using env! inside the build.rs script makes it difficult to build this
package with other build tools like bazel which sandbox execution, since
the source directory might be at different paths between when the
build.rs file is compiled, and when the resulting build script is
executed.

Cargo recommends that CARGO_MANIFEST_DIR is read at runtime instead:
https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts

The above links also includes the promise that Cargo will set the cwd to
the crate's directory. This patch uses this to find the descriptors.bin
file.

Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>

* chore(deps): update itertools requirement from 0.10 to 0.11 (influxdata#98)

Updates the requirements on [itertools](https://github.com/rust-itertools/itertools) to permit the latest version.
- [Changelog](https://github.com/rust-itertools/itertools/blob/master/CHANGELOG.md)
- [Commits](rust-itertools/itertools@v0.10.0...v0.11.0)

---
updated-dependencies:
- dependency-name: itertools
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>

* chore(deps): update base64 requirement from 0.13 to 0.21 (influxdata#89)

* chore(deps): update base64 requirement from 0.13 to 0.21

Updates the requirements on [base64](https://github.com/marshallpierce/rust-base64) to permit the latest version.
- [Release notes](https://github.com/marshallpierce/rust-base64/releases)
- [Changelog](https://github.com/marshallpierce/rust-base64/blob/master/RELEASE-NOTES.md)
- [Commits](marshallpierce/rust-base64@v0.13.0...v0.21.0)

---
updated-dependencies:
- dependency-name: base64
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: fix removed methods

* fix: indifferent padding

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>

* chore: prepare 0.6.0 (influxdata#112)

* fix: prost deprecations (influxdata#113)

* chore: patch release pjson-build 0.6.2 (influxdata#114)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Bob Aman <sporkmonger@users.noreply.github.com>
Co-authored-by: Jeffrey Smith II <jeffreyssmith2nd@gmail.com>
Co-authored-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
Co-authored-by: Nikolay Alexandrov <nick.alexandrov@gmail.com>
Co-authored-by: Thomas Gibson-Robinson <thomas.gibsonrobinson@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Raphael Taylor-Davies <1781103+tustvold@users.noreply.github.com>
  • Loading branch information
8 people authored Dec 20, 2023
1 parent 9c18a04 commit 1b44e42
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 72 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[workspace]
members = [
"pbjson",
"pbjson-build",
"pbjson-test",
"pbjson-types",
"pbjson",
"pbjson-build",
"pbjson-test",
"pbjson-types",
]

exclude = [
"pbjson-no-std"
]
resolver = "2"
6 changes: 3 additions & 3 deletions pbjson-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ name = "pbjson_build"

[dependencies]
heck = "0.4"
prost = "0.11"
prost-types = "0.11"
itertools = "0.10"
prost = "0.12"
prost-types = "0.12"
itertools = "0.12"

[dev-dependencies]
tempfile = "3.1"
4 changes: 2 additions & 2 deletions pbjson-build/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use prost_types::{
FileDescriptorProto, FileDescriptorSet, MessageOptions, OneofDescriptorProto,
};

use crate::escape::escape_ident;
use crate::escape::{escape_ident, escape_type};

#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Package {
Expand Down Expand Up @@ -75,7 +75,7 @@ impl TypeName {

pub fn to_upper_camel_case(&self) -> String {
use heck::ToUpperCamelCase;
self.0.to_upper_camel_case()
escape_type(self.0.to_upper_camel_case())
}
}

Expand Down
8 changes: 8 additions & 0 deletions pbjson-build/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ pub fn escape_ident(mut ident: String) -> String {
};
ident
}

pub fn escape_type(mut ident: String) -> String {
// this keyword is not supported as a raw identifier and is therefore suffixed with an underscore.
if ident == "Self" {
ident += "_";
}
ident
}
11 changes: 7 additions & 4 deletions pbjson-build/src/generator/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{
use crate::descriptor::{EnumDescriptor, TypePath};
use crate::generator::write_fields_array;
use crate::resolver::Resolver;
use std::collections::HashSet;
use std::io::{Result, Write};

pub fn generate_enum<W: Write>(
Expand All @@ -22,9 +23,13 @@ pub fn generate_enum<W: Write>(
) -> Result<()> {
let rust_type = resolver.rust_type(path);

let mut seen_numbers = HashSet::new();
let variants: Vec<_> = descriptor
.values
.iter()
// Skip duplicates if we've seen the number before
// Protobuf's `allow_alias` option permits duplicates if set
.filter(|variant| seen_numbers.insert(variant.number()))
.map(|variant| {
let variant_name = variant.name.clone().unwrap();
let variant_number = variant.number();
Expand Down Expand Up @@ -104,10 +109,9 @@ fn write_visitor<W: Write>(
{indent} where
{indent} E: serde::de::Error,
{indent} {{
{indent} use core::convert::TryFrom;
{indent} i32::try_from(v)
{indent} .ok()
{indent} .and_then({rust_type}::from_i32)
{indent} .and_then(|x| x.try_into().ok())
{indent} .ok_or_else(|| {{
{indent} serde::de::Error::invalid_value(serde::de::Unexpected::Signed(v), &self)
{indent} }})
Expand All @@ -117,10 +121,9 @@ fn write_visitor<W: Write>(
{indent} where
{indent} E: serde::de::Error,
{indent} {{
{indent} use core::convert::TryFrom;
{indent} i32::try_from(v)
{indent} .ok()
{indent} .and_then({rust_type}::from_i32)
{indent} .and_then(|x| x.try_into().ok())
{indent} .ok_or_else(|| {{
{indent} serde::de::Error::invalid_value(serde::de::Unexpected::Unsigned(v), &self)
{indent} }})
Expand Down
63 changes: 37 additions & 26 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::{
Indent,
};
use crate::descriptor::TypePath;
use crate::escape::escape_type;
use crate::generator::write_fields_array;
use crate::resolver::Resolver;

Expand Down Expand Up @@ -207,10 +208,10 @@ fn write_decode_variant<W: Write>(
path: &TypePath,
writer: &mut W,
) -> Result<()> {
writeln!(writer, "{}::from_i32({})", resolver.rust_type(path), value)?;
writeln!(writer, "{}::try_from({})", resolver.rust_type(path), value)?;
write!(
writer,
"{}.ok_or_else(|| serde::ser::Error::custom(::alloc::format!(\"Invalid variant {{}}\", {})))",
"{}.map_err(|_| serde::ser::Error::custom(::alloc::format!(\"Invalid variant {{}}\", {})))",
Indent(indent),
value
)
Expand Down Expand Up @@ -374,6 +375,11 @@ fn write_serialize_scalar_variable<W: Write>(
)
}
_ => {
writeln!(
writer,
"{}#[allow(clippy::needless_borrow)]",
Indent(indent)
)?;
writeln!(
writer,
"{}struct_ser.serialize_field(\"{}\", {}(&{}).as_str())?;",
Expand Down Expand Up @@ -517,7 +523,7 @@ fn write_deserialize_message<W: Write>(
{indent} formatter.write_str("struct {name}")
{indent} }}
{indent} fn visit_map<V>(self, mut map: V) -> core::result::Result<{rust_type}, V::Error>
{indent} fn visit_map<V>(self, mut map_: V) -> core::result::Result<{rust_type}, V::Error>
{indent} where
{indent} V: serde::de::MapAccess<'de>,
{indent} {{"#,
Expand Down Expand Up @@ -547,7 +553,7 @@ fn write_deserialize_message<W: Write>(
if !message.fields.is_empty() || !message.one_ofs.is_empty() {
writeln!(
writer,
"{}while let Some(k) = map.next_key()? {{",
"{}while let Some(k) = map_.next_key()? {{",
Indent(indent + 2)
)?;

Expand Down Expand Up @@ -582,7 +588,7 @@ fn write_deserialize_message<W: Write>(
)?;
writeln!(
writer,
"{}let _ = map.next_value::<serde::de::IgnoredAny>()?;",
"{}let _ = map_.next_value::<serde::de::IgnoredAny>()?;",
Indent(indent + 5),
)?;
writeln!(writer, "{}}}", Indent(indent + 4))?;
Expand All @@ -593,12 +599,12 @@ fn write_deserialize_message<W: Write>(
} else {
writeln!(
writer,
"{}while map.next_key::<GeneratedField>()?.is_some() {{",
"{}while map_.next_key::<GeneratedField>()?.is_some() {{",
Indent(indent + 2)
)?;
writeln!(
writer,
"{}let _ = map.next_value::<serde::de::IgnoredAny>()?;",
"{}let _ = map_.next_value::<serde::de::IgnoredAny>()?;",
Indent(indent + 3)
)?;
writeln!(writer, "{}}}", Indent(indent + 2))?;
Expand Down Expand Up @@ -720,15 +726,15 @@ fn write_deserialize_field_name<W: Write>(
Indent(indent + 5),
json_name,
proto_name,
type_name
escape_type(type_name.to_string())
)?;
} else {
writeln!(
writer,
"{}\"{}\" => Ok(GeneratedField::{}),",
Indent(indent + 5),
json_name,
type_name
escape_type(type_name.to_string())
)?;
}
}
Expand Down Expand Up @@ -784,7 +790,12 @@ fn write_fields_enum<'a, W: Write, I: Iterator<Item = &'a str>>(
)?;
writeln!(writer, "{}enum GeneratedField {{", Indent(indent))?;
for type_name in fields {
writeln!(writer, "{}{},", Indent(indent + 1), type_name)?;
writeln!(
writer,
"{}{},",
Indent(indent + 1),
escape_type(type_name.to_string())
)?;
}

if ignore_unknown_fields {
Expand Down Expand Up @@ -837,7 +848,7 @@ fn write_deserialize_field<W: Write>(
Some(deserializer) => {
write!(
writer,
"map.next_value::<::core::option::Option<{}>>()?.map(|x| {}::{}(x.0))",
"map_.next_value::<::core::option::Option<{}>>()?.map(|x| {}::{}(x.0))",
deserializer,
resolver.rust_type(&one_of.path),
field.rust_type_name()
Expand All @@ -846,7 +857,7 @@ fn write_deserialize_field<W: Write>(
None => {
write!(
writer,
"map.next_value::<::core::option::Option<_>>()?.map({}::{})",
"map_.next_value::<::core::option::Option<_>>()?.map({}::{})",
resolver.rust_type(&one_of.path),
field.rust_type_name()
)?;
Expand All @@ -855,15 +866,15 @@ fn write_deserialize_field<W: Write>(
FieldType::Enum(path) => {
write!(
writer,
"map.next_value::<::core::option::Option<{}>>()?.map(|x| {}::{}(x as i32))",
"map_.next_value::<::core::option::Option<{}>>()?.map(|x| {}::{}(x as i32))",
resolver.rust_type(path),
resolver.rust_type(&one_of.path),
field.rust_type_name()
)?;
}
FieldType::Message(_) => writeln!(
writer,
"map.next_value::<::core::option::Option<_>>()?.map({}::{})",
"map_.next_value::<::core::option::Option<_>>()?.map({}::{})",
resolver.rust_type(&one_of.path),
field.rust_type_name()
)?,
Expand All @@ -878,21 +889,21 @@ fn write_deserialize_field<W: Write>(
FieldModifier::Optional => {
write!(
writer,
"map.next_value::<::core::option::Option<{}>>()?.map(|x| x as i32)",
"map_.next_value::<::core::option::Option<{}>>()?.map(|x| x as i32)",
resolver.rust_type(path)
)?;
}
FieldModifier::Repeated => {
write!(
writer,
"Some(map.next_value::<::alloc::vec::Vec<{}>>()?.into_iter().map(|x| x as i32).collect())",
"Some(map_.next_value::<::alloc::vec::Vec<{}>>()?.into_iter().map(|x| x as i32).collect())",
resolver.rust_type(path)
)?;
}
_ => {
write!(
writer,
"Some(map.next_value::<{}>()? as i32)",
"Some(map_.next_value::<{}>()? as i32)",
resolver.rust_type(path)
)?;
}
Expand All @@ -903,12 +914,12 @@ fn write_deserialize_field<W: Write>(
match btree_map {
true => write!(
writer,
"{}map.next_value::<alloc::collections::BTreeMap<",
"{}map_.next_value::<alloc::collections::BTreeMap<",
Indent(indent + 2),
)?,
false => write!(
writer,
"{}map.next_value::<std::collections::HashMap<",
"{}map_.next_value::<std::collections::HashMap<",
Indent(indent + 2),
)?,
}
Expand Down Expand Up @@ -970,9 +981,9 @@ fn write_deserialize_field<W: Write>(
FieldType::Message(_) => match field.field_modifier {
FieldModifier::Repeated => {
// No explicit presence for repeated fields
write!(writer, "Some(map.next_value()?)")?;
write!(writer, "Some(map_.next_value()?)")?;
}
_ => write!(writer, "map.next_value()?")?,
_ => write!(writer, "map_.next_value()?")?,
},
},
}
Expand All @@ -999,9 +1010,9 @@ fn write_encode_scalar_field<W: Write>(
None => {
return match field_modifier {
FieldModifier::Optional => {
write!(writer, "map.next_value()?")
write!(writer, "map_.next_value()?")
}
_ => write!(writer, "Some(map.next_value()?)"),
_ => write!(writer, "Some(map_.next_value()?)"),
};
}
};
Expand All @@ -1012,15 +1023,15 @@ fn write_encode_scalar_field<W: Write>(
FieldModifier::Optional => {
writeln!(
writer,
"{}map.next_value::<::core::option::Option<{}>>()?.map(|x| x.0)",
"{}map_.next_value::<::core::option::Option<{}>>()?.map(|x| x.0)",
Indent(indent + 1),
deserializer
)?;
}
FieldModifier::Repeated => {
writeln!(
writer,
"{}Some(map.next_value::<::alloc::vec::Vec<{}>>()?",
"{}Some(map_.next_value::<::alloc::vec::Vec<{}>>()?",
Indent(indent + 1),
deserializer
)?;
Expand All @@ -1033,7 +1044,7 @@ fn write_encode_scalar_field<W: Write>(
_ => {
writeln!(
writer,
"{}Some(map.next_value::<{}>()?.0)",
"{}Some(map_.next_value::<{}>()?.0)",
Indent(indent + 1),
deserializer
)?;
Expand Down
8 changes: 4 additions & 4 deletions pbjson-build/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use prost_types::{
};

use crate::descriptor::{Descriptor, DescriptorSet, MessageDescriptor, Syntax, TypeName, TypePath};
use crate::escape::escape_ident;
use crate::escape::{escape_ident, escape_type};

#[derive(Debug, Clone, Copy)]
pub enum ScalarType {
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct Field {
impl Field {
pub fn rust_type_name(&self) -> String {
use heck::ToUpperCamelCase;
self.name.to_upper_camel_case()
escape_type(self.name.to_upper_camel_case())
}

pub fn rust_field_name(&self) -> String {
Expand Down Expand Up @@ -183,7 +183,7 @@ fn field_modifier(
field: &FieldDescriptorProto,
field_type: &FieldType,
) -> FieldModifier {
let label = Label::from_i32(field.label.expect("expected label")).expect("valid label");
let label = Label::try_from(field.label.expect("expected label")).expect("valid label");
if field.proto3_optional.unwrap_or(false) {
assert_eq!(label, Label::Optional);
return FieldModifier::Optional;
Expand Down Expand Up @@ -217,7 +217,7 @@ fn field_type(descriptors: &DescriptorSet, field: &FieldDescriptorProto) -> Fiel
Some(type_name) => resolve_type(descriptors, type_name.as_str()),
None => {
let scalar =
match Type::from_i32(field.r#type.expect("expected type")).expect("valid type") {
match Type::try_from(field.r#type.expect("expected type")).expect("valid type") {
Type::Double => ScalarType::F64,
Type::Float => ScalarType::F32,
Type::Int64 | Type::Sfixed64 | Type::Sint64 => ScalarType::I64,
Expand Down
4 changes: 2 additions & 2 deletions pbjson-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ publish = false
name = "pbjson_test"

[dependencies]
prost = { version = "0.11", default-features = false, features = ["prost-derive"] }
prost = { version = "0.12", default-features = false, features = ["prost-derive"] }
informalsystems-pbjson = { path = "../pbjson" , default-features = false}
informalsystems-pbjson-types = { path = "../pbjson-types" , default-features = false}
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
Expand All @@ -30,5 +30,5 @@ chrono = "0.4"
serde_json = "1.0"

[build-dependencies]
prost-build = "0.11"
prost-build = "0.12"
informalsystems-pbjson-build = { path = "../pbjson-build" }
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("duplicate_number.proto"),
root.join("escape.proto"),
];

Expand Down
Loading

0 comments on commit 1b44e42

Please sign in to comment.