@Test public void testConvertsTargetChangeWithNoChange() { WatchTargetChange expected = new WatchTargetChange(WatchTargetChangeType.NoChange, asList(1, 4)); WatchTargetChange actual = (WatchTargetChange) serializer.decodeWatchChange( ListenResponse.newBuilder() .setTargetChange( TargetChange.newBuilder() .setTargetChangeType(TargetChangeType.NO_CHANGE) .addTargetIds(1) .addTargetIds(4)) .build()); assertEquals(expected, actual); }
public WatchChange decodeWatchChange(ListenResponse protoChange) { WatchChange watchChange; switch (protoChange.getResponseTypeCase()) { case TARGET_CHANGE: com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange(); WatchTargetChangeType changeType; Status cause = null; switch (targetChange.getTargetChangeType()) { case NO_CHANGE: changeType = WatchTargetChangeType.NoChange; break; case ADD: changeType = WatchTargetChangeType.Added; break; case REMOVE: changeType = WatchTargetChangeType.Removed; cause = fromStatus(targetChange.getCause()); break; case CURRENT: changeType = WatchTargetChangeType.Current; break; case RESET: changeType = WatchTargetChangeType.Reset; break; case UNRECOGNIZED: default: throw new IllegalArgumentException("Unknown target change type"); } watchChange = new WatchTargetChange( changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause); break; case DOCUMENT_CHANGE: DocumentChange docChange = protoChange.getDocumentChange(); List<Integer> added = docChange.getTargetIdsList(); List<Integer> removed = docChange.getRemovedTargetIdsList(); DocumentKey key = decodeKey(docChange.getDocument().getName()); SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime()); hardAssert( !version.equals(SnapshotVersion.NONE), "Got a document change without an update time"); ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap()); Document document = new Document(key, version, data, Document.DocumentState.SYNCED); watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document); break; case DOCUMENT_DELETE: DocumentDelete docDelete = protoChange.getDocumentDelete(); removed = docDelete.getRemovedTargetIdsList(); key = decodeKey(docDelete.getDocument()); version = decodeVersion(docDelete.getReadTime()); NoDocument doc = new NoDocument(key, version, false); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc); break; case DOCUMENT_REMOVE: DocumentRemove docRemove = protoChange.getDocumentRemove(); removed = docRemove.getRemovedTargetIdsList(); key = decodeKey(docRemove.getDocument()); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null); break; case FILTER: com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter(); ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount()); int targetId = protoFilter.getTargetId(); watchChange = new ExistenceFilterWatchChange(targetId, filter); break; case RESPONSETYPE_NOT_SET: default: throw new IllegalArgumentException("Unknown change type set"); } return watchChange; }
RemoteSerializer { public WatchChange decodeWatchChange(ListenResponse protoChange) { WatchChange watchChange; switch (protoChange.getResponseTypeCase()) { case TARGET_CHANGE: com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange(); WatchTargetChangeType changeType; Status cause = null; switch (targetChange.getTargetChangeType()) { case NO_CHANGE: changeType = WatchTargetChangeType.NoChange; break; case ADD: changeType = WatchTargetChangeType.Added; break; case REMOVE: changeType = WatchTargetChangeType.Removed; cause = fromStatus(targetChange.getCause()); break; case CURRENT: changeType = WatchTargetChangeType.Current; break; case RESET: changeType = WatchTargetChangeType.Reset; break; case UNRECOGNIZED: default: throw new IllegalArgumentException("Unknown target change type"); } watchChange = new WatchTargetChange( changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause); break; case DOCUMENT_CHANGE: DocumentChange docChange = protoChange.getDocumentChange(); List<Integer> added = docChange.getTargetIdsList(); List<Integer> removed = docChange.getRemovedTargetIdsList(); DocumentKey key = decodeKey(docChange.getDocument().getName()); SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime()); hardAssert( !version.equals(SnapshotVersion.NONE), "Got a document change without an update time"); ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap()); Document document = new Document(key, version, data, Document.DocumentState.SYNCED); watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document); break; case DOCUMENT_DELETE: DocumentDelete docDelete = protoChange.getDocumentDelete(); removed = docDelete.getRemovedTargetIdsList(); key = decodeKey(docDelete.getDocument()); version = decodeVersion(docDelete.getReadTime()); NoDocument doc = new NoDocument(key, version, false); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc); break; case DOCUMENT_REMOVE: DocumentRemove docRemove = protoChange.getDocumentRemove(); removed = docRemove.getRemovedTargetIdsList(); key = decodeKey(docRemove.getDocument()); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null); break; case FILTER: com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter(); ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount()); int targetId = protoFilter.getTargetId(); watchChange = new ExistenceFilterWatchChange(targetId, filter); break; case RESPONSETYPE_NOT_SET: default: throw new IllegalArgumentException("Unknown change type set"); } return watchChange; } }
RemoteSerializer { public WatchChange decodeWatchChange(ListenResponse protoChange) { WatchChange watchChange; switch (protoChange.getResponseTypeCase()) { case TARGET_CHANGE: com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange(); WatchTargetChangeType changeType; Status cause = null; switch (targetChange.getTargetChangeType()) { case NO_CHANGE: changeType = WatchTargetChangeType.NoChange; break; case ADD: changeType = WatchTargetChangeType.Added; break; case REMOVE: changeType = WatchTargetChangeType.Removed; cause = fromStatus(targetChange.getCause()); break; case CURRENT: changeType = WatchTargetChangeType.Current; break; case RESET: changeType = WatchTargetChangeType.Reset; break; case UNRECOGNIZED: default: throw new IllegalArgumentException("Unknown target change type"); } watchChange = new WatchTargetChange( changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause); break; case DOCUMENT_CHANGE: DocumentChange docChange = protoChange.getDocumentChange(); List<Integer> added = docChange.getTargetIdsList(); List<Integer> removed = docChange.getRemovedTargetIdsList(); DocumentKey key = decodeKey(docChange.getDocument().getName()); SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime()); hardAssert( !version.equals(SnapshotVersion.NONE), "Got a document change without an update time"); ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap()); Document document = new Document(key, version, data, Document.DocumentState.SYNCED); watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document); break; case DOCUMENT_DELETE: DocumentDelete docDelete = protoChange.getDocumentDelete(); removed = docDelete.getRemovedTargetIdsList(); key = decodeKey(docDelete.getDocument()); version = decodeVersion(docDelete.getReadTime()); NoDocument doc = new NoDocument(key, version, false); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc); break; case DOCUMENT_REMOVE: DocumentRemove docRemove = protoChange.getDocumentRemove(); removed = docRemove.getRemovedTargetIdsList(); key = decodeKey(docRemove.getDocument()); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null); break; case FILTER: com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter(); ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount()); int targetId = protoFilter.getTargetId(); watchChange = new ExistenceFilterWatchChange(targetId, filter); break; case RESPONSETYPE_NOT_SET: default: throw new IllegalArgumentException("Unknown change type set"); } return watchChange; } RemoteSerializer(DatabaseId databaseId); }
RemoteSerializer { public WatchChange decodeWatchChange(ListenResponse protoChange) { WatchChange watchChange; switch (protoChange.getResponseTypeCase()) { case TARGET_CHANGE: com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange(); WatchTargetChangeType changeType; Status cause = null; switch (targetChange.getTargetChangeType()) { case NO_CHANGE: changeType = WatchTargetChangeType.NoChange; break; case ADD: changeType = WatchTargetChangeType.Added; break; case REMOVE: changeType = WatchTargetChangeType.Removed; cause = fromStatus(targetChange.getCause()); break; case CURRENT: changeType = WatchTargetChangeType.Current; break; case RESET: changeType = WatchTargetChangeType.Reset; break; case UNRECOGNIZED: default: throw new IllegalArgumentException("Unknown target change type"); } watchChange = new WatchTargetChange( changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause); break; case DOCUMENT_CHANGE: DocumentChange docChange = protoChange.getDocumentChange(); List<Integer> added = docChange.getTargetIdsList(); List<Integer> removed = docChange.getRemovedTargetIdsList(); DocumentKey key = decodeKey(docChange.getDocument().getName()); SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime()); hardAssert( !version.equals(SnapshotVersion.NONE), "Got a document change without an update time"); ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap()); Document document = new Document(key, version, data, Document.DocumentState.SYNCED); watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document); break; case DOCUMENT_DELETE: DocumentDelete docDelete = protoChange.getDocumentDelete(); removed = docDelete.getRemovedTargetIdsList(); key = decodeKey(docDelete.getDocument()); version = decodeVersion(docDelete.getReadTime()); NoDocument doc = new NoDocument(key, version, false); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc); break; case DOCUMENT_REMOVE: DocumentRemove docRemove = protoChange.getDocumentRemove(); removed = docRemove.getRemovedTargetIdsList(); key = decodeKey(docRemove.getDocument()); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null); break; case FILTER: com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter(); ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount()); int targetId = protoFilter.getTargetId(); watchChange = new ExistenceFilterWatchChange(targetId, filter); break; case RESPONSETYPE_NOT_SET: default: throw new IllegalArgumentException("Unknown change type set"); } return watchChange; } RemoteSerializer(DatabaseId databaseId); com.google.protobuf.Timestamp encodeTimestamp(Timestamp timestamp); Timestamp decodeTimestamp(com.google.protobuf.Timestamp proto); com.google.protobuf.Timestamp encodeVersion(SnapshotVersion version); SnapshotVersion decodeVersion(com.google.protobuf.Timestamp proto); String encodeKey(DocumentKey key); DocumentKey decodeKey(String name); String databaseName(); com.google.firestore.v1.Document encodeDocument(DocumentKey key, ObjectValue value); MaybeDocument decodeMaybeDocument(BatchGetDocumentsResponse response); com.google.firestore.v1.Write encodeMutation(Mutation mutation); Mutation decodeMutation(com.google.firestore.v1.Write mutation); MutationResult decodeMutationResult(
      com.google.firestore.v1.WriteResult proto, SnapshotVersion commitVersion); @Nullable Map<String, String> encodeListenRequestLabels(TargetData targetData); Target encodeTarget(TargetData targetData); DocumentsTarget encodeDocumentsTarget(com.google.firebase.firestore.core.Target target); com.google.firebase.firestore.core.Target decodeDocumentsTarget(DocumentsTarget target); QueryTarget encodeQueryTarget(com.google.firebase.firestore.core.Target target); com.google.firebase.firestore.core.Target decodeQueryTarget(QueryTarget target); WatchChange decodeWatchChange(ListenResponse protoChange); SnapshotVersion decodeVersionFromListenResponse(ListenResponse watchChange); }
RemoteSerializer { public WatchChange decodeWatchChange(ListenResponse protoChange) { WatchChange watchChange; switch (protoChange.getResponseTypeCase()) { case TARGET_CHANGE: com.google.firestore.v1.TargetChange targetChange = protoChange.getTargetChange(); WatchTargetChangeType changeType; Status cause = null; switch (targetChange.getTargetChangeType()) { case NO_CHANGE: changeType = WatchTargetChangeType.NoChange; break; case ADD: changeType = WatchTargetChangeType.Added; break; case REMOVE: changeType = WatchTargetChangeType.Removed; cause = fromStatus(targetChange.getCause()); break; case CURRENT: changeType = WatchTargetChangeType.Current; break; case RESET: changeType = WatchTargetChangeType.Reset; break; case UNRECOGNIZED: default: throw new IllegalArgumentException("Unknown target change type"); } watchChange = new WatchTargetChange( changeType, targetChange.getTargetIdsList(), targetChange.getResumeToken(), cause); break; case DOCUMENT_CHANGE: DocumentChange docChange = protoChange.getDocumentChange(); List<Integer> added = docChange.getTargetIdsList(); List<Integer> removed = docChange.getRemovedTargetIdsList(); DocumentKey key = decodeKey(docChange.getDocument().getName()); SnapshotVersion version = decodeVersion(docChange.getDocument().getUpdateTime()); hardAssert( !version.equals(SnapshotVersion.NONE), "Got a document change without an update time"); ObjectValue data = ObjectValue.fromMap(docChange.getDocument().getFieldsMap()); Document document = new Document(key, version, data, Document.DocumentState.SYNCED); watchChange = new WatchChange.DocumentChange(added, removed, document.getKey(), document); break; case DOCUMENT_DELETE: DocumentDelete docDelete = protoChange.getDocumentDelete(); removed = docDelete.getRemovedTargetIdsList(); key = decodeKey(docDelete.getDocument()); version = decodeVersion(docDelete.getReadTime()); NoDocument doc = new NoDocument(key, version, false); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, doc.getKey(), doc); break; case DOCUMENT_REMOVE: DocumentRemove docRemove = protoChange.getDocumentRemove(); removed = docRemove.getRemovedTargetIdsList(); key = decodeKey(docRemove.getDocument()); watchChange = new WatchChange.DocumentChange(Collections.emptyList(), removed, key, null); break; case FILTER: com.google.firestore.v1.ExistenceFilter protoFilter = protoChange.getFilter(); ExistenceFilter filter = new ExistenceFilter(protoFilter.getCount()); int targetId = protoFilter.getTargetId(); watchChange = new ExistenceFilterWatchChange(targetId, filter); break; case RESPONSETYPE_NOT_SET: default: throw new IllegalArgumentException("Unknown change type set"); } return watchChange; } RemoteSerializer(DatabaseId databaseId); com.google.protobuf.Timestamp encodeTimestamp(Timestamp timestamp); Timestamp decodeTimestamp(com.google.protobuf.Timestamp proto); com.google.protobuf.Timestamp encodeVersion(SnapshotVersion version); SnapshotVersion decodeVersion(com.google.protobuf.Timestamp proto); String encodeKey(DocumentKey key); DocumentKey decodeKey(String name); String databaseName(); com.google.firestore.v1.Document encodeDocument(DocumentKey key, ObjectValue value); MaybeDocument decodeMaybeDocument(BatchGetDocumentsResponse response); com.google.firestore.v1.Write encodeMutation(Mutation mutation); Mutation decodeMutation(com.google.firestore.v1.Write mutation); MutationResult decodeMutationResult(
      com.google.firestore.v1.WriteResult proto, SnapshotVersion commitVersion); @Nullable Map<String, String> encodeListenRequestLabels(TargetData targetData); Target encodeTarget(TargetData targetData); DocumentsTarget encodeDocumentsTarget(com.google.firebase.firestore.core.Target target); com.google.firebase.firestore.core.Target decodeDocumentsTarget(DocumentsTarget target); QueryTarget encodeQueryTarget(com.google.firebase.firestore.core.Target target); com.google.firebase.firestore.core.Target decodeQueryTarget(QueryTarget target); WatchChange decodeWatchChange(ListenResponse protoChange); SnapshotVersion decodeVersionFromListenResponse(ListenResponse watchChange);  }