|
19 | 19 | import androidx.annotation.NonNull;
|
20 | 20 | import androidx.annotation.Nullable;
|
21 | 21 | import com.google.firebase.Timestamp;
|
22 |
| -import com.google.firebase.firestore.model.DatabaseId; |
23 | 22 | import com.google.firebase.firestore.model.Document;
|
24 | 23 | import com.google.firebase.firestore.model.DocumentKey;
|
25 |
| -import com.google.firebase.firestore.model.value.ArrayValue; |
26 | 24 | import com.google.firebase.firestore.model.value.FieldValue;
|
27 |
| -import com.google.firebase.firestore.model.value.ObjectValue; |
28 |
| -import com.google.firebase.firestore.model.value.ReferenceValue; |
29 |
| -import com.google.firebase.firestore.model.value.ServerTimestampValue; |
30 |
| -import com.google.firebase.firestore.model.value.TimestampValue; |
31 | 25 | import com.google.firebase.firestore.util.CustomClassMapper;
|
32 |
| -import com.google.firebase.firestore.util.Logger; |
33 |
| -import java.util.ArrayList; |
34 | 26 | import java.util.Date;
|
35 |
| -import java.util.HashMap; |
36 |
| -import java.util.List; |
37 | 27 | import java.util.Map;
|
38 | 28 |
|
39 | 29 | /**
|
@@ -78,18 +68,6 @@ public enum ServerTimestampBehavior {
|
78 | 68 | static final ServerTimestampBehavior DEFAULT = ServerTimestampBehavior.NONE;
|
79 | 69 | }
|
80 | 70 |
|
81 |
| - /** Holds settings that define field value deserialization options. */ |
82 |
| - static class FieldValueOptions { |
83 |
| - final ServerTimestampBehavior serverTimestampBehavior; |
84 |
| - final boolean timestampsInSnapshotsEnabled; |
85 |
| - |
86 |
| - private FieldValueOptions( |
87 |
| - ServerTimestampBehavior serverTimestampBehavior, boolean timestampsInSnapshotsEnabled) { |
88 |
| - this.serverTimestampBehavior = serverTimestampBehavior; |
89 |
| - this.timestampsInSnapshotsEnabled = timestampsInSnapshotsEnabled; |
90 |
| - } |
91 |
| - } |
92 |
| - |
93 | 71 | private final FirebaseFirestore firestore;
|
94 | 72 |
|
95 | 73 | private final DocumentKey key;
|
@@ -166,13 +144,12 @@ public Map<String, Object> getData() {
|
166 | 144 | public Map<String, Object> getData(@NonNull ServerTimestampBehavior serverTimestampBehavior) {
|
167 | 145 | checkNotNull(
|
168 | 146 | serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
|
169 |
| - return doc == null |
170 |
| - ? null |
171 |
| - : convertObject( |
172 |
| - doc.getData(), |
173 |
| - new FieldValueOptions( |
174 |
| - serverTimestampBehavior, |
175 |
| - firestore.getFirestoreSettings().areTimestampsInSnapshotsEnabled())); |
| 147 | + UserDataWriter userDataWriter = |
| 148 | + new UserDataWriter( |
| 149 | + firestore, |
| 150 | + firestore.getFirestoreSettings().areTimestampsInSnapshotsEnabled(), |
| 151 | + serverTimestampBehavior); |
| 152 | + return doc == null ? null : userDataWriter.convertObject(doc.getData()); |
176 | 153 | }
|
177 | 154 |
|
178 | 155 | /**
|
@@ -285,9 +262,8 @@ public Object get(
|
285 | 262 | serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
|
286 | 263 | return getInternal(
|
287 | 264 | fieldPath.getInternalPath(),
|
288 |
| - new FieldValueOptions( |
289 |
| - serverTimestampBehavior, |
290 |
| - firestore.getFirestoreSettings().areTimestampsInSnapshotsEnabled())); |
| 265 | + serverTimestampBehavior, |
| 266 | + firestore.getFirestoreSettings().areTimestampsInSnapshotsEnabled()); |
291 | 267 | }
|
292 | 268 |
|
293 | 269 | /**
|
@@ -438,8 +414,8 @@ public Date getDate(
|
438 | 414 | Object maybeDate =
|
439 | 415 | getInternal(
|
440 | 416 | FieldPath.fromDotSeparatedPath(field).getInternalPath(),
|
441 |
| - new FieldValueOptions( |
442 |
| - serverTimestampBehavior, /*timestampsInSnapshotsEnabled=*/ false)); |
| 417 | + serverTimestampBehavior, |
| 418 | + /* timestampsInSnapshots= */ false); |
443 | 419 | return castTypedValue(maybeDate, field, Date.class);
|
444 | 420 | }
|
445 | 421 |
|
@@ -479,7 +455,8 @@ public Timestamp getTimestamp(
|
479 | 455 | Object maybeTimestamp =
|
480 | 456 | getInternal(
|
481 | 457 | FieldPath.fromDotSeparatedPath(field).getInternalPath(),
|
482 |
| - new FieldValueOptions(serverTimestampBehavior, /*timestampsInSnapshotsEnabled=*/ true)); |
| 458 | + serverTimestampBehavior, |
| 459 | + /* timestampsInSnapshots= */ true); |
483 | 460 | return castTypedValue(maybeTimestamp, field, Timestamp.class);
|
484 | 461 | }
|
485 | 462 |
|
@@ -546,87 +523,17 @@ private <T> T castTypedValue(Object value, String field, Class<T> clazz) {
|
546 | 523 | return clazz.cast(value);
|
547 | 524 | }
|
548 | 525 |
|
549 |
| - @Nullable |
550 |
| - private Object convertValue(FieldValue value, FieldValueOptions options) { |
551 |
| - if (value instanceof ObjectValue) { |
552 |
| - return convertObject((ObjectValue) value, options); |
553 |
| - } else if (value instanceof ArrayValue) { |
554 |
| - return convertArray((ArrayValue) value, options); |
555 |
| - } else if (value instanceof ReferenceValue) { |
556 |
| - return convertReference((ReferenceValue) value); |
557 |
| - } else if (value instanceof TimestampValue) { |
558 |
| - return convertTimestamp((TimestampValue) value, options); |
559 |
| - } else if (value instanceof ServerTimestampValue) { |
560 |
| - return convertServerTimestamp((ServerTimestampValue) value, options); |
561 |
| - } else { |
562 |
| - return value.value(); |
563 |
| - } |
564 |
| - } |
565 |
| - |
566 |
| - private Object convertServerTimestamp(ServerTimestampValue value, FieldValueOptions options) { |
567 |
| - switch (options.serverTimestampBehavior) { |
568 |
| - case PREVIOUS: |
569 |
| - return value.getPreviousValue(); |
570 |
| - case ESTIMATE: |
571 |
| - return value.getLocalWriteTime(); |
572 |
| - default: |
573 |
| - return value.value(); |
574 |
| - } |
575 |
| - } |
576 |
| - |
577 |
| - private Object convertTimestamp(TimestampValue value, FieldValueOptions options) { |
578 |
| - Timestamp timestamp = value.value(); |
579 |
| - if (options.timestampsInSnapshotsEnabled) { |
580 |
| - return timestamp; |
581 |
| - } else { |
582 |
| - return timestamp.toDate(); |
583 |
| - } |
584 |
| - } |
585 |
| - |
586 |
| - private Object convertReference(ReferenceValue value) { |
587 |
| - DocumentKey key = value.value(); |
588 |
| - DatabaseId refDatabase = value.getDatabaseId(); |
589 |
| - DatabaseId database = this.firestore.getDatabaseId(); |
590 |
| - if (!refDatabase.equals(database)) { |
591 |
| - // TODO: Somehow support foreign references. |
592 |
| - Logger.warn( |
593 |
| - "DocumentSnapshot", |
594 |
| - "Document %s contains a document reference within a different database " |
595 |
| - + "(%s/%s) which is not supported. It will be treated as a reference in " |
596 |
| - + "the current database (%s/%s) instead.", |
597 |
| - key.getPath(), |
598 |
| - refDatabase.getProjectId(), |
599 |
| - refDatabase.getDatabaseId(), |
600 |
| - database.getProjectId(), |
601 |
| - database.getDatabaseId()); |
602 |
| - } |
603 |
| - return new DocumentReference(key, firestore); |
604 |
| - } |
605 |
| - |
606 |
| - private Map<String, Object> convertObject(ObjectValue objectValue, FieldValueOptions options) { |
607 |
| - Map<String, Object> result = new HashMap<>(); |
608 |
| - for (Map.Entry<String, FieldValue> entry : objectValue.getInternalValue()) { |
609 |
| - result.put(entry.getKey(), convertValue(entry.getValue(), options)); |
610 |
| - } |
611 |
| - return result; |
612 |
| - } |
613 |
| - |
614 |
| - private List<Object> convertArray(ArrayValue arrayValue, FieldValueOptions options) { |
615 |
| - ArrayList<Object> result = new ArrayList<>(arrayValue.getInternalValue().size()); |
616 |
| - for (FieldValue v : arrayValue.getInternalValue()) { |
617 |
| - result.add(convertValue(v, options)); |
618 |
| - } |
619 |
| - return result; |
620 |
| - } |
621 |
| - |
622 | 526 | @Nullable
|
623 | 527 | private Object getInternal(
|
624 | 528 | @NonNull com.google.firebase.firestore.model.FieldPath fieldPath,
|
625 |
| - @NonNull FieldValueOptions options) { |
| 529 | + @NonNull ServerTimestampBehavior serverTimestampBehavior, |
| 530 | + boolean timestampsInSnapshots) { |
626 | 531 | if (doc != null) {
|
627 | 532 | FieldValue val = doc.getField(fieldPath);
|
628 | 533 | if (val != null) {
|
629 |
| - return convertValue(val, options); |
| 534 | + UserDataWriter userDataWriter = |
| 535 | + new UserDataWriter(firestore, timestampsInSnapshots, serverTimestampBehavior); |
| 536 | + return userDataWriter.convertValue(val); |
630 | 537 | }
|
631 | 538 | }
|
632 | 539 | return null;
|
|
0 commit comments