Last active
July 14, 2023 17:58
-
-
Save stevenliebregt/bdee67757f85fbe6afb1e5679c5eed86 to your computer and use it in GitHub Desktop.
Serializable parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(test)] | |
mod tests { | |
use postgres_types::{ToSql, FromSql}; | |
use serde::{Deserialize, Serialize}; | |
use serde_json::json; | |
#[test] | |
fn it_works() { | |
#[derive(Debug, Serialize, Deserialize, ToSql, FromSql)] | |
struct MyData { | |
a: i32, | |
b: String, | |
} | |
let my_data = MyData { | |
a: 1, | |
b: String::from("Hello, world!") | |
}; | |
let my_params = json!({ | |
"an_int": &1, | |
"a_bool": true, | |
"my_data": &my_data, | |
}); | |
println!("I can still access my_data: {:?}", my_data); | |
dbg!(&my_params); | |
println!("Params as pretty JSON: {}", serde_json::to_string_pretty(&my_params).unwrap()); | |
let mut final_params: Vec<&(dyn ToSql + Sync)> = vec![&8008135; my_params.as_object().unwrap().len()]; | |
final_params[0] = my_params.get("an_int").unwrap(); | |
final_params[1] = my_params.get("a_bool").unwrap(); | |
final_params[2] = my_params.get("my_data").unwrap(); | |
dbg!(final_params); | |
let deserialized_params: serde_json::Value = serde_json::from_str(&serde_json::to_string_pretty(&my_params).unwrap()).unwrap(); | |
dbg!(&deserialized_params); | |
for (key, value) in deserialized_params.as_object().unwrap().iter() { | |
dbg!(key, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment