Skip to content

Commit

Permalink
properly serialize null values (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavadeli authored Jan 10, 2025
1 parent 80ca6ec commit 1aaff7b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ version = "0.1.2"
async-trait = "0.1.83"
axum = "0.6.20"
base64 = "0.22.1"
bytes = "1.9.0"
clap = "4.5.20"
color-eyre = "0.6.3"
console-subscriber = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/googleapis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ doctest = false

[dependencies]
base64 = { workspace = true }
bytes = { workspace = true }
prost = { workspace = true }
serde = { workspace = true, features = ["serde_derive"] }
thiserror = { workspace = true }
Expand All @@ -21,6 +20,7 @@ tonic-build = { workspace = true }
[dev-dependencies]
itertools = { workspace = true }
rstest = { workspace = true }
serde_json = { workspace = true }

# Do not use workspace lints here, because generated code is not that clean.
[lints.rust]
Expand Down
6 changes: 5 additions & 1 deletion crates/googleapis/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ fn main() {
.enum_attribute(".google.firestore.v1.Value", "#[serde(untagged)]")
.field_attribute(
"bytes_value",
r#"#[serde(serialize_with="crate::bytes_base64::serialize")]"#,
r#"#[serde(serialize_with="crate::ser::as_base64")]"#,
)
.field_attribute(
"value_type.null_value",
r#"#[serde(serialize_with="crate::ser::as_null")]"#,
)
.compile(
&["include/google/firestore/v1/firestore.proto"],
Expand Down
7 changes: 0 additions & 7 deletions crates/googleapis/src/bytes_base64.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/googleapis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

tonic::include_proto!("googleapis");

mod bytes_base64;
mod ser;
mod timestamp_ext;
mod transaction_options_ext;
mod value_ext;
28 changes: 28 additions & 0 deletions crates/googleapis/src/ser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use base64::{display::Base64Display, prelude::BASE64_STANDARD_NO_PAD};
use serde::Serializer;

pub(crate) fn as_base64<T: AsRef<[u8]>, S: Serializer>(v: T, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(&Base64Display::new(v.as_ref(), &BASE64_STANDARD_NO_PAD))
}

pub(crate) fn as_null<T, S: Serializer>(_v: T, s: S) -> Result<S::Ok, S::Error> {
s.serialize_unit()
}

#[cfg(test)]
mod tests {
use prost::bytes::Bytes;

use crate::google::firestore::v1::{value::ValueType, Value};

#[test]
fn serialization() {
let values = [
Value::null(),
Value {
value_type: Some(ValueType::BytesValue(Bytes::from_static(b"\xff\xff\xbe"))),
},
];
assert_eq!(serde_json::to_string(&values).unwrap(), r#"[null,"//++"]"#);
}
}
11 changes: 5 additions & 6 deletions crates/googleapis/src/value_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,13 @@ impl PartialOrd for LatLng {
const REFERENCE_NAME_MIN_ID: &str = "__id-9223372036854775808__";

fn prep_ref_for_cmp(path: &str) -> Cow<str> {
(|| -> Option<Cow<str>> {
(|| {
let path = path.strip_suffix(REFERENCE_NAME_MIN_ID)?;
let collection = path.strip_suffix('/')?;
let result = match collection.strip_suffix('\0') {
Some(collection) => Cow::Owned(collection.to_string() + "@"),
None => Cow::Borrowed(path),
};
Some(result)
match collection.strip_suffix('\0') {
Some(collection) => Some(Cow::Owned(collection.to_string() + "@")),
None => Some(Cow::Borrowed(path)),
}
})()
.unwrap_or(Cow::Borrowed(path))
}

0 comments on commit 1aaff7b

Please sign in to comment.