Skip to content

Drop Array, Reference, Number, Integer and DoubleValue #1206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static FieldValue wrap(Object value) {
UserDataReader dataReader = new UserDataReader(databaseId);
// HACK: We use parseQueryValue() since it accepts scalars as well as arrays / objects, and
// our tests currently use wrap() pretty generically so we don't know the intent.
return dataReader.parseQueryValue(value);
return FieldValue.valueOf(dataReader.parseQueryValue(value));
}

public static ObjectValue wrapObject(Map<String, Object> value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.value.ArrayValue;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ReferenceValue;
import com.google.firebase.firestore.model.value.ProtoValues;
import com.google.firebase.firestore.model.value.ServerTimestampValue;
import com.google.firebase.firestore.util.Executors;
import com.google.firebase.firestore.util.Util;
import com.google.firestore.v1.ArrayValue;
import com.google.firestore.v1.Value;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -327,7 +326,7 @@ public Query whereIn(@NonNull FieldPath fieldPath, @NonNull List<? extends Objec
private Query whereHelper(@NonNull FieldPath fieldPath, Operator op, Object value) {
checkNotNull(fieldPath, "Provided field path must not be null.");
checkNotNull(op, "Provided op must not be null.");
FieldValue fieldValue;
Value fieldValue;
com.google.firebase.firestore.model.FieldPath internalPath = fieldPath.getInternalPath();
if (internalPath.isKeyField()) {
if (op == Operator.ARRAY_CONTAINS || op == Operator.ARRAY_CONTAINS_ANY) {
Expand All @@ -337,11 +336,11 @@ private Query whereHelper(@NonNull FieldPath fieldPath, Operator op, Object valu
+ "' queries on FieldPath.documentId().");
} else if (op == Operator.IN) {
validateDisjunctiveFilterElements(value, op);
List<Value> referenceList = new ArrayList<>();
ArrayValue.Builder referenceList = ArrayValue.newBuilder();
for (Object arrayValue : (List) value) {
referenceList.add(parseDocumentIdValue(arrayValue).getProto());
referenceList.addValues(parseDocumentIdValue(arrayValue));
}
fieldValue = ArrayValue.fromList(referenceList);
fieldValue = Value.newBuilder().setArrayValue(referenceList).build();
} else {
fieldValue = parseDocumentIdValue(value);
}
Expand All @@ -368,7 +367,7 @@ private void validateOrderByField(com.google.firebase.firestore.model.FieldPath
* Parses the given documentIdValue into a ReferenceValue, throwing appropriate errors if the
* value is anything other than a DocumentReference or String, or if the string is malformed.
*/
private ReferenceValue parseDocumentIdValue(Object documentIdValue) {
private Value parseDocumentIdValue(Object documentIdValue) {
if (documentIdValue instanceof String) {
String documentId = (String) documentIdValue;
if (documentId.isEmpty()) {
Expand All @@ -393,11 +392,10 @@ private ReferenceValue parseDocumentIdValue(Object documentIdValue) {
+ path.length()
+ ").");
}
return ReferenceValue.valueOf(
this.getFirestore().getDatabaseId(), DocumentKey.fromPath(path));
return ProtoValues.refValue(this.getFirestore().getDatabaseId(), DocumentKey.fromPath(path));
} else if (documentIdValue instanceof DocumentReference) {
DocumentReference ref = (DocumentReference) documentIdValue;
return ReferenceValue.valueOf(this.getFirestore().getDatabaseId(), ref.getKey());
return ProtoValues.refValue(this.getFirestore().getDatabaseId(), ref.getKey());
} else {
throw new IllegalArgumentException(
"Invalid query. When querying with FieldPath.documentId() you must provide a valid "
Expand Down Expand Up @@ -741,7 +739,7 @@ private Bound boundFromDocumentSnapshot(
+ "().");
}
Document document = snapshot.getDocument();
List<FieldValue> components = new ArrayList<>();
List<Value> components = new ArrayList<>();

// Because people expect to continue/end a query at the exact document provided, we need to
// use the implicit sort order rather than the explicit sort order, because it's guaranteed to
Expand All @@ -750,10 +748,10 @@ private Bound boundFromDocumentSnapshot(
// 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(ReferenceValue.valueOf(firestore.getDatabaseId(), document.getKey()));
components.add(ProtoValues.refValue(firestore.getDatabaseId(), document.getKey()));
} else {
FieldValue value = document.getField(orderBy.getField());
if (value instanceof ServerTimestampValue) {
Value value = document.getFieldProto(orderBy.getField());
if (ServerTimestampValue.isServerTimestamp(value)) {
throw new IllegalArgumentException(
"Invalid query. You are trying to start or end a query using a document for which "
+ "the field '"
Expand Down Expand Up @@ -786,7 +784,7 @@ private Bound boundFromFields(String methodName, Object[] values, boolean before
+ "than or equal to the number of orderBy() clauses.");
}

List<FieldValue> components = new ArrayList<>();
List<Value> components = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
Object rawValue = values[i];
OrderBy orderBy = explicitOrderBy.get(i);
Expand Down Expand Up @@ -820,9 +818,9 @@ private Bound boundFromFields(String methodName, Object[] values, boolean before
+ "' is not because it contains an odd number of segments.");
}
DocumentKey key = DocumentKey.fromPath(path);
components.add(ReferenceValue.valueOf(firestore.getDatabaseId(), key));
components.add(ProtoValues.refValue(firestore.getDatabaseId(), key));
} else {
FieldValue wrapped = firestore.getUserDataReader().parseQueryValue(rawValue);
Value wrapped = firestore.getUserDataReader().parseQueryValue(rawValue);
components.add(wrapped);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import com.google.firebase.firestore.model.mutation.FieldMask;
import com.google.firebase.firestore.model.mutation.NumericIncrementTransformOperation;
import com.google.firebase.firestore.model.mutation.ServerTimestampOperation;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.NumberValue;
import com.google.firebase.firestore.model.value.ObjectValue;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.CustomClassMapper;
Expand Down Expand Up @@ -186,7 +184,7 @@ public ParsedUpdateData parseUpdateData(List<Object> fieldsAndValues) {
}

/** Parse a "query value" (e.g. value in a where filter or a value in a cursor bound). */
public FieldValue parseQueryValue(Object input) {
public Value parseQueryValue(Object input) {
return parseQueryValue(input, false);
}

Expand All @@ -196,7 +194,7 @@ public FieldValue parseQueryValue(Object input) {
* @param allowArrays Whether the query value is an array that may directly contain additional
* arrays (e.g. the operand of a `whereIn` query).
*/
public FieldValue parseQueryValue(Object input, boolean allowArrays) {
public Value parseQueryValue(Object input, boolean allowArrays) {
ParseAccumulator accumulator =
new ParseAccumulator(
allowArrays ? UserData.Source.ArrayArgument : UserData.Source.Argument);
Expand All @@ -206,7 +204,7 @@ public FieldValue parseQueryValue(Object input, boolean allowArrays) {
hardAssert(
accumulator.getFieldTransforms().isEmpty(),
"Field transforms should have been disallowed.");
return FieldValue.valueOf(parsed);
return parsed;
}

/** Converts a POJO to native types and then parses it into model types. */
Expand All @@ -231,12 +229,11 @@ private ObjectValue convertAndParseDocumentData(Object input, ParseContext conte
}

Object converted = CustomClassMapper.convertToPlainJavaTypes(input);
FieldValue value = FieldValue.valueOf(parseData(converted, context));

if (!(value instanceof ObjectValue)) {
Value parsedValue = parseData(converted, context);
if (parsedValue.getValueTypeCase() != Value.ValueTypeCase.MAP_VALUE) {
throw new IllegalArgumentException(badDocReason + "of type: " + Util.typeName(input));
}
return (ObjectValue) value;
return new ObjectValue(parsedValue);
}

/**
Expand Down Expand Up @@ -373,7 +370,7 @@ private void parseSentinelFieldValue(
com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue
numericIncrementFieldValue =
(com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) value;
NumberValue operand = (NumberValue) parseQueryValue(numericIncrementFieldValue.getOperand());
Value operand = parseQueryValue(numericIncrementFieldValue.getOperand());
NumericIncrementTransformOperation incrementOperation =
new NumericIncrementTransformOperation(operand);
context.addToFieldTransforms(context.getPath(), incrementOperation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
package com.google.firebase.firestore;

import static com.google.firebase.firestore.util.Assert.fail;
import static com.google.firebase.firestore.util.Assert.hardAssert;

import androidx.annotation.RestrictTo;
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.model.DatabaseId;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ReferenceValue;
import com.google.firebase.firestore.model.value.ServerTimestampValue;
import com.google.firebase.firestore.util.Logger;
import com.google.firestore.v1.ArrayValue;
Expand Down Expand Up @@ -125,15 +122,9 @@ private List<Object> convertArray(ArrayValue arrayValue) {
return result;
}

protected Object convertReference(Value value) {
FieldValue fieldValue = FieldValue.valueOf(value);
hardAssert(
fieldValue instanceof ReferenceValue,
"FieldValue conversion returned invalid type for %s",
value);

DatabaseId refDatabase = ((ReferenceValue) fieldValue).getDatabaseId();
DocumentKey key = ((ReferenceValue) fieldValue).getKey();
private Object convertReference(Value value) {
DatabaseId refDatabase = DatabaseId.fromName(value.getReferenceValue());
DocumentKey key = DocumentKey.fromName(value.getReferenceValue());
DatabaseId database = firestore.getDatabaseId();
if (!refDatabase.equals(database)) {
// TODO: Somehow support foreign references.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@

package com.google.firebase.firestore.core;

import static com.google.firebase.firestore.util.Assert.hardAssert;

import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.FieldPath;
import com.google.firebase.firestore.model.value.ArrayValue;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ProtoValues;
import com.google.firestore.v1.Value;

/** A Filter that implements the array-contains-any operator. */
public class ArrayContainsAnyFilter extends FieldFilter {
ArrayContainsAnyFilter(FieldPath field, FieldValue value) {
ArrayContainsAnyFilter(FieldPath field, Value value) {
super(field, Operator.ARRAY_CONTAINS_ANY, value);
hardAssert(ProtoValues.isArray(value), "ArrayContainsAnyFilter expects an ArrayValue");
}

@Override
public boolean matches(Document doc) {
FieldValue other = doc.getField(getField());
if (!(other instanceof ArrayValue)) {
Value other = doc.getFieldProto(getField());
if (!ProtoValues.isArray(other)) {
return false;
}
for (Value val : other.getProto().getArrayValue().getValuesList()) {
if (ProtoValues.contains(getValue().getProto().getArrayValue().getValuesList(), val)) {
for (Value val : other.getArrayValue().getValuesList()) {
if (ProtoValues.contains(getValue().getArrayValue(), val)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.FieldPath;
import com.google.firebase.firestore.model.value.ArrayValue;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ProtoValues;
import com.google.firestore.v1.Value;

/** A Filter that implements the array-contains operator. */
public class ArrayContainsFilter extends FieldFilter {
ArrayContainsFilter(FieldPath field, FieldValue value) {
ArrayContainsFilter(FieldPath field, Value value) {
super(field, Operator.ARRAY_CONTAINS, value);
}

@Override
public boolean matches(Document doc) {
FieldValue other = doc.getField(getField());
return other instanceof ArrayValue
&& ProtoValues.contains(
other.getProto().getArrayValue().getValuesList(), getValue().getProto());
Value other = doc.getFieldProto(getField());
return ProtoValues.isArray(other) && ProtoValues.contains(other.getArrayValue(), getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import com.google.firebase.firestore.core.OrderBy.Direction;
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.FieldPath;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ReferenceValue;
import com.google.firebase.firestore.model.value.ProtoValues;
import com.google.firestore.v1.Value;
import java.util.List;

/**
Expand All @@ -41,14 +42,14 @@ public final class Bound {
private final boolean before;

/** The index position of this bound */
private final List<FieldValue> position;
private final List<Value> position;

public Bound(List<FieldValue> position, boolean before) {
public Bound(List<Value> position, boolean before) {
this.position = position;
this.before = before;
}

public List<FieldValue> getPosition() {
public List<Value> getPosition() {
return position;
}

Expand All @@ -64,8 +65,13 @@ public String canonicalString() {
} else {
builder.append("a:");
}
for (FieldValue indexComponent : position) {
builder.append(indexComponent.toString());
boolean first = true;
for (Value indexComponent : position) {
if (!first) {
builder.append(",");
}
first = false;
builder.append(ProtoValues.canonicalId(indexComponent));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're fixing the format here, should we separate these values with something?

(Optional; may not be worth it. Just a thought because it may make these more readable.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comma separation.

}
return builder.toString();
}
Expand All @@ -76,18 +82,19 @@ public boolean sortsBeforeDocument(List<OrderBy> orderBy, Document document) {
int comparison = 0;
for (int i = 0; i < position.size(); i++) {
OrderBy orderByComponent = orderBy.get(i);
FieldValue component = position.get(i);
Value component = position.get(i);
if (orderByComponent.field.equals(FieldPath.KEY_PATH)) {
hardAssert(
component instanceof ReferenceValue,
ProtoValues.isReferenceValue(component),
"Bound has a non-key value where the key path is being used %s",
component);
comparison = ((ReferenceValue) component).getKey().compareTo(document.getKey());
comparison =
DocumentKey.fromName(component.getReferenceValue()).compareTo(document.getKey());
} else {
FieldValue docValue = document.getField(orderByComponent.getField());
Value docValue = document.getFieldProto(orderByComponent.getField());
hardAssert(
docValue != null, "Field should exist since document matched the orderBy already.");
comparison = component.compareTo(docValue);
comparison = ProtoValues.compare(component, docValue);
}

if (orderByComponent.getDirection().equals(Direction.DESCENDING)) {
Expand Down
Loading