Skip to content

Commit 212e916

Browse files
Make FieldValue a test-only class
This is a intermediate step before we remove it from all tests
1 parent 3d2eadd commit 212e916

File tree

13 files changed

+116
-33
lines changed

13 files changed

+116
-33
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/model/value/FieldValue.java renamed to firebase-firestore/ktx/src/test/java/com/google/firebase/firestore/model/value/FieldValue.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Represents a FieldValue that is backed by a single Firestore V1 Value proto and implements
2222
* Firestore's Value semantics for ordering and equality.
2323
*/
24+
// TODO(mrschmidt): Drop this class
2425
public class FieldValue implements Comparable<FieldValue> {
2526
final Value internalValue;
2627

firebase-firestore/src/main/java/com/google/firebase/firestore/UserDataWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Object convertValue(Value value) {
5353
switch (value.getValueTypeCase()) {
5454
case MAP_VALUE:
5555
if (ServerTimestampValue.isServerTimestamp(value)) {
56-
return convertServerTimestamp(ServerTimestampValue.valueOf(value));
56+
return convertServerTimestamp(new ServerTimestampValue(value));
5757
}
5858
return convertObject(value.getMapValue().getFieldsMap());
5959
case ARRAY_VALUE:

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteCollectionIndex.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.google.firebase.firestore.model.DocumentKey;
2020
import com.google.firebase.firestore.model.FieldPath;
2121
import com.google.firebase.firestore.model.ResourcePath;
22-
import com.google.firebase.firestore.model.value.FieldValue;
22+
import com.google.firestore.v1.Value;
2323

2424
/**
2525
* A persisted "collection index" of all documents in the local cache (with mutations overlaid on
@@ -38,12 +38,12 @@ public class SQLiteCollectionIndex {
3838
}
3939

4040
/** Adds the specified entry to the index. */
41-
public void addEntry(FieldPath fieldPath, FieldValue fieldValue, DocumentKey documentKey) {
41+
public void addEntry(FieldPath fieldPath, Value fieldValue, DocumentKey documentKey) {
4242
throw new RuntimeException("Not yet implemented.");
4343
}
4444

4545
/** Adds the specified entry to the index. */
46-
public void removeEntry(FieldPath fieldPath, FieldValue fieldValue, DocumentKey documentKey) {
46+
public void removeEntry(FieldPath fieldPath, Value fieldValue, DocumentKey documentKey) {
4747
throw new RuntimeException("Not yet implemented.");
4848
}
4949

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/ServerTimestampOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static ServerTimestampOperation getInstance() {
3131

3232
@Override
3333
public Value applyToLocalView(@Nullable Value previousValue, Timestamp localWriteTime) {
34-
return ServerTimestampValue.valueOf(localWriteTime, previousValue).getProto();
34+
return ServerTimestampValue.valueOf(localWriteTime, previousValue);
3535
}
3636

3737
@Override

firebase-firestore/src/main/java/com/google/firebase/firestore/model/value/ObjectValue.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
/** A structured object value stored in Firestore. */
3030
// TODO(mrschmidt): Rename to DocumentValue
31-
public class ObjectValue extends FieldValue {
31+
public class ObjectValue {
32+
private Value internalValue;
33+
3234
private static final ObjectValue EMPTY_INSTANCE =
3335
new ObjectValue(Value.newBuilder().setMapValue(MapValue.getDefaultInstance()).build());
3436

@@ -38,13 +40,13 @@ public static ObjectValue fromMap(Map<String, Value> value) {
3840
}
3941

4042
public ObjectValue(Value value) {
41-
super(value);
4243
hardAssert(
4344
value.getValueTypeCase() == Value.ValueTypeCase.MAP_VALUE,
4445
"ObjectValues should be backed by a MapValue");
4546
hardAssert(
4647
!ServerTimestampValue.isServerTimestamp(value),
47-
"ServerTimestamps should be converted to ServerTimestampValue");
48+
"ServerTimestamps should not be treated as ObjectValues");
49+
this.internalValue = value;
4850
}
4951

5052
public static ObjectValue emptyObject() {
@@ -109,6 +111,26 @@ private FieldMask extractFieldMask(MapValue value) {
109111
}
110112
}
111113

114+
/** Returns the Protobuf that backs this ObjectValue. */
115+
public Value getProto() {
116+
return internalValue;
117+
}
118+
119+
@Override
120+
public boolean equals(Object o) {
121+
if (this == o) {
122+
return true;
123+
} else if (o instanceof ObjectValue) {
124+
return ProtoValues.equals(internalValue, ((ObjectValue) o).internalValue);
125+
}
126+
return false;
127+
}
128+
129+
@Override
130+
public int hashCode() {
131+
return internalValue.hashCode();
132+
}
133+
112134
/** Creates a ObjectValue.Builder instance that is based on the current value. */
113135
public ObjectValue.Builder toBuilder() {
114136
return new ObjectValue.Builder(this);

firebase-firestore/src/main/java/com/google/firebase/firestore/model/value/ServerTimestampValue.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* <li>They sort after all TimestampValues. With respect to other ServerTimestampValues, they sort
3838
* by their localWriteTime.
3939
*/
40-
public final class ServerTimestampValue extends FieldValue {
40+
public final class ServerTimestampValue {
4141
private static final String SERVER_TIMESTAMP_SENTINEL = "server_timestamp";
4242
private static final String TYPE_KEY = "__type__";
4343
private static final String PREVIOUS_VALUE_KEY = "__previous_value__";
@@ -48,18 +48,16 @@ public static boolean isServerTimestamp(@Nullable Value value) {
4848
return type != null && SERVER_TIMESTAMP_SENTINEL.equals(type.getStringValue());
4949
}
5050

51-
ServerTimestampValue(Value value) {
52-
super(value);
51+
private final Value internalValue;
52+
53+
public ServerTimestampValue(Value value) {
5354
hardAssert(
5455
ServerTimestampValue.isServerTimestamp(value),
5556
"Backing value is not a ServerTimestampValue");
57+
this.internalValue = value;
5658
}
5759

58-
public static ServerTimestampValue valueOf(Value value) {
59-
return new ServerTimestampValue(value);
60-
}
61-
62-
public static FieldValue valueOf(Timestamp localWriteTime, @Nullable Value previousValue) {
60+
public static Value valueOf(Timestamp localWriteTime, @Nullable Value previousValue) {
6361
Value encodedType = Value.newBuilder().setStringValue(SERVER_TIMESTAMP_SENTINEL).build();
6462
Value encodeWriteTime =
6563
Value.newBuilder()
@@ -78,7 +76,7 @@ public static FieldValue valueOf(Timestamp localWriteTime, @Nullable Value previ
7876
mapRepresentation.putFields(PREVIOUS_VALUE_KEY, previousValue);
7977
}
8078

81-
return new ServerTimestampValue(Value.newBuilder().setMapValue(mapRepresentation).build());
79+
return Value.newBuilder().setMapValue(mapRepresentation).build();
8280
}
8381

8482
/**

firebase-firestore/src/test/java/com/google/firebase/firestore/QuerySnapshotTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public void testToObjects() {
8686
.thenReturn(new FirebaseFirestoreSettings.Builder().build());
8787

8888
ObjectValue objectData =
89-
ObjectValue.fromMap(
90-
map("timestamp", ServerTimestampValue.valueOf(Timestamp.now(), null).getProto()));
89+
ObjectValue.fromMap(map("timestamp", ServerTimestampValue.valueOf(Timestamp.now(), null)));
9190
QuerySnapshot foo = TestUtil.querySnapshot("foo", map(), map("a", objectData), true, false);
9291

9392
List<POJO> docs = foo.toObjects(POJO.class);

firebase-firestore/src/test/java/com/google/firebase/firestore/UserDataWriterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public void testConvertsSimpleObjects() {
242242
"c", wrap(true),
243243
"d", wrap(null));
244244

245-
FieldValue wrappedActual = wrapObject(actual);
245+
ObjectValue wrappedActual = wrapObject(actual);
246246
assertEquals(wrappedActual, wrappedExpected);
247247
}
248248

@@ -256,7 +256,7 @@ private static ObjectValue fromMap(Object... entries) {
256256

257257
@Test
258258
public void testConvertsNestedObjects() {
259-
FieldValue actual = wrapObject("a", map("b", map("c", "foo"), "d", true));
259+
ObjectValue actual = wrapObject("a", map("b", map("c", "foo"), "d", true));
260260
ObjectValue.Builder expected = ObjectValue.newBuilder();
261261
expected.set(field("a.b.c"), valueOf("foo"));
262262
expected.set(field("a.d"), valueOf(true));

firebase-firestore/src/test/java/com/google/firebase/firestore/model/FieldValueTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,15 @@ public void testExtractsFields() {
8080

8181
@Test
8282
public void testExtractsFieldMask() {
83-
FieldValue val =
83+
ObjectValue val =
8484
wrapObject(
8585
"a",
8686
"b",
8787
"map",
8888
map("a", 1, "b", true, "c", "string", "nested", map("d", "e")),
8989
"emptymap",
9090
map());
91-
assertTrue(val instanceof ObjectValue);
92-
FieldMask mask = ((ObjectValue) val).getFieldMask();
91+
FieldMask mask = val.getFieldMask();
9392
assertEquals(fieldMask("a", "map.a", "map.b", "map.c", "map.nested.d", "emptymap"), mask);
9493
}
9594

@@ -381,7 +380,7 @@ public void testCanonicalIds() {
381380
assertCanonicalId(wrap(new Timestamp(30, 1000)), "time(30,1000)");
382381
assertCanonicalId(wrap("a"), "a");
383382
assertCanonicalId(wrap(blob(1, 2, 3)), "010203");
384-
assertCanonicalId(wrapRef(dbId("p1", "d1"), key("c1/doc1")), "c1/doc1");
383+
assertCanonicalId(new FieldValue(wrapRef(dbId("p1", "d1"), key("c1/doc1"))), "c1/doc1");
385384
assertCanonicalId(wrap(new GeoPoint(30, 60)), "geo(30.0,60.0)");
386385
assertCanonicalId(wrap(Arrays.asList(1, 2, 3)), "[1,2,3]");
387386
assertCanonicalId(wrap(map("a", 1, "b", 2, "c", "3")), "{a:1,b:2,c:3}");

firebase-firestore/src/test/java/com/google/firebase/firestore/model/MutationTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ public void testAppliesLocalServerTimestampTransformsToDocuments() {
156156
ObjectValue expectedData =
157157
wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
158158
com.google.firebase.firestore.model.value.FieldValue fieldValue =
159-
ServerTimestampValue.valueOf(timestamp, valueOf("bar-value"));
159+
new com.google.firebase.firestore.model.value.FieldValue(
160+
ServerTimestampValue.valueOf(timestamp, valueOf("bar-value")));
160161
expectedData = expectedData.toBuilder().set(field("foo.bar"), fieldValue.getProto()).build();
161162

162163
Document expectedDoc =
@@ -308,9 +309,10 @@ public void testCreateArrayUnionTransform() {
308309
transformMutation(
309310
"collection/key",
310311
map(
311-
"a", FieldValue.arrayUnion("tag"),
312+
"a",
313+
FieldValue.arrayUnion("tag"),
312314
"bar.baz",
313-
FieldValue.arrayUnion(true, map("nested", map("a", Arrays.asList(1, 2))))));
315+
FieldValue.arrayUnion(true, map("nested", map("a", Arrays.asList(1, 2))))));
314316
assertEquals(2, transform.getFieldTransforms().size());
315317

316318
FieldTransform first = transform.getFieldTransforms().get(0);

firebase-firestore/src/test/java/com/google/firebase/firestore/remote/RemoteSerializerTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.google.firebase.firestore.model.SnapshotVersion;
5252
import com.google.firebase.firestore.model.mutation.Mutation;
5353
import com.google.firebase.firestore.model.value.FieldValue;
54+
import com.google.firebase.firestore.model.value.ObjectValue;
5455
import com.google.firebase.firestore.model.value.ProtoValues;
5556
import com.google.firebase.firestore.remote.WatchChange.WatchTargetChange;
5657
import com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType;
@@ -121,7 +122,7 @@ private void assertRoundTrip(
121122
com.google.firestore.v1.Value actual = value.getProto();
122123
assertEquals(typeCase, actual.getValueTypeCase());
123124
assertEquals(proto, actual);
124-
assertEquals(value, new FieldValue(proto));
125+
assertEquals(value.getProto(), new FieldValue(proto));
125126
}
126127

127128
@Test
@@ -258,7 +259,7 @@ public void testEncodeArrays() {
258259

259260
@Test
260261
public void testEncodesNestedObjects() {
261-
FieldValue model =
262+
ObjectValue model =
262263
TestUtil.wrapObject(
263264
map(
264265
"b",
@@ -304,7 +305,7 @@ public void testEncodesNestedObjects() {
304305
.putFields("o", valueBuilder().setMapValue(middle).build());
305306

306307
com.google.firestore.v1.Value proto = valueBuilder().setMapValue(obj).build();
307-
assertRoundTrip(model, proto, ValueTypeCase.MAP_VALUE);
308+
assertRoundTrip(new FieldValue(model.getProto()), proto, ValueTypeCase.MAP_VALUE);
308309
}
309310

310311
@Test
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore.model.value;
16+
17+
import androidx.annotation.NonNull;
18+
import com.google.firestore.v1.Value;
19+
20+
/**
21+
* Represents a FieldValue that is backed by a single Firestore V1 Value proto and implements
22+
* Firestore's Value semantics for ordering and equality.
23+
*/
24+
// TODO(mrschmidt): Drop this class
25+
public class FieldValue implements Comparable<FieldValue> {
26+
final Value internalValue;
27+
28+
public FieldValue(Value value) {
29+
this.internalValue = value;
30+
}
31+
32+
/** Returns Firestore Value Protobuf that backs this FieldValuee */
33+
public Value getProto() {
34+
return internalValue;
35+
}
36+
37+
@Override
38+
public boolean equals(Object o) {
39+
if (this == o) {
40+
return true;
41+
} else if (o instanceof FieldValue) {
42+
return ProtoValues.equals(internalValue, ((FieldValue) o).internalValue);
43+
}
44+
return false;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return internalValue.hashCode();
50+
}
51+
52+
@Override
53+
public int compareTo(@NonNull FieldValue other) {
54+
return ProtoValues.compare(internalValue, other.internalValue);
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return ProtoValues.canonicalId(internalValue);
60+
}
61+
}

firebase-firestore/src/testUtil/java/com/google/firebase/firestore/testutil/TestUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public static FieldValue wrap(Object value) {
135135
return new FieldValue(dataReader.parseQueryValue(value));
136136
}
137137

138-
public static FieldValue wrapRef(DatabaseId databaseId, DocumentKey key) {
139-
return new FieldValue(ProtoValues.refValue(databaseId, key));
138+
public static Value wrapRef(DatabaseId databaseId, DocumentKey key) {
139+
return ProtoValues.refValue(databaseId, key);
140140
}
141141

142142
public static ObjectValue wrapObject(Map<String, Object> value) {

0 commit comments

Comments
 (0)