use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class UserDataReader method parseUpdateData.
/**
* Parses the update data from the update(field, value, field, value...) varargs call, accepting
* both strings and FieldPaths.
*/
public ParsedUpdateData parseUpdateData(List<Object> fieldsAndValues) {
// fieldsAndValues.length and alternating types should already be validated by
// Util.collectUpdateArguments().
hardAssert(fieldsAndValues.size() % 2 == 0, "Expected fieldAndValues to contain an even number of elements");
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
ParseContext context = accumulator.rootContext();
ObjectValue updateData = new ObjectValue();
Iterator<Object> iterator = fieldsAndValues.iterator();
while (iterator.hasNext()) {
Object fieldPath = iterator.next();
Object fieldValue = iterator.next();
hardAssert(fieldPath instanceof String || fieldPath instanceof com.google.firebase.firestore.FieldPath, "Expected argument to be String or FieldPath.");
FieldPath parsedField;
if (fieldPath instanceof String) {
parsedField = com.google.firebase.firestore.FieldPath.fromDotSeparatedPath((String) fieldPath).getInternalPath();
} else {
parsedField = ((com.google.firebase.firestore.FieldPath) fieldPath).getInternalPath();
}
if (fieldValue instanceof DeleteFieldValue) {
// Add it to the field mask, but don't add anything to updateData.
context.addToFieldMask(parsedField);
} else {
Value parsedValue = convertAndParseFieldData(fieldValue, context.childContext(parsedField));
if (parsedValue != null) {
context.addToFieldMask(parsedField);
updateData.set(parsedField, parsedValue);
}
}
}
return accumulator.toUpdateData(updateData);
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class RemoteSerializer method decodeWatchChange.
// Watch changes
public WatchChange decodeWatchChange(ListenResponse protoChange) {
WatchChange watchChange;
switch(protoChange.getResponseTypeCase()) {
case TARGET_CHANGE:
com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange();
WatchTargetChangeType changeType;
Status cause = null;
switch(targetChange.getTargetChangeType()) {
case NO_CHANGE:
changeType = WatchTargetChangeType.NoChange;
break;
case ADD:
changeType = WatchTargetChangeType.Added;
break;
case REMOVE:
changeType = WatchTargetChangeType.Removed;
cause = fromStatus(targetChange.getCause());
break;
case CURRENT:
changeType = WatchTargetChangeType.Current;
break;
case RESET:
changeType = WatchTargetChangeType.Reset;
break;
case UNRECOGNIZED:
default:
throw new IllegalArgumentException("Unknown target change type");
}
watchChange = new WatchTargetChange(changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause);
break;
case DOCUMENT_CHANGE:
DocumentChange docChange = protoChange.getDocumentChange();
List<Integer> added = docChange.getTargetIdsList();
List<Integer> removed = docChange.getRemovedTargetIdsList();
DocumentKey key = decodeKey(docChange.getDocument().getName());
SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime());
hardAssert(!version.equals(SnapshotVersion.NONE), "Got a document change without an update time");
ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap());
MutableDocument document = MutableDocument.newFoundDocument(key, version, data);
watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document);
break;
case DOCUMENT_DELETE:
DocumentDelete docDelete = protoChange.getDocumentDelete();
removed = docDelete.getRemovedTargetIdsList();
key = decodeKey(docDelete.getDocument());
// Note that version might be unset in which case we use SnapshotVersion.NONE
version = decodeVersion(docDelete.getReadTime());
MutableDocument doc = MutableDocument.newNoDocument(key, version);
watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc);
break;
case DOCUMENT_REMOVE:
DocumentRemove docRemove = protoChange.getDocumentRemove();
removed = docRemove.getRemovedTargetIdsList();
key = decodeKey(docRemove.getDocument());
watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null);
break;
case FILTER:
com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter();
// TODO: implement existence filter parsing (see b/33076578)
ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount());
int targetId = protoFilter.getTargetId();
watchChange = new ExistenceFilterWatchChange(targetId, filter);
break;
case RESPONSETYPE_NOT_SET:
default:
throw new IllegalArgumentException("Unknown change type set");
}
return watchChange;
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class RemoteSerializer method decodeFoundDocument.
private MutableDocument decodeFoundDocument(BatchGetDocumentsResponse response) {
Assert.hardAssert(response.getResultCase().equals(ResultCase.FOUND), "Tried to deserialize a found document from a missing document.");
DocumentKey key = decodeKey(response.getFound().getName());
ObjectValue value = ObjectValue.fromMap(response.getFound().getFieldsMap());
SnapshotVersion version = decodeVersion(response.getFound().getUpdateTime());
hardAssert(!version.equals(SnapshotVersion.NONE), "Got a document response with no snapshot version");
return MutableDocument.newFoundDocument(key, version, value);
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class QuerySnapshotTest method testEquals.
@Test
public void testEquals() {
ObjectValue firstValue = wrapObject("a", 1);
ObjectValue secondValue = wrapObject("b", 1);
QuerySnapshot foo = TestUtil.querySnapshot("foo", map(), map("a", firstValue), true, false);
QuerySnapshot fooDup = TestUtil.querySnapshot("foo", map(), map("a", firstValue), true, false);
QuerySnapshot differentPath = TestUtil.querySnapshot("bar", map(), map("a", firstValue), true, false);
QuerySnapshot differentDoc = TestUtil.querySnapshot("foo", map(), map("a", secondValue), true, false);
QuerySnapshot noPendingWrites = TestUtil.querySnapshot("foo", map(), map("a", firstValue), false, false);
QuerySnapshot fromCache = TestUtil.querySnapshot("foo", map(), map("a", firstValue), true, true);
assertEquals(foo, fooDup);
assertNotEquals(foo, differentPath);
assertNotEquals(foo, differentDoc);
assertNotEquals(foo, noPendingWrites);
assertNotEquals(foo, fromCache);
// Note: `foo` and `differentDoc` have the same hash code since we no longer take document
// contents into account.
assertEquals(foo.hashCode(), fooDup.hashCode());
assertNotEquals(foo.hashCode(), differentPath.hashCode());
assertNotEquals(foo.hashCode(), noPendingWrites.hashCode());
assertNotEquals(foo.hashCode(), fromCache.hashCode());
}
use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.
the class SetMutation method applyToLocalView.
@Override
public FieldMask applyToLocalView(MutableDocument document, @Nullable FieldMask previousMask, Timestamp localWriteTime) {
verifyKeyMatches(document);
if (!this.getPrecondition().isValidFor(document)) {
return previousMask;
}
Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
ObjectValue localValue = value.clone();
localValue.setAll(transformResults);
document.convertToFoundDocument(document.getVersion(), localValue).setHasLocalMutations();
// SetMutation overwrites all fields.
return null;
}
Aggregations