use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.
the class Query method boundFromFields.
/**
* Converts a list of field values to Bound.
*/
private Bound boundFromFields(String methodName, Object[] values, boolean inclusive) {
// Use explicit order by's because it has to match the query the user made
List<OrderBy> explicitOrderBy = query.getExplicitOrderBy();
if (values.length > explicitOrderBy.size()) {
throw new IllegalArgumentException("Too many arguments provided to " + methodName + "(). The number of arguments must be less " + "than or equal to the number of orderBy() clauses.");
}
List<Value> components = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
Object rawValue = values[i];
OrderBy orderBy = explicitOrderBy.get(i);
if (orderBy.getField().equals(com.google.firebase.firestore.model.FieldPath.KEY_PATH)) {
if (!(rawValue instanceof String)) {
throw new IllegalArgumentException("Invalid query. Expected a string for document ID in " + methodName + "(), but got " + rawValue + ".");
}
String documentId = (String) rawValue;
if (!query.isCollectionGroupQuery() && documentId.contains("/")) {
throw new IllegalArgumentException("Invalid query. When querying a collection and ordering by FieldPath.documentId(), " + "the value passed to " + methodName + "() must be a plain document ID, but '" + documentId + "' contains a slash.");
}
ResourcePath path = query.getPath().append(ResourcePath.fromString(documentId));
if (!DocumentKey.isDocumentKey(path)) {
throw new IllegalArgumentException("Invalid query. When querying a collection group and ordering by " + "FieldPath.documentId(), the value passed to " + methodName + "() must result in a valid document path, but '" + path + "' is not because it contains an odd number of segments.");
}
DocumentKey key = DocumentKey.fromPath(path);
components.add(Values.refValue(firestore.getDatabaseId(), key));
} else {
Value wrapped = firestore.getUserDataReader().parseQueryValue(rawValue);
components.add(wrapped);
}
}
return new Bound(components, inclusive);
}
use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.
the class MutationTest method testNumericIncrementBaseValue.
@Test
public void testNumericIncrementBaseValue() {
Map<String, Object> allValues = map("ignore", "foo", "double", 42.0, "long", 42, "string", "foo", "map", map());
allValues.put("nested", new HashMap<>(allValues));
MutableDocument baseDoc = doc("collection/key", 1, allValues);
Map<String, Object> allTransforms = map("double", FieldValue.increment(1), "long", FieldValue.increment(1), "string", FieldValue.increment(1), "map", FieldValue.increment(1), "missing", FieldValue.increment(1));
allTransforms.put("nested", new HashMap<>(allTransforms));
Mutation mutation = patchMutation("collection/key", allTransforms);
ObjectValue baseValue = mutation.extractTransformBaseValue(baseDoc);
Value expected = wrap(map("double", 42.0, "long", 42, "string", 0, "map", 0, "missing", 0, "nested", map("double", 42.0, "long", 42, "string", 0, "map", 0, "missing", 0)));
assertTrue(Values.equals(expected, baseValue.get(FieldPath.EMPTY_PATH)));
}
use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.
the class MutationTest method testAppliesLocalServerTimestampTransformsToDocuments.
@Test
public void testAppliesLocalServerTimestampTransformsToDocuments() {
Map<String, Object> data = map("foo", map("bar", "bar-value"), "baz", "baz-value");
MutableDocument transformedDoc = doc("collection/key", 1, data);
Timestamp timestamp = Timestamp.now();
Mutation transform = patchMutation("collection/key", map("foo.bar", FieldValue.serverTimestamp()));
transform.applyToLocalView(transformedDoc, /* previousMask= */
null, timestamp);
// Server timestamps aren't parsed, so we manually insert it.
ObjectValue expectedData = wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
Value fieldValue = ServerTimestamps.valueOf(timestamp, wrap("bar-value"));
expectedData.set(field("foo.bar"), fieldValue);
MutableDocument expectedDoc = doc("collection/key", 1, expectedData).setHasLocalMutations();
assertEquals(expectedDoc, transformedDoc);
}
use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.
the class RemoteSerializerTest method testEncodesDoubles.
@Test
public void testEncodesDoubles() {
List<Double> tests = asList(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, Long.MAX_VALUE * -1.0 - 1.0, -2.0, -1.1, -1.0, -Double.MIN_VALUE, -Double.MIN_NORMAL, -0.0, 0.0, Double.MIN_NORMAL, Double.MIN_VALUE, 0.1, 1.1, Long.MAX_VALUE * 1.0, Double.MAX_VALUE, Double.POSITIVE_INFINITY);
for (Double test : tests) {
Value value = wrap(test);
Value proto = Value.newBuilder().setDoubleValue(test).build();
assertRoundTrip(value, proto, Value.ValueTypeCase.DOUBLE_VALUE);
}
}
use of com.google.api.expr.v1alpha1.Value in project firebase-android-sdk by firebase.
the class RemoteSerializerTest method testEncodesDates.
@Test
public void testEncodesDates() {
Calendar date1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
date1.set(2016, 0, 2, 10, 20, 50);
date1.set(Calendar.MILLISECOND, 500);
Calendar date2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
date2.set(2016, 5, 17, 10, 50, 15);
date2.set(Calendar.MILLISECOND, 0);
List<Date> tests = asList(date1.getTime(), date2.getTime());
Timestamp ts1 = Timestamp.newBuilder().setNanos(500000000).setSeconds(1451730050).build();
Timestamp ts2 = Timestamp.newBuilder().setNanos(0).setSeconds(1466160615).build();
List<Value> expected = asList(Value.newBuilder().setTimestampValue(ts1).build(), Value.newBuilder().setTimestampValue(ts2).build());
for (int i = 0; i < tests.size(); i++) {
Value value = wrap(tests.get(i));
assertRoundTrip(value, expected.get(i), Value.ValueTypeCase.TIMESTAMP_VALUE);
}
}
Aggregations