From ab7b03852f3ce4729a2f9575c1068c3ea2f7a8bd Mon Sep 17 00:00:00 2001 From: bouzuya Date: Sat, 29 Oct 2022 09:25:25 +0900 Subject: [PATCH] twiq: Add firestore_rpc::helper::get_field_as_timestamp --- twiq/crates/db/src/firestore_rpc.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/twiq/crates/db/src/firestore_rpc.rs b/twiq/crates/db/src/firestore_rpc.rs index ec9d7ade..128af03e 100644 --- a/twiq/crates/db/src/firestore_rpc.rs +++ b/twiq/crates/db/src/firestore_rpc.rs @@ -31,6 +31,10 @@ pub mod helper { document.fields.get(key).map(value_as_str_unchecked) } + pub fn get_field_as_timestamp(document: &Document, key: &str) -> Option { + document.fields.get(key).map(value_to_timestamp_unchecked) + } + // panic if value_type is not string pub fn value_as_str_unchecked(value: &Value) -> &str { match value.value_type.as_ref() { @@ -129,6 +133,29 @@ pub mod helper { ); } + #[test] + fn get_field_as_timestamp_test() -> anyhow::Result<()> { + let timestamp = Timestamp::from_str("2020-01-02T15:04:05Z")?; + assert_eq!( + get_field_as_timestamp( + &Document { + name: "name".to_owned(), + fields: { + let mut fields = HashMap::new(); + fields + .insert("key".to_owned(), value_from_timestamp(timestamp.clone())); + fields + }, + create_time: None, + update_time: None + }, + "key" + ), + Some(timestamp) + ); + Ok(()) + } + #[test] fn value_as_str_unchecked_test() { assert_eq!(