Search in sources :

Example 1 with ParseAccumulator

use of com.google.firebase.firestore.core.UserData.ParseAccumulator 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 2 with ParseAccumulator

use of com.google.firebase.firestore.core.UserData.ParseAccumulator 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 3 with ParseAccumulator

use of com.google.firebase.firestore.core.UserData.ParseAccumulator 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 4 with ParseAccumulator

use of com.google.firebase.firestore.core.UserData.ParseAccumulator in project firebase-android-sdk by firebase.

the class UserDataReader method parseQueryValue.

/**
 * Parse a "query value" (e.g. value in a where filter or a value in a cursor bound).
 *
 * @param allowArrays Whether the query value is an array that may directly contain additional
 *     arrays (e.g. the operand of a `whereIn` query).
 */
public Value parseQueryValue(Object input, boolean allowArrays) {
    ParseAccumulator accumulator = new ParseAccumulator(allowArrays ? UserData.Source.ArrayArgument : UserData.Source.Argument);
    @Nullable Value parsed = convertAndParseFieldData(input, accumulator.rootContext());
    hardAssert(parsed != null, "Parsed data should not be null.");
    hardAssert(accumulator.getFieldTransforms().isEmpty(), "Field transforms should have been disallowed.");
    return parsed;
}
Also used : ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) 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) Nullable(androidx.annotation.Nullable)

Example 5 with ParseAccumulator

use of com.google.firebase.firestore.core.UserData.ParseAccumulator in project firebase-android-sdk by firebase.

the class UserDataReader method parseArrayTransformElements.

private List<Value> parseArrayTransformElements(List<Object> elements) {
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Argument);
    List<Value> result = new ArrayList<>(elements.size());
    for (int i = 0; i < elements.size(); i++) {
        Object element = elements.get(i);
        // Although array transforms are used with writes, the actual elements
        // being unioned or removed are not considered writes since they cannot
        // contain any FieldValue sentinels, etc.
        ParseContext context = accumulator.rootContext();
        result.add(convertAndParseFieldData(element, context.childContext(i)));
    }
    return result;
}
Also used : ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) 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) ArrayList(java.util.ArrayList) ParseContext(com.google.firebase.firestore.core.UserData.ParseContext)

Aggregations

ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)6 ObjectValue (com.google.firebase.firestore.model.ObjectValue)6 ArrayRemoveFieldValue (com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)4 ArrayUnionFieldValue (com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue)4 DeleteFieldValue (com.google.firebase.firestore.FieldValue.DeleteFieldValue)4 ServerTimestampFieldValue (com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue)4 ArrayValue (com.google.firestore.v1.ArrayValue)4 MapValue (com.google.firestore.v1.MapValue)4 Value (com.google.firestore.v1.Value)4 NullValue (com.google.protobuf.NullValue)4 ParseContext (com.google.firebase.firestore.core.UserData.ParseContext)3 FieldPath (com.google.firebase.firestore.model.FieldPath)3 Nullable (androidx.annotation.Nullable)2 ArrayList (java.util.ArrayList)1