Skip to content

Commit 94844bb

Browse files
Adding test utilities to create Value types (#1158)
1 parent e7ac8b9 commit 94844bb

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2020 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;
16+
17+
import com.google.firebase.Timestamp;
18+
import com.google.firebase.firestore.model.DatabaseId;
19+
import com.google.firebase.firestore.model.DocumentKey;
20+
import com.google.firestore.v1.ArrayValue;
21+
import com.google.firestore.v1.MapValue;
22+
import com.google.firestore.v1.Value;
23+
import com.google.protobuf.NullValue;
24+
import com.google.type.LatLng;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
/** Test helper to create Firestore Value protos from Java types. */
29+
public class Values {
30+
31+
// TODO(mrschmidt): Move into UserDataConverter
32+
public static Value valueOf(Object o) {
33+
if (o instanceof Value) {
34+
return (Value) o;
35+
} else if (o == null) {
36+
return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
37+
} else if (o instanceof Boolean) {
38+
return Value.newBuilder().setBooleanValue((Boolean) o).build();
39+
} else if (o instanceof Integer) {
40+
return Value.newBuilder().setIntegerValue((Integer) o).build();
41+
} else if (o instanceof Long) {
42+
return Value.newBuilder().setIntegerValue((Long) o).build();
43+
} else if (o instanceof Double) {
44+
return Value.newBuilder().setDoubleValue((Double) o).build();
45+
} else if (o instanceof Timestamp) {
46+
Timestamp timestamp = (Timestamp) o;
47+
return Value.newBuilder()
48+
.setTimestampValue(
49+
com.google.protobuf.Timestamp.newBuilder()
50+
.setSeconds(timestamp.getSeconds())
51+
.setNanos(timestamp.getNanoseconds()))
52+
.build();
53+
} else if (o instanceof String) {
54+
return Value.newBuilder().setStringValue((String) o).build();
55+
} else if (o instanceof Blob) {
56+
return Value.newBuilder().setBytesValue(((Blob) o).toByteString()).build();
57+
58+
} else if (o instanceof DocumentReference) {
59+
return Value.newBuilder()
60+
.setReferenceValue(
61+
"projects/project/databases/(default)/documents/" + ((DocumentReference) o).getPath())
62+
.build();
63+
} else if (o instanceof GeoPoint) {
64+
GeoPoint geoPoint = (GeoPoint) o;
65+
return Value.newBuilder()
66+
.setGeoPointValue(
67+
LatLng.newBuilder()
68+
.setLatitude(geoPoint.getLatitude())
69+
.setLongitude(geoPoint.getLongitude()))
70+
.build();
71+
72+
} else if (o instanceof List) {
73+
ArrayValue.Builder list = ArrayValue.newBuilder();
74+
for (Object element : (List) o) {
75+
list.addValues(valueOf(element));
76+
}
77+
return Value.newBuilder().setArrayValue(list).build();
78+
} else if (o instanceof Map) {
79+
MapValue.Builder builder = MapValue.newBuilder();
80+
for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) {
81+
builder.putFields(entry.getKey(), valueOf(entry.getValue()));
82+
}
83+
return Value.newBuilder().setMapValue(builder).build();
84+
}
85+
86+
throw new UnsupportedOperationException("Failed to serialize object: " + o);
87+
}
88+
89+
/** Creates a MapValue from a list of key/value arguments. */
90+
public static Value map(Object... entries) {
91+
MapValue.Builder builder = MapValue.newBuilder();
92+
for (int i = 0; i < entries.length; i += 2) {
93+
builder.putFields((String) entries[i], valueOf(entries[i + 1]));
94+
}
95+
return Value.newBuilder().setMapValue(builder).build();
96+
}
97+
98+
public static Value refValue(DatabaseId dbId, DocumentKey key) {
99+
return Value.newBuilder()
100+
.setReferenceValue(
101+
String.format(
102+
"projects/%s/databases/%s/documents/%s",
103+
dbId.getProjectId(), dbId.getDatabaseId(), key.toString()))
104+
.build();
105+
}
106+
}

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteSchemaTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private void addMutationBatch(SQLiteDatabase db, int batchId, String uid, String
268268
Write.newBuilder()
269269
.setUpdate(
270270
Document.newBuilder()
271-
.setName("projects/projectId/databases/(default)/documents/" + doc)));
271+
.setName("projects/project/databases/(default)/documents/" + doc)));
272272
}
273273

274274
db.execSQL(

0 commit comments

Comments
 (0)