From 0beeba19ae142eb5f02ce0f32104d5ecc4f21890 Mon Sep 17 00:00:00 2001 From: bouzuya Date: Fri, 3 Nov 2023 19:10:49 +0900 Subject: [PATCH] Add infra::firestore::timestamp mod --- rust/crates/web/src/infra/firestore.rs | 23 +++++++++---------- .../web/src/infra/firestore/document.rs | 11 ++++----- .../web/src/infra/firestore/timestamp.rs | 17 ++++++++++++++ 3 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 rust/crates/web/src/infra/firestore/timestamp.rs diff --git a/rust/crates/web/src/infra/firestore.rs b/rust/crates/web/src/infra/firestore.rs index d72080d..1f4c7b6 100644 --- a/rust/crates/web/src/infra/firestore.rs +++ b/rust/crates/web/src/infra/firestore.rs @@ -1,5 +1,6 @@ mod document; mod path; +mod timestamp; use google_api_proto::google::firestore::v1::{ firestore_client::FirestoreClient, precondition::ConditionType, value::ValueType, @@ -8,7 +9,6 @@ use google_api_proto::google::firestore::v1::{ UpdateDocumentRequest, }; use google_authz::{Credentials, GoogleAuthz}; -use prost_types::Timestamp; use serde::{de::DeserializeOwned, Serialize}; use serde_firestore_value::to_value; use tonic::{transport::Channel, Request}; @@ -16,6 +16,7 @@ use tonic::{transport::Channel, Request}; use self::{ document::Document, path::{CollectionPath, DocumentPath}, + timestamp::Timestamp, }; #[derive(Debug, thiserror::Error)] @@ -107,7 +108,9 @@ impl Client { .delete_document(Request::new(DeleteDocumentRequest { name: document_path.path(), current_document: Some(Precondition { - condition_type: Some(ConditionType::UpdateTime(current_update_time)), + condition_type: Some(ConditionType::UpdateTime(prost_types::Timestamp::from( + current_update_time, + ))), }), })) .await?; @@ -191,7 +194,9 @@ impl Client { update_mask: None, mask: None, current_document: Some(Precondition { - condition_type: Some(ConditionType::UpdateTime(current_update_time)), + condition_type: Some(ConditionType::UpdateTime(prost_types::Timestamp::from( + current_update_time, + ))), }), })) .await?; @@ -221,9 +226,7 @@ mod tests { // reset let (documents, _) = client.list::(&collection_path).await?; for doc in documents { - // TODO: Document::update_time - let current_update_time = doc.clone().update_time(); - client.delete(doc.name(), current_update_time).await?; + client.delete(doc.name(), doc.update_time()).await?; } // CREATE @@ -251,15 +254,13 @@ mod tests { assert_eq!(next_page_token, ""); // UPDATE - // TODO: Document::update_time - let current_update_time = got.clone().update_time(); let updated: Document = client .update( got.name(), V { k1: "v2".to_owned(), // "v1" -> "v2 }, - current_update_time, + got.update_time(), ) .await?; assert_eq!( @@ -270,9 +271,7 @@ mod tests { ); // DELETE - client - .delete(updated.name(), updated.clone().update_time()) - .await?; + client.delete(updated.name(), updated.update_time()).await?; Ok(()) } diff --git a/rust/crates/web/src/infra/firestore/document.rs b/rust/crates/web/src/infra/firestore/document.rs index 3459e91..c2c700a 100644 --- a/rust/crates/web/src/infra/firestore/document.rs +++ b/rust/crates/web/src/infra/firestore/document.rs @@ -3,9 +3,8 @@ use std::str::FromStr; use google_api_proto::google::firestore::v1::{ value::ValueType, Document as FirestoreDocument, MapValue, Value, }; -use prost_types::Timestamp; -use super::path::DocumentPath; +use super::{path::DocumentPath, timestamp::Timestamp}; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -36,12 +35,12 @@ impl Document { update_time, }: FirestoreDocument, ) -> Result { - let create_time = create_time.ok_or(Error::CreateTimeIsNone)?; + let create_time = Timestamp::from(create_time.ok_or(Error::CreateTimeIsNone)?); let data: T = serde_firestore_value::from_value(&Value { value_type: Some(ValueType::MapValue(MapValue { fields })), })?; let name = DocumentPath::from_str(name.as_str())?; - let update_time = update_time.ok_or(Error::UpdateTimeIsNone)?; + let update_time = Timestamp::from(update_time.ok_or(Error::UpdateTimeIsNone)?); Ok(Self { create_time, data, @@ -50,7 +49,7 @@ impl Document { }) } - pub fn create_time(self) -> Timestamp { + pub fn create_time(&self) -> Timestamp { self.create_time } @@ -62,7 +61,7 @@ impl Document { &self.name } - pub fn update_time(self) -> Timestamp { + pub fn update_time(&self) -> Timestamp { self.update_time } } diff --git a/rust/crates/web/src/infra/firestore/timestamp.rs b/rust/crates/web/src/infra/firestore/timestamp.rs new file mode 100644 index 0000000..11d221f --- /dev/null +++ b/rust/crates/web/src/infra/firestore/timestamp.rs @@ -0,0 +1,17 @@ +#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +pub struct Timestamp { + seconds: i64, + nanos: i32, +} + +impl From for Timestamp { + fn from(prost_types::Timestamp { seconds, nanos }: prost_types::Timestamp) -> Self { + Self { seconds, nanos } + } +} + +impl From for prost_types::Timestamp { + fn from(Timestamp { seconds, nanos }: Timestamp) -> Self { + Self { seconds, nanos } + } +}