Skip to content

Commit

Permalink
Add infra::firestore::timestamp mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 3, 2023
1 parent 6bb369a commit 0beeba1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
23 changes: 11 additions & 12 deletions 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,
Expand All @@ -8,14 +9,14 @@ 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};

use self::{
document::Document,
path::{CollectionPath, DocumentPath},
timestamp::Timestamp,
};

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -221,9 +226,7 @@ mod tests {
// reset
let (documents, _) = client.list::<V>(&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
Expand Down Expand Up @@ -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<V> = client
.update(
got.name(),
V {
k1: "v2".to_owned(), // "v1" -> "v2
},
current_update_time,
got.update_time(),
)
.await?;
assert_eq!(
Expand All @@ -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(())
}
Expand Down
11 changes: 5 additions & 6 deletions rust/crates/web/src/infra/firestore/document.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -36,12 +35,12 @@ impl<T: serde::de::DeserializeOwned> Document<T> {
update_time,
}: FirestoreDocument,
) -> Result<Self, Error> {
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,
Expand All @@ -50,7 +49,7 @@ impl<T: serde::de::DeserializeOwned> Document<T> {
})
}

pub fn create_time(self) -> Timestamp {
pub fn create_time(&self) -> Timestamp {
self.create_time
}

Expand All @@ -62,7 +61,7 @@ impl<T: serde::de::DeserializeOwned> Document<T> {
&self.name
}

pub fn update_time(self) -> Timestamp {
pub fn update_time(&self) -> Timestamp {
self.update_time
}
}
17 changes: 17 additions & 0 deletions 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<prost_types::Timestamp> for Timestamp {
fn from(prost_types::Timestamp { seconds, nanos }: prost_types::Timestamp) -> Self {
Self { seconds, nanos }
}
}

impl From<Timestamp> for prost_types::Timestamp {
fn from(Timestamp { seconds, nanos }: Timestamp) -> Self {
Self { seconds, nanos }
}
}

0 comments on commit 0beeba1

Please sign in to comment.