Skip to content

Commit

Permalink
feat: add support for fieldmask to document reference (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-qlogic committed Jun 15, 2020
1 parent 9b275ca commit 4a846b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Expand Up @@ -354,6 +354,19 @@ public ApiFuture<DocumentSnapshot> get() {
return extractFirst(firestore.getAll(this));
}

/**
* Reads the document referenced by this DocumentReference. If the document doesn't exist, the
* get(FieldMask fieldMask) will return an empty DocumentSnapshot.
*
* @param fieldMask A FieldMask object to retrieve the field value
* @return An ApiFuture that will be resolved with the contents of the Document at this
* DocumentReference, or a failure if the document does not exist
*/
@Nonnull
public ApiFuture<DocumentSnapshot> get(FieldMask fieldMask) {
return extractFirst(firestore.getAll(new DocumentReference[] {this}, fieldMask));
}

/**
* Fetches the subcollections that are direct children of this document.
*
Expand Down
Expand Up @@ -275,6 +275,19 @@ public void deserializeDocumentReference() throws Exception {
assertEquals(documentReference, snapshot.getReference());
}

@Test
public void getFieldWithFieldMask() throws Exception {
doAnswer(getAllResponse(SINGLE_FIELD_PROTO))
.when(firestoreMock)
.streamRequest(
getAllCapture.capture(),
streamObserverCapture.capture(),
Matchers.<ServerStreamingCallable>any());
DocumentSnapshot snapshot = documentReference.get(FieldMask.of(FieldPath.of("foo"))).get();
assertEquals("foo", getAllCapture.getValue().getMask().getFieldPaths(0));
assertEquals("bar", snapshot.get("foo"));
}

@Test
public void deserializesDates() throws Exception {
doAnswer(getAllResponse(ALL_SUPPORTED_TYPES_PROTO))
Expand Down
Expand Up @@ -157,6 +157,15 @@ public void getAllWithFieldMask() throws Exception {
assertEquals(map("foo", "bar"), documentSnapshots.get(0).getData());
}

@Test
public void getFieldMaskWithDocumentReference() throws Exception {
DocumentReference ref = randomColl.document("doc1");
ref.set(ALL_SUPPORTED_TYPES_MAP).get();
DocumentSnapshot documentSnapshots = ref.get(FieldMask.of("foo", "foobar")).get();
assertEquals("bar", documentSnapshots.get("foo"));
assertNull(documentSnapshots.get("foobar"));
}

@Test
public void addDocument() throws Exception {
DocumentReference documentReference = randomColl.add(SINGLE_FIELD_MAP).get();
Expand Down

0 comments on commit 4a846b1

Please sign in to comment.