Search in sources :

Example 6 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class TestUtil method querySnapshot.

/**
 * A convenience method for creating a particular query snapshot for tests.
 *
 * @param path To be used in constructing the query.
 * @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each entry maps to a
 *     document, with the key being the document id, and the value being the document contents.
 * @param docsToAdd Specifies data to be added into the query snapshot as of now. Each entry maps
 *     to a document, with the key being the document id, and the value being the document
 *     contents.
 * @param isFromCache Whether the query snapshot is cache result.
 * @return A query snapshot that consists of both sets of documents.
 */
public static QuerySnapshot querySnapshot(String path, Map<String, ObjectValue> oldDocs, Map<String, ObjectValue> docsToAdd, boolean hasPendingWrites, boolean isFromCache) {
    DocumentSet oldDocuments = docSet(Document.KEY_COMPARATOR);
    ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
    for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
        String docKey = path + "/" + pair.getKey();
        MutableDocument doc = doc(docKey, 1L, pair.getValue());
        if (hasPendingWrites) {
            doc.setHasCommittedMutations();
            mutatedKeys = mutatedKeys.insert(key(docKey));
        }
        oldDocuments = oldDocuments.add(doc);
    }
    DocumentSet newDocuments = docSet(Document.KEY_COMPARATOR);
    List<DocumentViewChange> documentChanges = new ArrayList<>();
    for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
        String docKey = path + "/" + pair.getKey();
        MutableDocument docToAdd = doc(docKey, 1L, pair.getValue());
        if (hasPendingWrites) {
            docToAdd.setHasCommittedMutations();
            mutatedKeys = mutatedKeys.insert(key(docKey));
        }
        newDocuments = newDocuments.add(docToAdd);
        documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));
    }
    ViewSnapshot viewSnapshot = new ViewSnapshot(com.google.firebase.firestore.testutil.TestUtil.query(path), newDocuments, oldDocuments, documentChanges, isFromCache, mutatedKeys, /* didSyncStateChange= */
    true, /* excludesMetadataChanges= */
    false);
    return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) ObjectValue(com.google.firebase.firestore.model.ObjectValue) DocumentViewChange(com.google.firebase.firestore.core.DocumentViewChange) DocumentKey(com.google.firebase.firestore.model.DocumentKey) DocumentSet(com.google.firebase.firestore.model.DocumentSet) Map(java.util.Map)

Example 7 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class UserDataReader method parseSetData.

/**
 * Parse document data from a non-merge {@code set()} call.
 *
 * @param input A map or POJO object representing document data.
 */
public ParsedSetData parseSetData(Object input) {
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Set);
    ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());
    return accumulator.toSetData(updateData);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator)

Example 8 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class UserDataReader method parseUpdateData.

/**
 * Parse update data from an {@code update()} call.
 */
public ParsedUpdateData parseUpdateData(Map<String, Object> data) {
    checkNotNull(data, "Provided update data must not be null.");
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
    ParseContext context = accumulator.rootContext();
    ObjectValue updateData = new ObjectValue();
    for (Entry<String, Object> entry : data.entrySet()) {
        FieldPath fieldPath = com.google.firebase.firestore.FieldPath.fromDotSeparatedPath(entry.getKey()).getInternalPath();
        Object fieldValue = entry.getValue();
        if (fieldValue instanceof DeleteFieldValue) {
            // Add it to the field mask, but don't add anything to updateData.
            context.addToFieldMask(fieldPath);
        } else {
            @Nullable Value parsedValue = convertAndParseFieldData(fieldValue, context.childContext(fieldPath));
            if (parsedValue != null) {
                context.addToFieldMask(fieldPath);
                updateData.set(fieldPath, parsedValue);
            }
        }
    }
    return accumulator.toUpdateData(updateData);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) FieldPath(com.google.firebase.firestore.model.FieldPath) 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) Nullable(androidx.annotation.Nullable)

Example 9 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class UserDataReader method parseMergeData.

/**
 * Parse document data from a {@code set()} call with {@link SetOptions#merge()} set.
 *
 * @param input A map or POJO object representing document data.
 * @param fieldMask A {@code FieldMask} object representing the fields to be merged.
 */
public ParsedSetData parseMergeData(Object input, @Nullable FieldMask fieldMask) {
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.MergeSet);
    ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());
    if (fieldMask != null) {
        // Verify that all elements specified in the field mask are part of the parsed context.
        for (FieldPath field : fieldMask.getMask()) {
            if (!accumulator.contains(field)) {
                throw new IllegalArgumentException("Field '" + field.toString() + "' is specified in your field mask but not in your input data.");
            }
        }
        return accumulator.toMergeData(updateData, fieldMask);
    } else {
        return accumulator.toMergeData(updateData);
    }
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) FieldPath(com.google.firebase.firestore.model.FieldPath)

Example 10 with ObjectValue

use of com.google.firebase.firestore.model.ObjectValue in project firebase-android-sdk by firebase.

the class UserDataReader method convertAndParseDocumentData.

/**
 * Converts a POJO to native types and then parses it into model types. It expects the input to
 * conform to document data (i.e. it must parse into an ObjectValue model type) and will throw an
 * appropriate error otherwise.
 */
private ObjectValue convertAndParseDocumentData(Object input, ParseContext context) {
    String badDocReason = "Invalid data. Data must be a Map<String, Object> or a suitable POJO object, but it was ";
    // to use List instead, which also won't work in a set().
    if (input.getClass().isArray()) {
        throw new IllegalArgumentException(badDocReason + "an array");
    }
    Object converted = CustomClassMapper.convertToPlainJavaTypes(input);
    Value parsedValue = parseData(converted, context);
    if (parsedValue.getValueTypeCase() != Value.ValueTypeCase.MAP_VALUE) {
        throw new IllegalArgumentException(badDocReason + "of type: " + Util.typeName(input));
    }
    return new ObjectValue(parsedValue);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) 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)

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