Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Firestore document metadata in responses #3063

Merged
merged 1 commit into from Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,14 +1,14 @@
package com.external.plugins;

import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError;
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException;
import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.ActionExecutionResult;
import com.appsmith.external.models.DBAuth;
import com.appsmith.external.models.DatasourceConfiguration;
import com.appsmith.external.models.DatasourceStructure;
import com.appsmith.external.models.DatasourceTestResult;
import com.appsmith.external.models.Property;
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError;
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException;
import com.appsmith.external.plugins.BasePlugin;
import com.appsmith.external.plugins.PluginExecutor;
import com.google.api.core.ApiFuture;
Expand All @@ -18,7 +18,6 @@
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.WriteResult;
import com.google.firebase.FirebaseApp;
Expand Down Expand Up @@ -370,8 +369,10 @@ private Object resultToMap(Object objResult, boolean isRoot) throws AppsmithPlug
return resultMap;

} else if (objResult instanceof DocumentSnapshot) {
// Individual document.
DocumentSnapshot documentSnapshot = (DocumentSnapshot) objResult;
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("$ref", resultToMap(documentSnapshot.getReference()));
if (documentSnapshot.getData() != null) {
for (final Map.Entry<String, Object> entry : documentSnapshot.getData().entrySet()) {
resultMap.put(entry.getKey(), resultToMap(entry.getValue(), false));
Expand All @@ -380,23 +381,16 @@ private Object resultToMap(Object objResult, boolean isRoot) throws AppsmithPlug
return resultMap;

} else if (objResult instanceof QuerySnapshot) {
QuerySnapshot querySnapshot = (QuerySnapshot) objResult;
List<Map<String, Object>> documents = new ArrayList<>();
for (QueryDocumentSnapshot documentSnapshot : querySnapshot.getDocuments()) {
final Map<String, Object> data = documentSnapshot.getData();
for (final Map.Entry<String, Object> entry : data.entrySet()) {
data.put(entry.getKey(), resultToMap(entry.getValue(), false));
}
documents.add(data);
}
return documents;
// Result of a GET_COLLECTION operation.
return resultToMap(((QuerySnapshot) objResult).getDocuments());

} else if (objResult instanceof DocumentReference) {
// A reference containing details of another document.
DocumentReference documentReference = (DocumentReference) objResult;
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("id", documentReference.getId());
resultMap.put("path", documentReference.getPath());
return resultMap;
return Map.of(
"id", documentReference.getId(),
"path", documentReference.getPath()
);

} else if (objResult instanceof Map) {
Map<String, Object> resultMap = (Map) objResult;
Expand Down
Expand Up @@ -112,6 +112,7 @@ public void testGetSingleDocument() {
assertEquals("one", first.remove("name"));
assertFalse((Boolean) first.remove("isPlural"));
assertEquals(1L, first.remove("value"));
assertEquals(Map.of("id", "one", "path", "initial/one"), first.remove("$ref"));
assertEquals(Collections.emptyMap(), first);
})
.verifyComplete();
Expand All @@ -138,6 +139,7 @@ public void testGetSingleDocument2() {
assertNotNull(doc.remove("dt"));
assertEquals("abc def", ((Blob) doc.remove("bytes")).toByteString().toStringUtf8());
assertNull(doc.remove("null-ref"));
assertEquals(Map.of("id", "two", "path", "initial/two"), doc.remove("$ref"));
assertEquals(Collections.emptyMap(), doc);
})
.verifyComplete();
Expand Down Expand Up @@ -166,6 +168,7 @@ public void testGetSingleDocument3() {
Map.of("path", "initial/one", "id", "one"),
Map.of("path", "initial/two", "id", "two")
), doc.remove("ref-list"));
assertEquals(Map.of("id", "inner-ref", "path", "initial/inner-ref"), doc.remove("$ref"));
assertEquals(Collections.emptyMap(), doc);
})
.verifyComplete();
Expand All @@ -192,6 +195,7 @@ public void testGetDocumentsInCollection() {
assertEquals("one", first.remove("name"));
assertFalse((Boolean) first.remove("isPlural"));
assertEquals(1L, first.remove("value"));
assertEquals(Map.of("id", "one", "path", "initial/one"), first.remove("$ref"));
assertEquals(Collections.emptyMap(), first);

final Map<String, Object> second = results.stream().filter(d -> "two".equals(d.get("name"))).findFirst().orElse(null);
Expand All @@ -204,6 +208,7 @@ public void testGetDocumentsInCollection() {
assertNotNull(second.remove("dt"));
assertEquals("abc def", ((Blob) second.remove("bytes")).toByteString().toStringUtf8());
assertNull(second.remove("null-ref"));
assertEquals(Map.of("id", "two", "path", "initial/two"), second.remove("$ref"));
assertEquals(Collections.emptyMap(), second);

final Map<String, Object> third = results.stream().filter(d -> "third".equals(d.get("name"))).findFirst().orElse(null);
Expand All @@ -218,6 +223,7 @@ public void testGetDocumentsInCollection() {
Map.of("path", "initial/one", "id", "one"),
Map.of("path", "initial/two", "id", "two")
), third.remove("ref-list"));
assertEquals(Map.of("id", "inner-ref", "path", "initial/inner-ref"), third.remove("$ref"));
assertEquals(Collections.emptyMap(), third);
})
.verifyComplete();
Expand Down