Skip to content

Commit c8d1ce8

Browse files
Review feedback
1 parent 529881b commit c8d1ce8

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

firebase-firestore/src/roboUtil/java/com/google/firebase/firestore/ValueHelper.java renamed to firebase-firestore/src/roboUtil/java/com/google/firebase/firestore/Values.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,71 @@
2222
import com.google.protobuf.NullValue;
2323
import com.google.type.LatLng;
2424
import java.util.List;
25+
import java.util.Map;
2526

2627
/** Test helper to create Firestore Value protos from Java types. */
27-
public class ValueHelper {
28+
public class Values {
2829

30+
// TODO(mrschmidt): Move into UserDataConverter
2931
public static Value valueOf(Object o) {
3032
if (o instanceof Value) {
3133
return (Value) o;
32-
} else if (o instanceof String) {
33-
return (Value.newBuilder().setStringValue((String) o).build());
34+
} else if (o == null) {
35+
return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
36+
} else if (o instanceof Boolean) {
37+
return Value.newBuilder().setBooleanValue((Boolean) o).build();
3438
} else if (o instanceof Integer) {
35-
return (Value.newBuilder().setIntegerValue((long) (Integer) o).build());
39+
return Value.newBuilder().setIntegerValue((Integer) o).build();
3640
} else if (o instanceof Long) {
37-
return (Value.newBuilder().setIntegerValue((Long) o).build());
41+
return Value.newBuilder().setIntegerValue((Long) o).build();
3842
} else if (o instanceof Double) {
39-
return (Value.newBuilder().setDoubleValue((Double) o).build());
40-
} else if (o instanceof Boolean) {
41-
return (Value.newBuilder().setBooleanValue((Boolean) o).build());
43+
return Value.newBuilder().setDoubleValue((Double) o).build();
4244
} else if (o instanceof Timestamp) {
4345
Timestamp timestamp = (Timestamp) o;
44-
return (Value.newBuilder()
46+
return Value.newBuilder()
4547
.setTimestampValue(
4648
com.google.protobuf.Timestamp.newBuilder()
4749
.setSeconds(timestamp.getSeconds())
48-
.setNanos(timestamp.getNanoseconds())
49-
.build())
50-
.build());
50+
.setNanos(timestamp.getNanoseconds()))
51+
.build();
52+
} else if (o instanceof String) {
53+
return Value.newBuilder().setStringValue((String) o).build();
54+
} else if (o instanceof Blob) {
55+
return Value.newBuilder().setBytesValue(((Blob) o).toByteString()).build();
56+
57+
} else if (o instanceof DocumentReference) {
58+
return Value.newBuilder()
59+
.setReferenceValue(
60+
"projects/project/databases/(default)/documents/" + ((DocumentReference) o).getPath())
61+
.build();
5162
} else if (o instanceof GeoPoint) {
5263
GeoPoint geoPoint = (GeoPoint) o;
53-
return (Value.newBuilder()
64+
return Value.newBuilder()
5465
.setGeoPointValue(
5566
LatLng.newBuilder()
5667
.setLatitude(geoPoint.getLatitude())
57-
.setLongitude(geoPoint.getLongitude())
58-
.build())
59-
.build());
60-
} else if (o instanceof Blob) {
61-
return (Value.newBuilder().setBytesValue(((Blob) o).toByteString()).build());
62-
} else if (o instanceof DocumentReference) {
63-
return (Value.newBuilder()
64-
.setReferenceValue(
65-
"projects/projectId/databases/(default)/documents/"
66-
+ ((DocumentReference) o).getPath())
67-
.build());
68+
.setLongitude(geoPoint.getLongitude()))
69+
.build();
70+
6871
} else if (o instanceof List) {
6972
ArrayValue.Builder list = ArrayValue.newBuilder();
7073
for (Object element : (List) o) {
7174
list.addValues(valueOf(element));
7275
}
73-
return (Value.newBuilder().setArrayValue(list).build());
74-
} else if (o == null) {
75-
return (Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
76+
return Value.newBuilder().setArrayValue(list).build();
77+
} else if (o instanceof Map) {
78+
com.google.firestore.v1.MapValue.Builder builder =
79+
com.google.firestore.v1.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();
7684
}
7785

78-
throw new UnsupportedOperationException();
86+
throw new UnsupportedOperationException("Failed to serialize object: " + o);
7987
}
8088

89+
/** Creates a MapValue from a list of key/value arguments. */
8190
public static Value map(Object... entries) {
8291
com.google.firestore.v1.MapValue.Builder builder =
8392
com.google.firestore.v1.MapValue.newBuilder();
@@ -87,7 +96,7 @@ public static Value map(Object... entries) {
8796
return Value.newBuilder().setMapValue(builder).build();
8897
}
8998

90-
public static Value wrapRef(DatabaseId dbId, DocumentKey key) {
99+
public static Value refValue(DatabaseId dbId, DocumentKey key) {
91100
return Value.newBuilder()
92101
.setReferenceValue(
93102
String.format(

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)