Skip to content

Commit

Permalink
WIP: Query serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jun 5, 2020
1 parent a0b7113 commit 1660d20
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 59 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,12 @@
@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 +69,10 @@ public static FieldPath documentId() {
return DOCUMENT_ID;
}

static boolean isDocumentId(StructuredQuery.FieldReference fieldReference) {
return DOCUMENT_ID_SENTINEL.equals(fieldReference.getFieldPath());
}

/** 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 +158,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 1660d20

Please sign in to comment.