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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations