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

Reject conflicting updates #2582

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding example with nested map
schmidt-sebastian committed Nov 6, 2017

Verified

This commit was signed with the committer’s verified signature.
commit 071417d0eaf06c5480687d1545ae1500f6eab124
Original file line number Diff line number Diff line change
@@ -76,4 +76,13 @@ B append(BasePath<B> path) {
abstract String[] splitChildPath(String path);

abstract B createPathWithSegments(ImmutableList<String> segments);

boolean isPrefixOf(BasePath<B> field) {
if (field.getSegments().size() < this.getSegments().size()) {
return false;
}

return this.equals(
createPathWithSegments(field.getSegments().subList(0, this.getSegments().size())));
}
}
Original file line number Diff line number Diff line change
@@ -57,10 +57,19 @@ private static Map<String, Object> expandObject(Map<FieldPath, Object> data) {

SortedSet<FieldPath> sortedFields = new TreeSet<>(data.keySet());

FieldPath lastField = null;

for (FieldPath field : sortedFields) {
List<String> segments = field.getSegments();

This comment was marked as spam.

Object value = data.get(field);

This comment was marked as spam.


if (lastField != null) {
if (lastField.isPrefixOf(field)) {

This comment was marked as spam.

throw new IllegalArgumentException(
String.format("Detected ambiguous definition for field '%s'.", lastField));
}
}

Map<String, Object> currentMap = result;

for (int i = 0; i < segments.size(); ++i) {
@@ -71,17 +80,11 @@ private static Map<String, Object> expandObject(Map<FieldPath, Object> data) {
currentMap.put(segments.get(i), new HashMap<>());
}

Object nestedMap = currentMap.get(segments.get(i));

if (!(nestedMap instanceof Map)) {
throw new IllegalArgumentException(
String.format(
"Detected ambiguous definition for field '%s'.",
FieldPath.of(segments.subList(0, i + 1).toArray(new String[] {}))));
}
currentMap = (Map<String, Object>) nestedMap;
currentMap = (Map<String, Object>) currentMap.get(segments.get(i));
}
}

lastField = field;
}

return result;
Original file line number Diff line number Diff line change
@@ -601,6 +601,24 @@ public void updateConflictingFields() throws Exception {
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a.b'.");
}

try {
documentReference
.update("a.b", SINGLE_FIELD_MAP, "a", SINGLE_FIELD_MAP)
.get();
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a'.");
}

try {
documentReference
.update("a.b", SINGLE_FIELD_MAP, "a.b.c", SINGLE_FIELD_MAP)
.get();
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a.b'.");
}
}

@Test