Search in sources :

Example 86 with Value

use of com.google.firestore.v1beta1.Value in project firebase-android-sdk by firebase.

the class UserDataReader method parseQueryValue.

/**
 * Parse a "query value" (e.g. value in a where filter or a value in a cursor bound).
 *
 * @param allowArrays Whether the query value is an array that may directly contain additional
 *     arrays (e.g. the operand of a `whereIn` query).
 */
public Value parseQueryValue(Object input, boolean allowArrays) {
    ParseAccumulator accumulator = new ParseAccumulator(allowArrays ? UserData.Source.ArrayArgument : UserData.Source.Argument);
    @Nullable Value parsed = convertAndParseFieldData(input, accumulator.rootContext());
    hardAssert(parsed != null, "Parsed data should not be null.");
    hardAssert(accumulator.getFieldTransforms().isEmpty(), "Field transforms should have been disallowed.");
    return parsed;
}
Also used : ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) NullValue(com.google.protobuf.NullValue) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) MapValue(com.google.firestore.v1.MapValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue) ArrayValue(com.google.firestore.v1.ArrayValue) Nullable(androidx.annotation.Nullable)

Example 87 with Value

use of com.google.firestore.v1beta1.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;
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) NullValue(com.google.protobuf.NullValue) MapValue(com.google.firestore.v1.MapValue) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Example 88 with Value

use of com.google.firestore.v1beta1.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);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) Bound(com.google.firebase.firestore.core.Bound) Document(com.google.firebase.firestore.model.Document)

Example 89 with Value

use of com.google.firestore.v1beta1.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);
}
Also used : OrderBy(com.google.firebase.firestore.core.OrderBy) ResourcePath(com.google.firebase.firestore.model.ResourcePath) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Bound(com.google.firebase.firestore.core.Bound)

Example 90 with Value

use of com.google.firestore.v1beta1.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)));
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)126 Value (com.google.firestore.v1.Value)108 ArrayValue (com.google.firestore.v1.ArrayValue)73 LinkedHashSet (java.util.LinkedHashSet)71 ObjectValue (com.google.firebase.firestore.model.ObjectValue)53 NullValue (com.google.protobuf.NullValue)50 MapValue (com.google.firestore.v1.MapValue)47 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)25 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)20 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)17 List (java.util.List)17 Record (org.apache.avro.generic.GenericData.Record)16 SchemaAndRecord (org.apache.beam.sdk.io.gcp.bigquery.SchemaAndRecord)16 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)16 Set (java.util.Set)14 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)13 Nullable (androidx.annotation.Nullable)10 Value (com.google.privacy.dlp.v2.Value)9