From bb5e6c82f22d6ff83bb66a7b017e41ccdfaf874d Mon Sep 17 00:00:00 2001 From: Alexandre Roux Date: Tue, 21 Nov 2023 17:41:06 +0100 Subject: [PATCH] withPath/withId helpers --- app_cv_firestore/lib/src/v2/cv_collection_reference.dart | 7 +++++++ app_cv_firestore/lib/src/v2/cv_document_reference.dart | 6 ++++++ app_cv_firestore/test/app_cv_firestore_v2_test.dart | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/app_cv_firestore/lib/src/v2/cv_collection_reference.dart b/app_cv_firestore/lib/src/v2/cv_collection_reference.dart index 7e1b252..984fa05 100644 --- a/app_cv_firestore/lib/src/v2/cv_collection_reference.dart +++ b/app_cv_firestore/lib/src/v2/cv_collection_reference.dart @@ -45,6 +45,13 @@ class CvCollectionReference CvCollectionReference cast() => CvCollectionReference(path); + /// New path, same type to a different type + CvCollectionReference withPath(String path) => + CvCollectionReference(path); + + /// New path, same type to a different type + CvCollectionReference withId(String id) => + CvCollectionReference(firestorePathReplaceId(path, id)); @override String toString() => 'CvCollectionReference<$T>($path)'; } diff --git a/app_cv_firestore/lib/src/v2/cv_document_reference.dart b/app_cv_firestore/lib/src/v2/cv_document_reference.dart index 2038764..44c633e 100644 --- a/app_cv_firestore/lib/src/v2/cv_document_reference.dart +++ b/app_cv_firestore/lib/src/v2/cv_document_reference.dart @@ -63,6 +63,12 @@ class CvDocumentReference /// Raw document reference DocumentReference raw(Firestore firestore) => firestore.doc(path); + /// New path, same type to a different type + CvDocumentReference withPath(String path) => CvDocumentReference(path); + + /// New path, same type to a different type + CvDocumentReference withId(String id) => + CvDocumentReference(firestorePathReplaceId(path, id)); @override String toString() => 'CvDocumentReference<$T>($path)'; } diff --git a/app_cv_firestore/test/app_cv_firestore_v2_test.dart b/app_cv_firestore/test/app_cv_firestore_v2_test.dart index 1345208..3314c28 100644 --- a/app_cv_firestore/test/app_cv_firestore_v2_test.dart +++ b/app_cv_firestore/test/app_cv_firestore_v2_test.dart @@ -311,7 +311,15 @@ void main() { var docRef = CvDocumentReference('test/1'); var rawRef = docRef.raw(firestore); expect(rawRef.path, 'test/1'); + expect(rawRef.path, 'test/1'); expect(rawRef.cv(), docRef); + + expect(docRef.withPath('other/2').path, 'other/2'); + expect(docRef.withId('2').path, 'test/2'); + + expect(docRef.parent.withId('other').path, 'other'); + expect( + docRef.parent.withPath('other/2/sub').withId('4').path, 'other/2/4'); }); test('document', () async { var docRef = CvDocumentReference('test/1');