Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for fieldmask to document reference #245

Merged
merged 3 commits into from Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an assertion for the absence of one of the fields that should not be returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

assertNull(documentSnapshots.get("foobar"));
}

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