Skip to content

Commit 81ef28b

Browse files
Brian Chenandreaowu
authored andcommitted
Separate out mergeMutation from patchMutation (#2306)
1 parent 3e3fa06 commit 81ef28b

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.firebase.firestore.testutil.TestUtil.filter;
2323
import static com.google.firebase.firestore.testutil.TestUtil.key;
2424
import static com.google.firebase.firestore.testutil.TestUtil.map;
25+
import static com.google.firebase.firestore.testutil.TestUtil.mergeMutation;
2526
import static com.google.firebase.firestore.testutil.TestUtil.noChangeEvent;
2627
import static com.google.firebase.firestore.testutil.TestUtil.patchMutation;
2728
import static com.google.firebase.firestore.testutil.TestUtil.query;
@@ -1298,7 +1299,7 @@ public void testHandlesMergeMutationWithTransformThenRemoteEvent() {
12981299
assertTargetId(2);
12991300

13001301
writeMutation(
1301-
patchMutation("foo/bar", map("sum", FieldValue.increment(1)), Collections.EMPTY_LIST));
1302+
mergeMutation("foo/bar", map("sum", FieldValue.increment(1)), Collections.EMPTY_LIST));
13021303
assertChanged(doc("foo/bar", 0, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
13031304
assertContains(doc("foo/bar", 0, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
13041305

firebase-firestore/src/test/java/com/google/firebase/firestore/model/MutationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.firebase.firestore.testutil.TestUtil.fieldMask;
2222
import static com.google.firebase.firestore.testutil.TestUtil.key;
2323
import static com.google.firebase.firestore.testutil.TestUtil.map;
24+
import static com.google.firebase.firestore.testutil.TestUtil.mergeMutation;
2425
import static com.google.firebase.firestore.testutil.TestUtil.mutationResult;
2526
import static com.google.firebase.firestore.testutil.TestUtil.patchMutation;
2627
import static com.google.firebase.firestore.testutil.TestUtil.setMutation;
@@ -84,7 +85,7 @@ public void testAppliesPatchToDocuments() {
8485
public void testAppliesPatchWithMergeToDocuments() {
8586
MaybeDocument baseDoc = deletedDoc("collection/key", 0);
8687
Mutation upsert =
87-
patchMutation(
88+
mergeMutation(
8889
"collection/key", map("foo.bar", "new-bar-value"), Arrays.asList(field("foo.bar")));
8990
MaybeDocument newDoc = upsert.applyToLocalView(baseDoc, baseDoc, Timestamp.now());
9091
Map<String, Object> expectedData = map("foo", map("bar", "new-bar-value"));
@@ -96,7 +97,7 @@ public void testAppliesPatchWithMergeToDocuments() {
9697
public void testAppliesPatchToNullDocWithMergeToDocuments() {
9798
MaybeDocument baseDoc = null;
9899
Mutation upsert =
99-
patchMutation(
100+
mergeMutation(
100101
"collection/key", map("foo.bar", "new-bar-value"), Arrays.asList(field("foo.bar")));
101102
MaybeDocument newDoc = upsert.applyToLocalView(baseDoc, baseDoc, Timestamp.now());
102103
Map<String, Object> expectedData = map("foo", map("bar", "new-bar-value"));

firebase-firestore/src/test/java/com/google/firebase/firestore/remote/RemoteSerializerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.firebase.firestore.testutil.TestUtil.filter;
2222
import static com.google.firebase.firestore.testutil.TestUtil.key;
2323
import static com.google.firebase.firestore.testutil.TestUtil.map;
24+
import static com.google.firebase.firestore.testutil.TestUtil.mergeMutation;
2425
import static com.google.firebase.firestore.testutil.TestUtil.orderBy;
2526
import static com.google.firebase.firestore.testutil.TestUtil.patchMutation;
2627
import static com.google.firebase.firestore.testutil.TestUtil.query;
@@ -362,7 +363,7 @@ public void testEncodesPatchMutation() {
362363
@Test
363364
public void testEncodesPatchMutationWithFieldMask() {
364365
Mutation mutation =
365-
patchMutation("docs/1", map("key", "value", "key2", true), asList(field("key")));
366+
mergeMutation("docs/1", map("key", "value", "key2", true), asList(field("key")));
366367

367368
Write expected =
368369
Write.newBuilder()

firebase-firestore/src/testUtil/java/com/google/firebase/firestore/testutil/TestUtil.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,20 @@ public static SetMutation setMutation(String path, Map<String, Object> values) {
486486
}
487487

488488
public static PatchMutation patchMutation(String path, Map<String, Object> values) {
489-
return patchMutation(path, values, null);
489+
return patchMutationHelper(path, values, Precondition.exists(true), null);
490490
}
491491

492-
public static PatchMutation patchMutation(
493-
String path, Map<String, Object> values, @Nullable List<FieldPath> updateMask) {
494-
// Replace '<DELETE>' from JSON with FieldValue
492+
public static PatchMutation mergeMutation(
493+
String path, Map<String, Object> values, List<FieldPath> updateMask) {
494+
return patchMutationHelper(path, values, Precondition.NONE, updateMask);
495+
}
496+
497+
private static PatchMutation patchMutationHelper(
498+
String path,
499+
Map<String, Object> values,
500+
Precondition precondition,
501+
@Nullable List<FieldPath> updateMask) {
502+
// Replace '<DELETE>' from JSON
495503
for (Entry<String, Object> entry : values.entrySet()) {
496504
if (entry.getValue().equals(DELETE_SENTINEL)) {
497505
values.put(entry.getKey(), FieldValue.delete());
@@ -500,13 +508,15 @@ public static PatchMutation patchMutation(
500508

501509
UserDataReader dataReader = new UserDataReader(DatabaseId.forProject("project"));
502510
ParsedUpdateData parsed = dataReader.parseUpdateData(values);
503-
boolean merge = updateMask != null;
511+
512+
// `mergeMutation()` provides an update mask for the merged fields, whereas `patchMutation()`
513+
// requires the update mask to be parsed from the values.
514+
Collection<FieldPath> mask = updateMask != null ? updateMask : parsed.getFieldMask().getMask();
504515

505516
// We sort the fieldMaskPaths to make the order deterministic in tests. (Otherwise, when we
506517
// flatten a Set to a proto repeated field, we'll end up comparing in iterator order and
507518
// possibly consider {foo,bar} != {bar,foo}.)
508-
SortedSet<FieldPath> fieldMaskPaths =
509-
new TreeSet<>(merge ? updateMask : parsed.getFieldMask().getMask());
519+
SortedSet<FieldPath> fieldMaskPaths = new TreeSet<>(mask);
510520

511521
// The order of the transforms doesn't matter, but we sort them so tests can assume a particular
512522
// order.
@@ -518,7 +528,7 @@ public static PatchMutation patchMutation(
518528
key(path),
519529
parsed.getData(),
520530
FieldMask.fromSet(fieldMaskPaths),
521-
merge ? Precondition.NONE : Precondition.exists(true),
531+
precondition,
522532
fieldTransforms);
523533
}
524534

0 commit comments

Comments
 (0)