Search in sources :

Example 1 with FieldTransform

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

the class TestUtil method patchMutationHelper.

private static PatchMutation patchMutationHelper(String path, Map<String, Object> values, Precondition precondition, @Nullable List<FieldPath> updateMask) {
    // Replace '<DELETE>' from JSON
    for (Entry<String, Object> entry : values.entrySet()) {
        if (entry.getValue().equals(DELETE_SENTINEL)) {
            values.put(entry.getKey(), FieldValue.delete());
        }
    }
    UserDataReader dataReader = new UserDataReader(DatabaseId.forProject("project"));
    ParsedUpdateData parsed = dataReader.parseUpdateData(values);
    // `mergeMutation()` provides an update mask for the merged fields, whereas `patchMutation()`
    // requires the update mask to be parsed from the values.
    Collection<FieldPath> mask = updateMask != null ? updateMask : parsed.getFieldMask().getMask();
    // We sort the fieldMaskPaths to make the order deterministic in tests. (Otherwise, when we
    // flatten a Set to a proto repeated field, we'll end up comparing in iterator order and
    // possibly consider {foo,bar} != {bar,foo}.)
    SortedSet<FieldPath> fieldMaskPaths = new TreeSet<>(mask);
    // The order of the transforms doesn't matter, but we sort them so tests can assume a particular
    // order.
    ArrayList<FieldTransform> fieldTransforms = new ArrayList<>(parsed.getFieldTransforms());
    Collections.sort(fieldTransforms, (ft1, ft2) -> ft1.getFieldPath().compareTo(ft2.getFieldPath()));
    return new PatchMutation(key(path), parsed.getData(), FieldMask.fromSet(fieldMaskPaths), precondition, fieldTransforms);
}
Also used : FieldPath(com.google.firebase.firestore.model.FieldPath) FieldTransform(com.google.firebase.firestore.model.mutation.FieldTransform) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ParsedUpdateData(com.google.firebase.firestore.core.UserData.ParsedUpdateData) UserDataReader(com.google.firebase.firestore.UserDataReader) TreeSet(java.util.TreeSet) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation)

Example 2 with FieldTransform

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

the class RemoteSerializer method decodeMutation.

public Mutation decodeMutation(com.google.firestore.v1.Write mutation) {
    Precondition precondition = mutation.hasCurrentDocument() ? decodePrecondition(mutation.getCurrentDocument()) : Precondition.NONE;
    List<FieldTransform> fieldTransforms = new ArrayList<>();
    for (DocumentTransform.FieldTransform fieldTransform : mutation.getUpdateTransformsList()) {
        fieldTransforms.add(decodeFieldTransform(fieldTransform));
    }
    switch(mutation.getOperationCase()) {
        case UPDATE:
            if (mutation.hasUpdateMask()) {
                return new PatchMutation(decodeKey(mutation.getUpdate().getName()), ObjectValue.fromMap(mutation.getUpdate().getFieldsMap()), decodeDocumentMask(mutation.getUpdateMask()), precondition, fieldTransforms);
            } else {
                return new SetMutation(decodeKey(mutation.getUpdate().getName()), ObjectValue.fromMap(mutation.getUpdate().getFieldsMap()), precondition, fieldTransforms);
            }
        case DELETE:
            return new DeleteMutation(decodeKey(mutation.getDelete()), precondition);
        case VERIFY:
            return new VerifyMutation(decodeKey(mutation.getVerify()), precondition);
        default:
            throw fail("Unknown mutation operation: %d", mutation.getOperationCase());
    }
}
Also used : DocumentTransform(com.google.firestore.v1.DocumentTransform) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation) Precondition(com.google.firebase.firestore.model.mutation.Precondition) FieldTransform(com.google.firebase.firestore.model.mutation.FieldTransform) ArrayList(java.util.ArrayList) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) VerifyMutation(com.google.firebase.firestore.model.mutation.VerifyMutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation)

Example 3 with FieldTransform

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

the class TestUtil method setMutation.

public static SetMutation setMutation(String path, Map<String, Object> values) {
    UserDataReader dataReader = new UserDataReader(DatabaseId.forProject("project"));
    ParsedSetData parsed = dataReader.parseSetData(values);
    // The order of the transforms doesn't matter, but we sort them so tests can assume a particular
    // order.
    ArrayList<FieldTransform> fieldTransforms = new ArrayList<>(parsed.getFieldTransforms());
    Collections.sort(fieldTransforms, (ft1, ft2) -> ft1.getFieldPath().compareTo(ft2.getFieldPath()));
    return new SetMutation(key(path), parsed.getData(), Precondition.NONE, fieldTransforms);
}
Also used : UserDataReader(com.google.firebase.firestore.UserDataReader) ParsedSetData(com.google.firebase.firestore.core.UserData.ParsedSetData) FieldTransform(com.google.firebase.firestore.model.mutation.FieldTransform) ArrayList(java.util.ArrayList) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation)

Aggregations

FieldTransform (com.google.firebase.firestore.model.mutation.FieldTransform)3 ArrayList (java.util.ArrayList)3 UserDataReader (com.google.firebase.firestore.UserDataReader)2 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)2 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)2 ParsedSetData (com.google.firebase.firestore.core.UserData.ParsedSetData)1 ParsedUpdateData (com.google.firebase.firestore.core.UserData.ParsedUpdateData)1 FieldPath (com.google.firebase.firestore.model.FieldPath)1 DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)1 Precondition (com.google.firebase.firestore.model.mutation.Precondition)1 VerifyMutation (com.google.firebase.firestore.model.mutation.VerifyMutation)1 DocumentTransform (com.google.firestore.v1.DocumentTransform)1 ByteString (com.google.protobuf.ByteString)1 TreeSet (java.util.TreeSet)1