Skip to content

Commit

Permalink
twiq: Add firestore_rpc::helper::get_field_as_timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Oct 29, 2022
1 parent a1596d8 commit ab7b038
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions twiq/crates/db/src/firestore_rpc.rs
Expand Up @@ -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<Timestamp> {
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() {
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit ab7b038

Please sign in to comment.