22
22
import com .google .protobuf .NullValue ;
23
23
import com .google .type .LatLng ;
24
24
import java .util .List ;
25
+ import java .util .Map ;
25
26
26
27
/** Test helper to create Firestore Value protos from Java types. */
27
- public class ValueHelper {
28
+ public class Values {
28
29
30
+ // TODO(mrschmidt): Move into UserDataConverter
29
31
public static Value valueOf (Object o ) {
30
32
if (o instanceof Value ) {
31
33
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 ();
34
38
} else if (o instanceof Integer ) {
35
- return ( Value .newBuilder ().setIntegerValue ((long ) ( Integer ) o ).build () );
39
+ return Value .newBuilder ().setIntegerValue ((Integer ) o ).build ();
36
40
} else if (o instanceof Long ) {
37
- return ( Value .newBuilder ().setIntegerValue ((Long ) o ).build () );
41
+ return Value .newBuilder ().setIntegerValue ((Long ) o ).build ();
38
42
} 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 ();
42
44
} else if (o instanceof Timestamp ) {
43
45
Timestamp timestamp = (Timestamp ) o ;
44
- return ( Value .newBuilder ()
46
+ return Value .newBuilder ()
45
47
.setTimestampValue (
46
48
com .google .protobuf .Timestamp .newBuilder ()
47
49
.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 ();
51
62
} else if (o instanceof GeoPoint ) {
52
63
GeoPoint geoPoint = (GeoPoint ) o ;
53
- return ( Value .newBuilder ()
64
+ return Value .newBuilder ()
54
65
.setGeoPointValue (
55
66
LatLng .newBuilder ()
56
67
.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
+
68
71
} else if (o instanceof List ) {
69
72
ArrayValue .Builder list = ArrayValue .newBuilder ();
70
73
for (Object element : (List ) o ) {
71
74
list .addValues (valueOf (element ));
72
75
}
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 ();
76
84
}
77
85
78
- throw new UnsupportedOperationException ();
86
+ throw new UnsupportedOperationException ("Failed to serialize object: " + o );
79
87
}
80
88
89
+ /** Creates a MapValue from a list of key/value arguments. */
81
90
public static Value map (Object ... entries ) {
82
91
com .google .firestore .v1 .MapValue .Builder builder =
83
92
com .google .firestore .v1 .MapValue .newBuilder ();
@@ -87,7 +96,7 @@ public static Value map(Object... entries) {
87
96
return Value .newBuilder ().setMapValue (builder ).build ();
88
97
}
89
98
90
- public static Value wrapRef (DatabaseId dbId , DocumentKey key ) {
99
+ public static Value refValue (DatabaseId dbId , DocumentKey key ) {
91
100
return Value .newBuilder ()
92
101
.setReferenceValue (
93
102
String .format (
0 commit comments