Search in sources :

Example 6 with ParseAccumulator

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

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