Skip to content

Commit

Permalink
fix: add support for deleting nested fields that contain periods (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-qlogic committed Jul 16, 2020
1 parent b91c57c commit 84f602e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Expand Up @@ -54,11 +54,25 @@ B getParent() {
* @param path A relative path
*/
B append(String path) {
return append(path, true);
}

/**
* Returns a new path pointing to a child of this Path.
*
* @param path A relative path
* @param splitPath Whether or not the path should be split
*/
B append(String path, boolean splitPath) {
Preconditions.checkArgument(
path != null && !path.isEmpty(), "'path' must be a non-empty String");
ImmutableList.Builder<String> components = ImmutableList.builder();
components.addAll(this.getSegments());
components.add(splitChildPath(path));
if (splitPath) {
components.add(splitChildPath(path));
} else {
components.add(path);
}
return createPathWithSegments(components.build());
}

Expand Down
Expand Up @@ -160,7 +160,9 @@ static Value encodeValue(
Map<String, Object> map = (Map<String, Object>) sanitizedObject;

for (Map.Entry<String, Object> entry : map.entrySet()) {
Value encodedValue = encodeValue(path.append(entry.getKey()), entry.getValue(), options);
Value encodedValue =
encodeValue(
path.append(entry.getKey(), /* splitPath= */ false), entry.getValue(), options);
if (encodedValue != null) {
res.putFields(entry.getKey(), encodedValue);
}
Expand Down
Expand Up @@ -1069,4 +1069,19 @@ public void updateIndividualPojo() throws ExecutionException, InterruptedExcepti
assertCommitEquals(expectedCommit, request);
}
}

@Test
public void deleteNestedFieldUsingFieldPath() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
FieldPath path = FieldPath.of("a.b", "c.d");
documentReference.update(path, FieldValue.delete()).get();
CommitRequest expectedCommit =
commit(
update(
Collections.<String, Value>emptyMap(), Collections.singletonList("`a.b`.`c.d`")));
assertEquals(expectedCommit, commitCapture.getValue());
}
}
Expand Up @@ -93,6 +93,7 @@ public final class LocalFirestoreHelper {
public static final Map<String, Value> EMPTY_MAP_PROTO;

public static final Map<String, Object> SINGLE_FIELD_MAP;
public static final Map<String, Object> SINGLE_FILED_MAP_WITH_DOT;
public static final SingleField SINGLE_FIELD_OBJECT;
public static final Map<String, Value> SINGLE_FIELD_PROTO;
public static final DocumentSnapshot SINGLE_FIELD_SNAPSHOT;
Expand Down Expand Up @@ -723,6 +724,7 @@ public boolean equals(Object o) {
map("inner", Value.newBuilder().setMapValue(MapValue.getDefaultInstance()).build());

SINGLE_FIELD_MAP = map("foo", (Object) "bar");
SINGLE_FILED_MAP_WITH_DOT = map("c.d", (Object) "bar");
SINGLE_FIELD_OBJECT = new SingleField();
SINGLE_FIELD_PROTO = map("foo", Value.newBuilder().setStringValue("bar").build());
UPDATED_POJO_PROTO =
Expand Down
Expand Up @@ -87,6 +87,8 @@ public class ITSystemTest {
private final SingleField SINGLE_FIELD_OBJECT = LocalFirestoreHelper.SINGLE_FIELD_OBJECT;
private final AllSupportedTypes ALL_SUPPORTED_TYPES_OBJECT =
LocalFirestoreHelper.ALL_SUPPORTED_TYPES_OBJECT;
private final Map<String, Object> SINGLE_FILED_MAP_WITH_DOT =
LocalFirestoreHelper.SINGLE_FILED_MAP_WITH_DOT;

@Rule public TestName testName = new TestName();

Expand Down Expand Up @@ -1269,4 +1271,17 @@ public void testInstanceReturnedByGetServiceCanBeUsedToDeserializeAQuery() throw
fs.close();
Query.fromProto(fs, proto);
}

@Test
public void deleteNestedFieldUsingFieldPath() throws Exception {
DocumentReference documentReference = randomColl.document("doc1");
documentReference.set(map("a.b", (Object) SINGLE_FILED_MAP_WITH_DOT)).get();
DocumentSnapshot documentSnapshots = documentReference.get().get();
assertEquals(SINGLE_FILED_MAP_WITH_DOT, documentSnapshots.getData().get("a.b"));

FieldPath path = FieldPath.of("a.b", "c.d");
documentReference.update(path, FieldValue.delete()).get();
documentSnapshots = documentReference.get().get();
assertNull(documentSnapshots.getData().get("c.d"));
}
}

0 comments on commit 84f602e

Please sign in to comment.