Skip to content

Commit 4a846b1

Browse files
authored
feat: add support for fieldmask to document reference (#245)
1 parent 9b275ca commit 4a846b1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentReference.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,19 @@ public ApiFuture<DocumentSnapshot> get() {
354354
return extractFirst(firestore.getAll(this));
355355
}
356356

357+
/**
358+
* Reads the document referenced by this DocumentReference. If the document doesn't exist, the
359+
* get(FieldMask fieldMask) will return an empty DocumentSnapshot.
360+
*
361+
* @param fieldMask A FieldMask object to retrieve the field value
362+
* @return An ApiFuture that will be resolved with the contents of the Document at this
363+
* DocumentReference, or a failure if the document does not exist
364+
*/
365+
@Nonnull
366+
public ApiFuture<DocumentSnapshot> get(FieldMask fieldMask) {
367+
return extractFirst(firestore.getAll(new DocumentReference[] {this}, fieldMask));
368+
}
369+
357370
/**
358371
* Fetches the subcollections that are direct children of this document.
359372
*

google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ public void deserializeDocumentReference() throws Exception {
275275
assertEquals(documentReference, snapshot.getReference());
276276
}
277277

278+
@Test
279+
public void getFieldWithFieldMask() throws Exception {
280+
doAnswer(getAllResponse(SINGLE_FIELD_PROTO))
281+
.when(firestoreMock)
282+
.streamRequest(
283+
getAllCapture.capture(),
284+
streamObserverCapture.capture(),
285+
Matchers.<ServerStreamingCallable>any());
286+
DocumentSnapshot snapshot = documentReference.get(FieldMask.of(FieldPath.of("foo"))).get();
287+
assertEquals("foo", getAllCapture.getValue().getMask().getFieldPaths(0));
288+
assertEquals("bar", snapshot.get("foo"));
289+
}
290+
278291
@Test
279292
public void deserializesDates() throws Exception {
280293
doAnswer(getAllResponse(ALL_SUPPORTED_TYPES_PROTO))

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ public void getAllWithFieldMask() throws Exception {
157157
assertEquals(map("foo", "bar"), documentSnapshots.get(0).getData());
158158
}
159159

160+
@Test
161+
public void getFieldMaskWithDocumentReference() throws Exception {
162+
DocumentReference ref = randomColl.document("doc1");
163+
ref.set(ALL_SUPPORTED_TYPES_MAP).get();
164+
DocumentSnapshot documentSnapshots = ref.get(FieldMask.of("foo", "foobar")).get();
165+
assertEquals("bar", documentSnapshots.get("foo"));
166+
assertNull(documentSnapshots.get("foobar"));
167+
}
168+
160169
@Test
161170
public void addDocument() throws Exception {
162171
DocumentReference documentReference = randomColl.add(SINGLE_FIELD_MAP).get();

0 commit comments

Comments
 (0)