Search in sources :

Example 6 with FieldFilter

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

the class Query method parseFieldFilter.

/**
 * Takes a {@link Filter.UnaryFilter} object, parses the value object and returns a new {@link
 * FieldFilter} for the field, operator, and parsed value.
 *
 * @param fieldFilterData The Filter.UnaryFilter object to parse.
 * @return The created {@link FieldFilter}.
 */
private FieldFilter parseFieldFilter(Filter.UnaryFilter fieldFilterData) {
    FieldPath fieldPath = fieldFilterData.getField();
    Operator op = fieldFilterData.getOperator();
    Object value = fieldFilterData.getValue();
    checkNotNull(fieldPath, "Provided field path must not be null.");
    checkNotNull(op, "Provided op must not be null.");
    Value fieldValue;
    com.google.firebase.firestore.model.FieldPath internalPath = fieldPath.getInternalPath();
    if (internalPath.isKeyField()) {
        if (op == Operator.ARRAY_CONTAINS || op == Operator.ARRAY_CONTAINS_ANY) {
            throw new IllegalArgumentException("Invalid query. You can't perform '" + op.toString() + "' queries on FieldPath.documentId().");
        } else if (op == Operator.IN || op == Operator.NOT_IN) {
            validateDisjunctiveFilterElements(value, op);
            ArrayValue.Builder referenceList = ArrayValue.newBuilder();
            for (Object arrayValue : (List) value) {
                referenceList.addValues(parseDocumentIdValue(arrayValue));
            }
            fieldValue = Value.newBuilder().setArrayValue(referenceList).build();
        } else {
            fieldValue = parseDocumentIdValue(value);
        }
    } else {
        if (op == Operator.IN || op == Operator.NOT_IN || op == Operator.ARRAY_CONTAINS_ANY) {
            validateDisjunctiveFilterElements(value, op);
        }
        fieldValue = firestore.getUserDataReader().parseQueryValue(value, op == Operator.IN || op == Operator.NOT_IN);
    }
    FieldFilter filter = FieldFilter.create(fieldPath.getInternalPath(), op, fieldValue);
    return filter;
}
Also used : Operator(com.google.firebase.firestore.core.FieldFilter.Operator) FieldFilter(com.google.firebase.firestore.core.FieldFilter) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue)

Example 7 with FieldFilter

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

the class RemoteSerializerTest method testArrayContainsAnySerialization.

@Test
public void testArrayContainsAnySerialization() {
    FieldFilter inputFilter = filter("field", "array-contains-any", asList(42));
    StructuredQuery.Filter apiFilter = serializer.encodeUnaryOrFieldFilter(inputFilter);
    ArrayValue.Builder arrayContainsAnyFilterValue = ArrayValue.newBuilder().addValues(Value.newBuilder().setIntegerValue(42));
    StructuredQuery.Filter expectedFilter = Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("field")).setOp(Operator.ARRAY_CONTAINS_ANY).setValue(Value.newBuilder().setArrayValue(arrayContainsAnyFilterValue)).build()).build();
    assertEquals(expectedFilter, apiFilter);
    FieldFilter roundTripped = serializer.decodeFieldFilter(apiFilter.getFieldFilter());
    assertEquals(roundTripped, inputFilter);
    assertTrue(roundTripped instanceof ArrayContainsAnyFilter);
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) FieldFilter(com.google.firebase.firestore.core.FieldFilter) KeyFieldFilter(com.google.firebase.firestore.core.KeyFieldFilter) Filter(com.google.firestore.v1.StructuredQuery.Filter) ArrayContainsAnyFilter(com.google.firebase.firestore.core.ArrayContainsAnyFilter) ArrayValue(com.google.firestore.v1.ArrayValue) Test(org.junit.Test)

Example 8 with FieldFilter

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

the class RemoteSerializerTest method testNotInSerialization.

@Test
public void testNotInSerialization() {
    FieldFilter inputFilter = filter("field", "not-in", asList(42));
    StructuredQuery.Filter apiFilter = serializer.encodeUnaryOrFieldFilter(inputFilter);
    ArrayValue.Builder notInFilterValue = ArrayValue.newBuilder().addValues(Value.newBuilder().setIntegerValue(42));
    StructuredQuery.Filter expectedFilter = Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("field")).setOp(Operator.NOT_IN).setValue(Value.newBuilder().setArrayValue(notInFilterValue)).build()).build();
    assertEquals(expectedFilter, apiFilter);
    FieldFilter roundTripped = serializer.decodeFieldFilter(apiFilter.getFieldFilter());
    assertEquals(roundTripped, inputFilter);
    assertTrue(roundTripped instanceof NotInFilter);
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) FieldFilter(com.google.firebase.firestore.core.FieldFilter) KeyFieldFilter(com.google.firebase.firestore.core.KeyFieldFilter) NotInFilter(com.google.firebase.firestore.core.NotInFilter) Filter(com.google.firestore.v1.StructuredQuery.Filter) ArrayValue(com.google.firestore.v1.ArrayValue) Test(org.junit.Test)

Example 9 with FieldFilter

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

the class RemoteSerializerTest method testInSerialization.

@Test
public void testInSerialization() {
    FieldFilter inputFilter = filter("field", "in", asList(42));
    StructuredQuery.Filter apiFilter = serializer.encodeUnaryOrFieldFilter(inputFilter);
    ArrayValue.Builder inFilterValue = ArrayValue.newBuilder().addValues(Value.newBuilder().setIntegerValue(42));
    StructuredQuery.Filter expectedFilter = Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("field")).setOp(Operator.IN).setValue(Value.newBuilder().setArrayValue(inFilterValue)).build()).build();
    assertEquals(expectedFilter, apiFilter);
    FieldFilter roundTripped = serializer.decodeFieldFilter(apiFilter.getFieldFilter());
    assertEquals(roundTripped, inputFilter);
    assertTrue(roundTripped instanceof InFilter);
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) FieldFilter(com.google.firebase.firestore.core.FieldFilter) KeyFieldFilter(com.google.firebase.firestore.core.KeyFieldFilter) Filter(com.google.firestore.v1.StructuredQuery.Filter) ArrayValue(com.google.firestore.v1.ArrayValue) NotInFilter(com.google.firebase.firestore.core.NotInFilter) InFilter(com.google.firebase.firestore.core.InFilter) Test(org.junit.Test)

Example 10 with FieldFilter

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

the class RemoteSerializerTest method testNotInWithNullSerialization.

@Test
public void testNotInWithNullSerialization() {
    List<Object> nullArray = new ArrayList<>();
    nullArray.add(null);
    FieldFilter inputFilter = filter("field", "not-in", nullArray);
    StructuredQuery.Filter apiFilter = serializer.encodeUnaryOrFieldFilter(inputFilter);
    ArrayValue.Builder notInFilterValue = ArrayValue.newBuilder().addValues(Value.newBuilder().setNullValue(NullValue.NULL_VALUE));
    StructuredQuery.Filter expectedFilter = Filter.newBuilder().setFieldFilter(StructuredQuery.FieldFilter.newBuilder().setField(FieldReference.newBuilder().setFieldPath("field")).setOp(Operator.NOT_IN).setValue(Value.newBuilder().setArrayValue(notInFilterValue)).build()).build();
    assertEquals(expectedFilter, apiFilter);
    FieldFilter roundTripped = serializer.decodeFieldFilter(apiFilter.getFieldFilter());
    assertEquals(roundTripped, inputFilter);
    assertTrue(roundTripped instanceof NotInFilter);
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) FieldFilter(com.google.firebase.firestore.core.FieldFilter) KeyFieldFilter(com.google.firebase.firestore.core.KeyFieldFilter) ArrayList(java.util.ArrayList) NotInFilter(com.google.firebase.firestore.core.NotInFilter) Filter(com.google.firestore.v1.StructuredQuery.Filter) ArrayValue(com.google.firestore.v1.ArrayValue) Test(org.junit.Test)

Aggregations

FieldFilter (com.google.firebase.firestore.core.FieldFilter)10 KeyFieldFilter (com.google.firebase.firestore.core.KeyFieldFilter)6 StructuredQuery (com.google.firestore.v1.StructuredQuery)6 Filter (com.google.firestore.v1.StructuredQuery.Filter)6 Test (org.junit.Test)6 ArrayValue (com.google.firestore.v1.ArrayValue)5 NotInFilter (com.google.firebase.firestore.core.NotInFilter)3 FieldPath (com.google.firebase.firestore.model.FieldPath)2 VisibleForTesting (androidx.annotation.VisibleForTesting)1 ArrayContainsAnyFilter (com.google.firebase.firestore.core.ArrayContainsAnyFilter)1 Operator (com.google.firebase.firestore.core.FieldFilter.Operator)1 InFilter (com.google.firebase.firestore.core.InFilter)1 Value (com.google.firestore.v1.Value)1 ArrayList (java.util.ArrayList)1