use of com.spotify.ffwd.v1.Value in project firebase-android-sdk by firebase.
the class BundleSerializer method decodePosition.
private List<Value> decodePosition(JSONObject bound) throws JSONException {
List<Value> cursor = new ArrayList<>();
JSONArray values = bound.optJSONArray("values");
if (values != null) {
for (int i = 0; i < values.length(); ++i) {
cursor.add(decodeValue(values.getJSONObject(i)));
}
}
return cursor;
}
use of com.spotify.ffwd.v1.Value in project firebase-android-sdk by firebase.
the class Query method boundFromDocumentSnapshot.
/**
* Create a Bound from a query given the document.
*
* <p>Note that the Bound will always include the key of the document and so only the provided
* document will compare equal to the returned position.
*
* <p>Will throw if the document does not contain all fields of the order by of the query or if
* any of the fields in the order by are an uncommitted server timestamp.
*/
private Bound boundFromDocumentSnapshot(String methodName, DocumentSnapshot snapshot, boolean inclusive) {
checkNotNull(snapshot, "Provided snapshot must not be null.");
if (!snapshot.exists()) {
throw new IllegalArgumentException("Can't use a DocumentSnapshot for a document that doesn't exist for " + methodName + "().");
}
Document document = snapshot.getDocument();
List<Value> components = new ArrayList<>();
// orders), multiple documents could match the position, yielding duplicate results.
for (OrderBy orderBy : query.getOrderBy()) {
if (orderBy.getField().equals(com.google.firebase.firestore.model.FieldPath.KEY_PATH)) {
components.add(Values.refValue(firestore.getDatabaseId(), document.getKey()));
} else {
Value value = document.getField(orderBy.getField());
if (ServerTimestamps.isServerTimestamp(value)) {
throw new IllegalArgumentException("Invalid query. You are trying to start or end a query using a document for which " + "the field '" + orderBy.getField() + "' is an uncommitted server timestamp. (Since the value of this field is " + "unknown, you cannot start/end a query with it.)");
} else if (value != null) {
components.add(value);
} else {
throw new IllegalArgumentException("Invalid query. You are trying to start or end a query using a document for which " + "the field '" + orderBy.getField() + "' (used as the orderBy) does not exist.");
}
}
}
return new Bound(components, inclusive);
}
use of com.spotify.ffwd.v1.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.spotify.ffwd.v1.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.spotify.ffwd.v1.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);
}
Aggregations