Skip to content

Commit

Permalink
feat: Ability to serialize Query to Proto
Browse files Browse the repository at this point in the history
This PR adds an API to serialize and deserialize Queries to and from RunQueryRequests. The new API endpoints are  and .
  • Loading branch information
schmidt-sebastian committed Jun 6, 2020
1 parent a0b7113 commit 3c6e051
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 65 deletions.
Expand Up @@ -19,6 +19,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.firestore.v1.StructuredQuery;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;

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

private static final String DOCUMENT_ID_SENTINEL = "__name__";

/**
* A special sentinel FieldPath to refer to the ID of a document. It can be used in queries to
* sort or filter by the document ID.
*/
static final FieldPath DOCUMENT_ID = FieldPath.of("__name__");
static final FieldPath DOCUMENT_ID = FieldPath.of(DOCUMENT_ID_SENTINEL);

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

/** Verifies if the provided path matches the path that is used for the Document ID sentinel. */
static boolean isDocumentId(String path) {
return DOCUMENT_ID_SENTINEL.equals(path);
}

/** Returns a field path from a dot separated string. Does not support escaping. */
static FieldPath fromDotSeparatedString(String field) {
if (PROHIBITED_CHARACTERS.matcher(field).matches()) {
Expand Down Expand Up @@ -152,4 +160,8 @@ FieldPath createPathWithSegments(ImmutableList<String> segments) {
public String toString() {
return getEncodedPath();
}

StructuredQuery.FieldReference toProto() {
return StructuredQuery.FieldReference.newBuilder().setFieldPath(getEncodedPath()).build();
}
}

0 comments on commit 3c6e051

Please sign in to comment.