Search in sources :

Example 81 with Value

use of com.spotify.ffwd.v1.Value in project firebase-android-sdk by firebase.

the class UserDataReader method parseList.

private <T> Value parseList(List<T> list, ParseContext context) {
    ArrayValue.Builder arrayBuilder = ArrayValue.newBuilder();
    int entryIndex = 0;
    for (T entry : list) {
        @Nullable Value parsedEntry = parseData(entry, context.childContext(entryIndex));
        if (parsedEntry == null) {
            // Just include nulls in the array for fields being replaced with a sentinel.
            parsedEntry = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
        }
        arrayBuilder.addValues(parsedEntry);
        entryIndex++;
    }
    return Value.newBuilder().setArrayValue(arrayBuilder).build();
}
Also used : 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) ArrayValue(com.google.firestore.v1.ArrayValue) Nullable(androidx.annotation.Nullable)

Example 82 with Value

use of com.spotify.ffwd.v1.Value in project firebase-android-sdk by firebase.

the class UserDataReader method parseUpdateData.

/**
 * Parse update data from an {@code update()} call.
 */
public ParsedUpdateData parseUpdateData(Map<String, Object> data) {
    checkNotNull(data, "Provided update data must not be null.");
    ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Update);
    ParseContext context = accumulator.rootContext();
    ObjectValue updateData = new ObjectValue();
    for (Entry<String, Object> entry : data.entrySet()) {
        FieldPath fieldPath = com.google.firebase.firestore.FieldPath.fromDotSeparatedPath(entry.getKey()).getInternalPath();
        Object fieldValue = entry.getValue();
        if (fieldValue instanceof DeleteFieldValue) {
            // Add it to the field mask, but don't add anything to updateData.
            context.addToFieldMask(fieldPath);
        } else {
            @Nullable Value parsedValue = convertAndParseFieldData(fieldValue, context.childContext(fieldPath));
            if (parsedValue != null) {
                context.addToFieldMask(fieldPath);
                updateData.set(fieldPath, parsedValue);
            }
        }
    }
    return accumulator.toUpdateData(updateData);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) ParseAccumulator(com.google.firebase.firestore.core.UserData.ParseAccumulator) FieldPath(com.google.firebase.firestore.model.FieldPath) ParseContext(com.google.firebase.firestore.core.UserData.ParseContext) 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) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) Nullable(androidx.annotation.Nullable)

Example 83 with Value

use of com.spotify.ffwd.v1.Value in project firebase-android-sdk by firebase.

the class UserDataReader method convertAndParseDocumentData.

/**
 * Converts a POJO to native types and then parses it into model types. It expects the input to
 * conform to document data (i.e. it must parse into an ObjectValue model type) and will throw an
 * appropriate error otherwise.
 */
private ObjectValue convertAndParseDocumentData(Object input, ParseContext context) {
    String badDocReason = "Invalid data. Data must be a Map<String, Object> or a suitable POJO object, but it was ";
    // to use List instead, which also won't work in a set().
    if (input.getClass().isArray()) {
        throw new IllegalArgumentException(badDocReason + "an array");
    }
    Object converted = CustomClassMapper.convertToPlainJavaTypes(input);
    Value parsedValue = parseData(converted, context);
    if (parsedValue.getValueTypeCase() != Value.ValueTypeCase.MAP_VALUE) {
        throw new IllegalArgumentException(badDocReason + "of type: " + Util.typeName(input));
    }
    return new ObjectValue(parsedValue);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) 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)

Example 84 with Value

use of com.spotify.ffwd.v1.Value in project firebase-android-sdk by firebase.

the class UserDataReader method parseSentinelFieldValue.

/**
 * "Parses" the provided FieldValue, adding any necessary transforms to context.fieldTransforms.
 */
private void parseSentinelFieldValue(com.google.firebase.firestore.FieldValue value, ParseContext context) {
    // Sentinels are only supported with writes, and not within arrays.
    if (!context.isWrite()) {
        throw context.createError(String.format("%s() can only be used with set() and update()", value.getMethodName()));
    }
    if (context.getPath() == null) {
        throw context.createError(String.format("%s() is not currently supported inside arrays", value.getMethodName()));
    }
    if (value instanceof DeleteFieldValue) {
        if (context.getDataSource() == UserData.Source.MergeSet) {
            // No transform to add for a delete, but we need to add it to our
            // fieldMask so it gets deleted.
            context.addToFieldMask(context.getPath());
        } else if (context.getDataSource() == UserData.Source.Update) {
            hardAssert(context.getPath().length() > 0, "FieldValue.delete() at the top level should have already been handled.");
            throw context.createError("FieldValue.delete() can only appear at the top level of your update data");
        } else {
            // We shouldn't encounter delete sentinels for queries or non-merge set() calls.
            throw context.createError("FieldValue.delete() can only be used with update() and " + "set() with SetOptions.merge()");
        }
    } else if (value instanceof ServerTimestampFieldValue) {
        context.addToFieldTransforms(context.getPath(), ServerTimestampOperation.getInstance());
    } else if (value instanceof ArrayUnionFieldValue) {
        List<Value> parsedElements = parseArrayTransformElements(((ArrayUnionFieldValue) value).getElements());
        ArrayTransformOperation arrayUnion = new ArrayTransformOperation.Union(parsedElements);
        context.addToFieldTransforms(context.getPath(), arrayUnion);
    } else if (value instanceof ArrayRemoveFieldValue) {
        List<Value> parsedElements = parseArrayTransformElements(((ArrayRemoveFieldValue) value).getElements());
        ArrayTransformOperation arrayRemove = new ArrayTransformOperation.Remove(parsedElements);
        context.addToFieldTransforms(context.getPath(), arrayRemove);
    } else if (value instanceof com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) {
        com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue numericIncrementFieldValue = (com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) value;
        Value operand = parseQueryValue(numericIncrementFieldValue.getOperand());
        NumericIncrementTransformOperation incrementOperation = new NumericIncrementTransformOperation(operand);
        context.addToFieldTransforms(context.getPath(), incrementOperation);
    } else {
        throw Assert.fail("Unknown FieldValue type: %s", Util.typeName(value));
    }
}
Also used : ArrayTransformOperation(com.google.firebase.firestore.model.mutation.ArrayTransformOperation) NumericIncrementTransformOperation(com.google.firebase.firestore.model.mutation.NumericIncrementTransformOperation) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) 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) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) ArrayList(java.util.ArrayList) List(java.util.List) DeleteFieldValue(com.google.firebase.firestore.FieldValue.DeleteFieldValue) ServerTimestampFieldValue(com.google.firebase.firestore.FieldValue.ServerTimestampFieldValue) ArrayUnionFieldValue(com.google.firebase.firestore.FieldValue.ArrayUnionFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue) ArrayRemoveFieldValue(com.google.firebase.firestore.FieldValue.ArrayRemoveFieldValue)

Example 85 with Value

use of com.spotify.ffwd.v1.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)

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)24 Value (com.google.datastore.v1.Value)20 Map (java.util.Map)19 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