Skip to content

Drop remaining primitive FieldValue types #1207

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 26 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1602062
Protobuf-backed FieldValues
schmidt-sebastian Feb 4, 2020
804a1b7
Merge branch 'mrschmidt/rewritefieldvalue' into mrschmidt/final
schmidt-sebastian Feb 4, 2020
3d5d7c2
Merge branch 'mrschmidt/rewritefieldvalue' into mrschmidt/final
schmidt-sebastian Feb 4, 2020
120ca33
Feedback
schmidt-sebastian Feb 5, 2020
82c0095
Update test-only variable
schmidt-sebastian Feb 5, 2020
98953a3
Add ProtoValues.contains()
schmidt-sebastian Feb 5, 2020
940a268
Migrate ArrayTransforms to use Values
schmidt-sebastian Feb 5, 2020
7ea5b37
Migrate TransformResult to use Value
schmidt-sebastian Feb 5, 2020
637838b
Drop Array, Reference, Number, Integer and DoubleValue
schmidt-sebastian Feb 5, 2020
522db2f
Drop remaining primitive FieldValue types
schmidt-sebastian Feb 6, 2020
b5cc998
Fix Kotlin
schmidt-sebastian Feb 6, 2020
9f87db1
Merge branch 'mrschmidt/droparray' into mrschmidt/droprest
schmidt-sebastian Feb 6, 2020
162f071
More Kotlin fixes
schmidt-sebastian Feb 6, 2020
3d2eadd
Merge branch 'mrschmidt/droparray' into mrschmidt/droprest
schmidt-sebastian Feb 6, 2020
13e01c8
Review
schmidt-sebastian Feb 6, 2020
baf650e
Merge branch 'mrschmidt/rewritefieldvalue' into mrschmidt/final
schmidt-sebastian Feb 6, 2020
aa7b3ec
Merge branch 'mrschmidt/final' into mrschmidt/migratevalues
schmidt-sebastian Feb 6, 2020
774598a
Feedback
schmidt-sebastian Feb 6, 2020
490c924
Make FieldValue package-private
schmidt-sebastian Feb 6, 2020
0abdfb1
Merge branch 'mrschmidt/migratevalues' into mrschmidt/droparray
schmidt-sebastian Feb 6, 2020
6ef7289
Merge
schmidt-sebastian Feb 6, 2020
3f2efb4
Merge
schmidt-sebastian Feb 6, 2020
067c43e
Merge
schmidt-sebastian Feb 6, 2020
585ce2a
Review
schmidt-sebastian Feb 6, 2020
9ab4f2c
Fix Kotlin
schmidt-sebastian Feb 6, 2020
2ce8a22
Fix Kotlin
schmidt-sebastian Feb 6, 2020
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,12 +36,12 @@ 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 FieldValue.valueOf(dataReader.parseQueryValue(value));
return new FieldValue(dataReader.parseQueryValue(value));
}

public static ObjectValue wrapObject(Map<String, Object> value) {
// Cast is safe here because value passed in is a map
return (ObjectValue) wrap(value);
return new ObjectValue(wrap(value).getProto());
}

public static DocumentKey key(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.google.firebase.Timestamp;
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.util.CustomClassMapper;
import com.google.firestore.v1.Value;
import java.util.Date;
import java.util.Map;

Expand Down Expand Up @@ -529,11 +529,11 @@ private Object getInternal(
@NonNull ServerTimestampBehavior serverTimestampBehavior,
boolean timestampsInSnapshots) {
if (doc != null) {
FieldValue val = doc.getField(fieldPath);
Value val = doc.getField(fieldPath);
if (val != null) {
UserDataWriter userDataWriter =
new UserDataWriter(firestore, timestampsInSnapshots, serverTimestampBehavior);
return userDataWriter.convertValue(val.getProto());
return userDataWriter.convertValue(val);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ private Bound boundFromDocumentSnapshot(
if (orderBy.getField().equals(com.google.firebase.firestore.model.FieldPath.KEY_PATH)) {
components.add(ProtoValues.refValue(firestore.getDatabaseId(), document.getKey()));
} else {
Value value = document.getFieldProto(orderBy.getField());
Value value = document.getField(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 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ Map<String, Object> convertObject(Map<String, Value> mapValue) {
private Object convertServerTimestamp(ServerTimestampValue value) {
switch (serverTimestampBehavior) {
case PREVIOUS:
return value.getPreviousValue() == null
? null
: convertValue(value.getPreviousValue().getProto());
return value.getPreviousValue() == null ? null : convertValue(value.getPreviousValue());
case ESTIMATE:
return !timestampsInSnapshots
? value.getLocalWriteTime().toDate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ArrayContainsAnyFilter extends FieldFilter {

@Override
public boolean matches(Document doc) {
Value other = doc.getFieldProto(getField());
Value other = doc.getField(getField());
if (!ProtoValues.isArray(other)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ArrayContainsFilter extends FieldFilter {

@Override
public boolean matches(Document doc) {
Value other = doc.getFieldProto(getField());
Value other = doc.getField(getField());
return ProtoValues.isArray(other) && ProtoValues.contains(other.getArrayValue(), getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean sortsBeforeDocument(List<OrderBy> orderBy, Document document) {
comparison =
DocumentKey.fromName(component.getReferenceValue()).compareTo(document.getKey());
} else {
Value docValue = document.getFieldProto(orderByComponent.getField());
Value docValue = document.getField(orderByComponent.getField());
hardAssert(
docValue != null, "Field should exist since document matched the orderBy already.");
comparison = ProtoValues.compare(component, docValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static FieldFilter create(FieldPath path, Operator operator, Value value)

@Override
public boolean matches(Document doc) {
Value other = doc.getFieldProto(field);
Value other = doc.getField(field);
// Only compare types with matching backend order (such as double and int).
return other != null
&& ProtoValues.typeOrder(other) == ProtoValues.typeOrder(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class InFilter extends FieldFilter {

@Override
public boolean matches(Document doc) {
Value other = doc.getFieldProto(getField());
Value other = doc.getField(getField());
return other != null && ProtoValues.contains(getValue().getArrayValue(), other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

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

/** Represents a sort order for a Firestore Query */
public class OrderBy {
Expand Down Expand Up @@ -61,11 +62,11 @@ int compare(Document d1, Document d2) {
if (field.equals(FieldPath.KEY_PATH)) {
return direction.getComparisonModifier() * d1.getKey().compareTo(d2.getKey());
} else {
FieldValue v1 = d1.getField(field);
FieldValue v2 = d2.getField(field);
Value v1 = d1.getField(field);
Value v2 = d2.getField(field);
Assert.hardAssert(
v1 != null && v2 != null, "Trying to compare documents on fields that don't exist.");
return direction.getComparisonModifier() * v1.compareTo(v2);
return direction.getComparisonModifier() * ProtoValues.compare(v1, v2);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ObjectValue;
import com.google.firestore.v1.Value;
import java.util.Comparator;
Expand Down Expand Up @@ -63,15 +62,10 @@ public ObjectValue getData() {
return objectValue;
}

public @Nullable FieldValue getField(FieldPath path) {
public @Nullable Value getField(FieldPath path) {
return objectValue.get(path);
}

public @Nullable Value getFieldProto(FieldPath path) {
FieldValue fieldValue = getField(path);
return fieldValue != null ? fieldValue.getProto() : null;
}

public boolean hasLocalMutations() {
return documentState.equals(DocumentState.LOCAL_MUTATIONS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import com.google.firebase.firestore.model.MaybeDocument;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firebase.firestore.model.UnknownDocument;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ObjectValue;
import com.google.firestore.v1.Value;

/**
* A mutation that modifies fields of the document at the given key with the given values. The
Expand Down Expand Up @@ -154,11 +154,11 @@ private ObjectValue patchObject(ObjectValue obj) {
ObjectValue.Builder builder = obj.toBuilder();
for (FieldPath path : mask.getMask()) {
if (!path.isEmpty()) {
FieldValue newValue = value.get(path);
Value newValue = value.get(path);
if (newValue == null) {
builder.delete(path);
} else {
builder.set(path, newValue.getProto());
builder.set(path, newValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ObjectValue extractBaseValue(@Nullable MaybeDocument maybeDoc) {
for (FieldTransform transform : fieldTransforms) {
Value existingValue = null;
if (maybeDoc instanceof Document) {
existingValue = ((Document) maybeDoc).getFieldProto(transform.getFieldPath());
existingValue = ((Document) maybeDoc).getField(transform.getFieldPath());
}

Value coercedValue = transform.getOperation().computeBaseValue(existingValue);
Expand Down Expand Up @@ -180,7 +180,7 @@ private List<Value> serverTransformResults(

Value previousValue = null;
if (baseDoc instanceof Document) {
previousValue = ((Document) baseDoc).getFieldProto(fieldTransform.getFieldPath());
previousValue = ((Document) baseDoc).getField(fieldTransform.getFieldPath());
}

transformResults.add(
Expand All @@ -207,14 +207,14 @@ private List<Value> localTransformResults(

Value previousValue = null;
if (maybeDoc instanceof Document) {
previousValue = ((Document) maybeDoc).getFieldProto(fieldTransform.getFieldPath());
previousValue = ((Document) maybeDoc).getField(fieldTransform.getFieldPath());
}

if (previousValue == null && baseDoc instanceof Document) {
// If the current document does not contain a value for the mutated field, use the value
// that existed before applying this mutation batch. This solves an edge case where a
// PatchMutation clears the values in a nested map before the TransformMutation is applied.
previousValue = ((Document) baseDoc).getFieldProto(fieldTransform.getFieldPath());
previousValue = ((Document) baseDoc).getField(fieldTransform.getFieldPath());
}

transformResults.add(transform.applyToLocalView(previousValue, localWriteTime));
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,20 @@

package com.google.firebase.firestore.model.value;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firestore.v1.Value;

/**
* Represents a FieldValue that is backed by a single Firestore V1 Value proto and implements
* Firestore's Value semantics for ordering and equality.
*
* <p>Supported types are:
*
* <ul>
* <li>Null
* <li>Boolean
* <li>Long
* <li>Double
* <li>Timestamp
* <li>ServerTimestamp (a sentinel used in uncommitted writes)
* <li>String
* <li>Binary
* <li>(Document) References
* <li>GeoPoint
* <li>Array
* <li>Object
* </ul>
*/
public class FieldValue implements Comparable<FieldValue> {
/** The order of types in Firestore; this order is defined by the backend. */
static final int TYPE_ORDER_NULL = 0;

static final int TYPE_ORDER_BOOLEAN = 1;
static final int TYPE_ORDER_NUMBER = 2;
static final int TYPE_ORDER_TIMESTAMP = 3;
static final int TYPE_ORDER_STRING = 4;
static final int TYPE_ORDER_BLOB = 5;
static final int TYPE_ORDER_REFERENCE = 6;
static final int TYPE_ORDER_GEOPOINT = 7;
static final int TYPE_ORDER_ARRAY = 8;
static final int TYPE_ORDER_OBJECT = 9;

final Value internalValue;

FieldValue(Value value) {
public FieldValue(Value value) {
this.internalValue = value;
}

/** Creates a new FieldValue based on the Protobuf Value. */
// TODO(mrschmidt): Unify with UserDataReader.parseScalarValue()
public static @Nullable FieldValue valueOf(@Nullable Value value) {
if (value == null) {
return null;
}

switch (value.getValueTypeCase()) {
case NULL_VALUE:
return new FieldValue(value);
case BOOLEAN_VALUE:
return new BooleanValue(value);
case INTEGER_VALUE:
return new FieldValue(value);
case DOUBLE_VALUE:
return new FieldValue(value);
case TIMESTAMP_VALUE:
return new TimestampValue(value);
case STRING_VALUE:
return new StringValue(value);
case BYTES_VALUE:
return new BlobValue(value);
case REFERENCE_VALUE:
return new FieldValue(value);
case GEO_POINT_VALUE:
return new GeoPointValue(value);
case ARRAY_VALUE:
return new FieldValue(value);
case MAP_VALUE:
if (ServerTimestampValue.isServerTimestamp(value)) {
return new ServerTimestampValue(value);
}
return new ObjectValue(value);
default:
throw fail("Invlaid value type: %s", value.getValueTypeCase());
}
}

/** Returns Firestore Value Protobuf that backs this FieldValuee */
public Value getProto() {
return internalValue;
Expand Down
Loading