Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Payload pretty-print from Display to Debug #805

Merged
merged 7 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion sdk-core-protos/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{env, path::PathBuf};
use std::{
env,
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
};

static ALWAYS_SERDE: &str = "#[cfg_attr(not(feature = \"serde_serialize\"), \
derive(::serde::Serialize, ::serde::Deserialize))]";
Expand Down Expand Up @@ -131,5 +136,58 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
],
)?;

let file_path = out.join("temporal.api.common.v1.rs");
post_process_payload_file(file_path)?;

yuandrew marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}

fn post_process_payload_file(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let file_contents: BufReader<File> = BufReader::new(File::open(file_path.clone())?);
let mut new_file_contents = String::new();

let mut found_payload = false;
let mut written = false;
for line in file_contents.lines() {
let line = line?;
if line.contains("pub struct Payload {") {
new_file_contents.push_str("#[prost(skip_debug)]\n");
found_payload = true;
}

// Write the current line to the new content
new_file_contents.push_str(&line);
new_file_contents.push('\n');

if found_payload && line == "}" && !written {
let debug_impl = r#"
impl std::fmt::Debug for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.data.len() > 64 {
yuandrew marked this conversation as resolved.
Show resolved Hide resolved
let mut windows = self.data.as_slice().windows(32);
write!(
f,
"[{}..{}]",
BASE64_STANDARD.encode(windows.next().unwrap_or_default()),
BASE64_STANDARD.encode(windows.next_back().unwrap_or_default())
)
} else {
write!(f, "[{}]", BASE64_STANDARD.encode(&self.data))
}
}
}
"#;
new_file_contents.push_str(debug_impl);
new_file_contents.push('\n');

written = true;
}
}

assert!(found_payload);

// Replace the original file with the modified content
std::fs::write(&file_path, new_file_contents)?;

Ok(())
}
12 changes: 1 addition & 11 deletions sdk-core-protos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,17 +1750,7 @@ pub mod temporal {

impl Display for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.data.len() > 64 {
let mut windows = self.data.as_slice().windows(32);
write!(
f,
"[{}..{}]",
BASE64_STANDARD.encode(windows.next().unwrap_or_default()),
BASE64_STANDARD.encode(windows.next_back().unwrap_or_default())
)
} else {
write!(f, "[{}]", BASE64_STANDARD.encode(&self.data))
}
write!(f, "{:?}", self)
}
}

Expand Down
Loading