Skip to content

Commit

Permalink
withPath/withId helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Nov 21, 2023
1 parent 03b16ed commit bb5e6c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app_cv_firestore/lib/src/v2/cv_collection_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class CvCollectionReference<T extends CvFirestoreDocument>
CvCollectionReference<TT> cast<TT extends CvFirestoreDocument>() =>
CvCollectionReference<TT>(path);

/// New path, same type to a different type
CvCollectionReference<T> withPath(String path) =>
CvCollectionReference<T>(path);

/// New path, same type to a different type
CvCollectionReference<T> withId(String id) =>
CvCollectionReference<T>(firestorePathReplaceId(path, id));
@override
String toString() => 'CvCollectionReference<$T>($path)';
}
Expand Down
6 changes: 6 additions & 0 deletions app_cv_firestore/lib/src/v2/cv_document_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class CvDocumentReference<T extends CvFirestoreDocument>
/// Raw document reference
DocumentReference raw(Firestore firestore) => firestore.doc(path);

/// New path, same type to a different type
CvDocumentReference<T> withPath(String path) => CvDocumentReference<T>(path);

/// New path, same type to a different type
CvDocumentReference<T> withId(String id) =>
CvDocumentReference<T>(firestorePathReplaceId(path, id));
@override
String toString() => 'CvDocumentReference<$T>($path)';
}
Expand Down
8 changes: 8 additions & 0 deletions app_cv_firestore/test/app_cv_firestore_v2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,15 @@ void main() {
var docRef = CvDocumentReference<CvFsSingleString>('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<CvFsSingleString>('test/1');
Expand Down

0 comments on commit bb5e6c8

Please sign in to comment.