Search in sources :

Example 16 with ObjectValue

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);
}
Also used : ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseContext(com.google.firebase.firestore.core.UserData.ParseContext) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) NullValue(com.google.protobuf.NullValue) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) MapValue(com.google.firestore.v1.MapValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue) ArrayValue(com.google.firestore.v1.ArrayValue) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue)

Example 17 with ObjectValue

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;
}
Also used : Status(io.grpc.Status) DocumentRemove(com.google.firestore.v1.DocumentRemove) MutableDocument(com.google.firebase.firestore.model.MutableDocument) WatchTargetChangeType(com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType) DocumentChange(com.google.firestore.v1.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) DocumentDelete(com.google.firestore.v1.DocumentDelete) ObjectValue(com.google.firebase.firestore.model.ObjectValue) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)

Example 18 with ObjectValue

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);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) DocumentKey(com.google.firebase.firestore.model.DocumentKey)

Example 19 with ObjectValue

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());
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) Test(org.junit.Test)

Example 20 with ObjectValue

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;
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldPath(com.google.firebase.firestore.model.FieldPath) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value)

Aggregations

ObjectValue (com.google.firebase.firestore.model.ObjectValue)24 Value (com.google.firestore.v1.Value)12 FieldPath (com.google.firebase.firestore.model.FieldPath)8 Test (org.junit.Test)8 MutableDocument (com.google.firebase.firestore.model.MutableDocument)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)4 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)4 ArrayValue (com.google.firestore.v1.ArrayValue)4 MapValue (com.google.firestore.v1.MapValue)4 NullValue (com.google.protobuf.NullValue)4 ArrayRemoveFieldValue (com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)3 ArrayUnionFieldValue (com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue)3 DeleteFieldValue (com.google.firebase.firestore.FieldValue.DeleteFieldValue)3 ServerTimestampFieldValue (com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)3 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3