Skip to content

Commit

Permalink
show bytes_value (byte slices) as base64 in json (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavadeli authored Jan 10, 2025
1 parent f769975 commit 80ca6ec
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ version = "0.1.2"
[workspace.dependencies]
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
4 changes: 3 additions & 1 deletion crates/googleapis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ edition = "2021"
doctest = false

[dependencies]
time = { workspace = true, features = ["formatting", "macros"] }
base64 = { workspace = true }
bytes = { workspace = true }
prost = { workspace = true }
serde = { workspace = true, features = ["serde_derive"] }
thiserror = { workspace = true }
time = { workspace = true, features = ["formatting", "macros"] }
tonic = { workspace = true }

[build-dependencies]
Expand Down
22 changes: 13 additions & 9 deletions crates/googleapis/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ fn main() {
.build_client(false)
.include_file("googleapis.rs")
.compile_well_known_types(true)
.message_attribute(".google.type.LatLng", "#[derive(serde::Serialize, Copy)]")
.message_attribute(
.bytes(["bytes_value"])
.type_attribute(".google.type.LatLng", "#[derive(serde::Serialize, Copy)]")
.type_attribute(
".google.protobuf.Timestamp",
"#[derive(Eq, PartialOrd, Ord, serde::Serialize, Copy)]",
)
.message_attribute(
.type_attribute(
".google.firestore.v1.Document",
"#[derive(serde::Serialize)]",
)
.message_attribute(".google.firestore.v1.Value", "#[derive(serde::Serialize)]")
.message_attribute(".google.firestore.v1.Value", "#[serde(transparent)]")
.enum_attribute(".google.firestore.v1.Value", "#[derive(serde::Serialize)]")
.enum_attribute(".google.firestore.v1.Value", "#[serde(untagged)]")
.message_attribute(
.type_attribute(
".google.firestore.v1.ArrayValue",
"#[derive(serde::Serialize)]",
)
.message_attribute(
.type_attribute(
".google.firestore.v1.MapValue",
"#[derive(serde::Serialize)]",
)
.type_attribute(".google.firestore.v1.Value", "#[derive(serde::Serialize)]")
.message_attribute(".google.firestore.v1.Value", "#[serde(transparent)]")
.enum_attribute(".google.firestore.v1.Value", "#[serde(untagged)]")
.field_attribute(
"bytes_value",
r#"#[serde(serialize_with="crate::bytes_base64::serialize")]"#,
)
.compile(
&["include/google/firestore/v1/firestore.proto"],
&["include"],
Expand Down
7 changes: 7 additions & 0 deletions crates/googleapis/src/bytes_base64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use base64::{display::Base64Display, prelude::BASE64_STANDARD_NO_PAD};
use bytes::Bytes;
use serde::Serializer;

pub(crate) fn serialize<S: Serializer>(v: &Bytes, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(&Base64Display::new(v, &BASE64_STANDARD_NO_PAD))
}
2 changes: 2 additions & 0 deletions crates/googleapis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![allow(rustdoc::invalid_html_tags)]

tonic::include_proto!("googleapis");

mod bytes_base64;
mod timestamp_ext;
mod transaction_options_ext;
mod value_ext;

0 comments on commit 80ca6ec

Please sign in to comment.