Skip to content

Commit

Permalink
Updated FirestoreReference split and a test
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Feb 17, 2024
1 parent 697c09b commit 44f8ebc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion examples/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

println!("Now in the list: {:?}", objects1);

let (parent_path, collection_name, document_id) = objects1.first().unwrap().some_ref.split(&db);
let (parent_path, collection_name, document_id) = objects1
.first()
.unwrap()
.some_ref
.split(db.get_documents_path());

println!("Document ID: {}", document_id);
println!("Collection name: {:?}", collection_name);
Expand Down
2 changes: 1 addition & 1 deletion src/db/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl FirestoreDb {
pub(crate) async fn get_docs_by_ids_from_cache(
&self,
collection_id: &str,
full_doc_ids: &Vec<String>,
full_doc_ids: &[String],
_return_only_fields: &Option<Vec<String>>,
) -> FirestoreResult<FirestoreCachedValue<BoxStream<FirestoreResult<(String, Option<Document>)>>>>
{
Expand Down
32 changes: 26 additions & 6 deletions src/firestore_serde/reference_serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize, Serializer};

use crate::db::split_document_path;
use crate::errors::*;
use crate::{FirestoreDb, FirestoreValue};
use crate::FirestoreValue;

pub(crate) const FIRESTORE_REFERENCE_TYPE_TAG_TYPE: &str = "FirestoreReference";

Expand All @@ -23,19 +23,17 @@ impl FirestoreReference {

/// Splits the reference into parent path, collection name and document id
/// Returns (parent_path, collection_name, document_id)
pub fn split(&self, db: &FirestoreDb) -> (Option<String>, String, String) {
pub fn split(&self, document_path: &str) -> (Option<String>, String, String) {
let (parent_raw_path, document_id) = split_document_path(self.as_str());

let parent_path = parent_raw_path
.replace(db.get_database_path(), "")
.replace("/documents/", "");
let parent_path = parent_raw_path.replace(format!("{}/", document_path).as_str(), "");

let split_pos = parent_path.rfind('/').map(|pos| pos + 1).unwrap_or(0);
if split_pos == 0 {
(None, parent_path, document_id.to_string())
} else {
(
Some(parent_path[..split_pos].to_string()),
Some(parent_path[..split_pos - 1].to_string()),
parent_path[split_pos..].to_string(),
document_id.to_string(),
)
Expand Down Expand Up @@ -342,3 +340,25 @@ pub fn serialize_reference_for_firestore<T: ?Sized + Serialize>(

value.serialize(ReferenceSerializer { none_as_null })
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_reference_split() {
let reference = FirestoreReference::new(
"projects/test-project/databases/(default)/documents/test-collection/test-document-id/child-collection/child-document-id"
.to_string(),
);
let (parent_path, collection_name, document_id) =
reference.split("projects/test-project/databases/(default)/documents");

assert_eq!(
parent_path,
Some("test-collection/test-document-id".to_string())
);
assert_eq!(collection_name, "child-collection");
assert_eq!(document_id, "child-document-id");
}
}

0 comments on commit 44f8ebc

Please sign in to comment.