Search in sources :

Example 1 with DeleteFieldValue

use of com.google.firebase.firestore.FieldValue.DeleteFieldValue 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 2 with DeleteFieldValue

use of com.google.firebase.firestore.FieldValue.DeleteFieldValue in project firebase-android-sdk by firebase.

the class UserDataReader method parseSentinelFieldValue.

/**
 * "Parses" the provided FieldValue, adding any necessary transforms to context.fieldTransforms.
 */
private void parseSentinelFieldValue(com.google.firebase.firestore.FieldValue value, ParseContext context) {
    // Sentinels are only supported with writes, and not within arrays.
    if (!context.isWrite()) {
        throw context.createError(String.format("%s() can only be used with set() and update()", value.getMethodName()));
    }
    if (context.getPath() == null) {
        throw context.createError(String.format("%s() is not currently supported inside arrays", value.getMethodName()));
    }
    if (value instanceof DeleteFieldValue) {
        if (context.getDataSource() == UserData.Source.MergeSet) {
            // No transform to add for a delete, but we need to add it to our
            // fieldMask so it gets deleted.
            context.addToFieldMask(context.getPath());
        } else if (context.getDataSource() == UserData.Source.Update) {
            hardAssert(context.getPath().length() > 0, "FieldValue.delete() at the top level should have already been handled.");
            throw context.createError("FieldValue.delete() can only appear at the top level of your update data");
        } else {
            // We shouldn't encounter delete sentinels for queries or non-merge set() calls.
            throw context.createError("FieldValue.delete() can only be used with update() and " + "set() with SetOptions.merge()");
        }
    } else if (value instanceof ServerTimestampFieldValue) {
        context.addToFieldTransforms(context.getPath(), ServerTimestampOperation.getInstance());
    } else if (value instanceof ArrayUnionFieldValue) {
        List<Value> parsedElements = parseArrayTransformElements(((ArrayUnionFieldValue) value).getElements());
        ArrayTransformOperation arrayUnion = new ArrayTransformOperation.Union(parsedElements);
        context.addToFieldTransforms(context.getPath(), arrayUnion);
    } else if (value instanceof ArrayRemoveFieldValue) {
        List<Value> parsedElements = parseArrayTransformElements(((ArrayRemoveFieldValue) value).getElements());
        ArrayTransformOperation arrayRemove = new ArrayTransformOperation.Remove(parsedElements);
        context.addToFieldTransforms(context.getPath(), arrayRemove);
    } else if (value instanceof com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) {
        com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue numericIncrementFieldValue = (com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) value;
        Value operand = parseQueryValue(numericIncrementFieldValue.getOperand());
        NumericIncrementTransformOperation incrementOperation = new NumericIncrementTransformOperation(operand);
        context.addToFieldTransforms(context.getPath(), incrementOperation);
    } else {
        throw Assert.fail("Unknown FieldValue type: %s", Util.typeName(value));
    }
}
Also used : ArrayTransformOperation(com.google.firebase.firestore.model.mutation.ArrayTransformOperation) NumericIncrementTransformOperation(com.google.firebase.firestore.model.mutation.NumericIncrementTransformOperation) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) 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) ArrayList(java.util.ArrayList) List(java.util.List) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)

Example 3 with DeleteFieldValue

use of com.google.firebase.firestore.FieldValue.DeleteFieldValue 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

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 ObjectValue (com.google.firebase.firestore.model.ObjectValue)3 ArrayValue (com.google.firestore.v1.ArrayValue)3 MapValue (com.google.firestore.v1.MapValue)3 Value (com.google.firestore.v1.Value)3 NullValue (com.google.protobuf.NullValue)3 ParseAccumulator (com.google.firebase.firestore.core.UserData.ParseAccumulator)2 ParseContext (com.google.firebase.firestore.core.UserData.ParseContext)2 FieldPath (com.google.firebase.firestore.model.FieldPath)2 Nullable (androidx.annotation.Nullable)1 ArrayTransformOperation (com.google.firebase.firestore.model.mutation.ArrayTransformOperation)1 NumericIncrementTransformOperation (com.google.firebase.firestore.model.mutation.NumericIncrementTransformOperation)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1