|
19 | 19 | import com.google.auto.value.AutoValue; |
20 | 20 | import com.google.common.base.Preconditions; |
21 | 21 | import com.google.common.collect.ImmutableList; |
| 22 | +import com.google.firestore.v1.StructuredQuery; |
22 | 23 | import java.util.regex.Pattern; |
23 | 24 | import javax.annotation.Nonnull; |
24 | 25 |
|
|
30 | 31 | @AutoValue |
31 | 32 | public abstract class FieldPath extends BasePath<FieldPath> implements Comparable<FieldPath> { |
32 | 33 |
|
| 34 | + private static final String DOCUMENT_ID_SENTINEL = "__name__"; |
| 35 | + |
33 | 36 | /** |
34 | 37 | * A special sentinel FieldPath to refer to the ID of a document. It can be used in queries to |
35 | 38 | * sort or filter by the document ID. |
36 | 39 | */ |
37 | | - static final FieldPath DOCUMENT_ID = FieldPath.of("__name__"); |
| 40 | + static final FieldPath DOCUMENT_ID = FieldPath.of(DOCUMENT_ID_SENTINEL); |
38 | 41 |
|
39 | 42 | /** Regular expression to verify that dot-separated field paths do not contain ~*[]/. */ |
40 | 43 | private static final Pattern PROHIBITED_CHARACTERS = Pattern.compile(".*[~*/\\[\\]].*"); |
@@ -67,6 +70,11 @@ public static FieldPath documentId() { |
67 | 70 | return DOCUMENT_ID; |
68 | 71 | } |
69 | 72 |
|
| 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 | + |
70 | 78 | /** Returns a field path from a dot separated string. Does not support escaping. */ |
71 | 79 | static FieldPath fromDotSeparatedString(String field) { |
72 | 80 | if (PROHIBITED_CHARACTERS.matcher(field).matches()) { |
@@ -152,4 +160,8 @@ FieldPath createPathWithSegments(ImmutableList<String> segments) { |
152 | 160 | public String toString() { |
153 | 161 | return getEncodedPath(); |
154 | 162 | } |
| 163 | + |
| 164 | + StructuredQuery.FieldReference toProto() { |
| 165 | + return StructuredQuery.FieldReference.newBuilder().setFieldPath(getEncodedPath()).build(); |
| 166 | + } |
155 | 167 | } |
0 commit comments