Skip to content

Commit 296928e

Browse files
feat: Ability to serialize Query to Proto
This PR adds an API to serialize and deserialize Queries to and from RunQueryRequests. The new API endpoints are and .
1 parent a0b7113 commit 296928e

File tree

4 files changed

+270
-65
lines changed

4 files changed

+270
-65
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FieldPath.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.auto.value.AutoValue;
2020
import com.google.common.base.Preconditions;
2121
import com.google.common.collect.ImmutableList;
22+
import com.google.firestore.v1.StructuredQuery;
2223
import java.util.regex.Pattern;
2324
import javax.annotation.Nonnull;
2425

@@ -30,11 +31,13 @@
3031
@AutoValue
3132
public abstract class FieldPath extends BasePath<FieldPath> implements Comparable<FieldPath> {
3233

34+
private static final String DOCUMENT_ID_SENTINEL = "__name__";
35+
3336
/**
3437
* A special sentinel FieldPath to refer to the ID of a document. It can be used in queries to
3538
* sort or filter by the document ID.
3639
*/
37-
static final FieldPath DOCUMENT_ID = FieldPath.of("__name__");
40+
static final FieldPath DOCUMENT_ID = FieldPath.of(DOCUMENT_ID_SENTINEL);
3841

3942
/** Regular expression to verify that dot-separated field paths do not contain ~*[]/. */
4043
private static final Pattern PROHIBITED_CHARACTERS = Pattern.compile(".*[~*/\\[\\]].*");
@@ -67,6 +70,11 @@ public static FieldPath documentId() {
6770
return DOCUMENT_ID;
6871
}
6972

73+
/** Verifies if the provided path matches the path that is used for the Document ID sentinel. */
74+
static boolean isDocumentId(String path) {
75+
return DOCUMENT_ID_SENTINEL.equals(path);
76+
}
77+
7078
/** Returns a field path from a dot separated string. Does not support escaping. */
7179
static FieldPath fromDotSeparatedString(String field) {
7280
if (PROHIBITED_CHARACTERS.matcher(field).matches()) {
@@ -152,4 +160,8 @@ FieldPath createPathWithSegments(ImmutableList<String> segments) {
152160
public String toString() {
153161
return getEncodedPath();
154162
}
163+
164+
StructuredQuery.FieldReference toProto() {
165+
return StructuredQuery.FieldReference.newBuilder().setFieldPath(getEncodedPath()).build();
166+
}
155167
}

0 commit comments

Comments
 (0)