Skip to content

Commit 9d0653a

Browse files
feat: Support for Composite Filters. (#975)
* Support for Composite Filters. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Minor fixes. * Remove commented code. * Better names? * Update license format. * Prefer `equals` to `==` for enum comparison. * We must throw an error if Null or NaN is used in anything other than == or !=. * Use Nunnull, not NonNull. * Add tests. * Remove unrelated comment. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 3a60446 commit 9d0653a

File tree

5 files changed

+574
-182
lines changed

5 files changed

+574
-182
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ If you are using Maven without BOM, add this to your dependencies:
4949
If you are using Gradle 5.x or later, add this to your dependencies
5050

5151
```Groovy
52-
implementation platform('com.google.cloud:libraries-bom:25.4.0')
52+
implementation platform('com.google.cloud:libraries-bom:26.0.0')
5353
5454
implementation 'com.google.cloud:google-cloud-firestore'
5555
```
5656
If you are using Gradle without BOM, add this to your dependencies
5757

5858
```Groovy
59-
implementation 'com.google.cloud:google-cloud-firestore:3.2.0'
59+
implementation 'com.google.cloud:google-cloud-firestore:3.3.0'
6060
```
6161

6262
If you are using SBT, add this to your dependencies
6363

6464
```Scala
65-
libraryDependencies += "com.google.cloud" % "google-cloud-firestore" % "3.2.0"
65+
libraryDependencies += "com.google.cloud" % "google-cloud-firestore" % "3.3.0"
6666
```
6767

6868
## Authentication
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.StructuredQuery;
20+
import com.google.firestore.v1.StructuredQuery.FieldFilter.Operator;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import javax.annotation.Nonnull;
24+
import javax.annotation.Nullable;
25+
26+
/** @hide */
27+
public class Filter {
28+
static class UnaryFilter extends Filter {
29+
private final FieldPath field;
30+
private final Operator operator;
31+
private final Object value;
32+
33+
public UnaryFilter(FieldPath field, Operator operator, @Nullable Object value) {
34+
this.field = field;
35+
this.operator = operator;
36+
this.value = value;
37+
}
38+
39+
public FieldPath getField() {
40+
return field;
41+
}
42+
43+
public Operator getOperator() {
44+
return operator;
45+
}
46+
47+
@Nullable
48+
public Object getValue() {
49+
return value;
50+
}
51+
}
52+
53+
static class CompositeFilter extends Filter {
54+
private final List<Filter> filters;
55+
private final StructuredQuery.CompositeFilter.Operator operator;
56+
57+
public CompositeFilter(
58+
@Nonnull List<Filter> filters, StructuredQuery.CompositeFilter.Operator operator) {
59+
this.filters = filters;
60+
this.operator = operator;
61+
}
62+
63+
public List<Filter> getFilters() {
64+
return filters;
65+
}
66+
67+
public StructuredQuery.CompositeFilter.Operator getOperator() {
68+
return operator;
69+
}
70+
}
71+
72+
@Nonnull
73+
public static Filter equalTo(@Nonnull String field, @Nullable Object value) {
74+
return equalTo(FieldPath.fromDotSeparatedString(field), value);
75+
}
76+
77+
@Nonnull
78+
public static Filter equalTo(@Nonnull FieldPath fieldPath, @Nullable Object value) {
79+
return new UnaryFilter(fieldPath, Operator.EQUAL, value);
80+
}
81+
82+
@Nonnull
83+
public static Filter notEqualTo(@Nonnull String field, @Nullable Object value) {
84+
return notEqualTo(FieldPath.fromDotSeparatedString(field), value);
85+
}
86+
87+
@Nonnull
88+
public static Filter notEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value) {
89+
return new UnaryFilter(fieldPath, Operator.NOT_EQUAL, value);
90+
}
91+
92+
@Nonnull
93+
public static Filter greaterThan(@Nonnull String field, @Nullable Object value) {
94+
return greaterThan(FieldPath.fromDotSeparatedString(field), value);
95+
}
96+
97+
@Nonnull
98+
public static Filter greaterThan(@Nonnull FieldPath fieldPath, @Nullable Object value) {
99+
return new UnaryFilter(fieldPath, Operator.GREATER_THAN, value);
100+
}
101+
102+
@Nonnull
103+
public static Filter greaterThanOrEqualTo(@Nonnull String field, @Nullable Object value) {
104+
return greaterThanOrEqualTo(FieldPath.fromDotSeparatedString(field), value);
105+
}
106+
107+
@Nonnull
108+
public static Filter greaterThanOrEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value) {
109+
return new UnaryFilter(fieldPath, Operator.GREATER_THAN_OR_EQUAL, value);
110+
}
111+
112+
@Nonnull
113+
public static Filter lessThan(@Nonnull String field, @Nullable Object value) {
114+
return lessThan(FieldPath.fromDotSeparatedString(field), value);
115+
}
116+
117+
@Nonnull
118+
public static Filter lessThan(@Nonnull FieldPath fieldPath, @Nullable Object value) {
119+
return new UnaryFilter(fieldPath, Operator.LESS_THAN, value);
120+
}
121+
122+
@Nonnull
123+
public static Filter lessThanOrEqualTo(@Nonnull String field, @Nullable Object value) {
124+
return lessThanOrEqualTo(FieldPath.fromDotSeparatedString(field), value);
125+
}
126+
127+
@Nonnull
128+
public static Filter lessThanOrEqualTo(@Nonnull FieldPath fieldPath, @Nullable Object value) {
129+
return new UnaryFilter(fieldPath, Operator.LESS_THAN_OR_EQUAL, value);
130+
}
131+
132+
@Nonnull
133+
public static Filter arrayContains(@Nonnull String field, @Nullable Object value) {
134+
return arrayContains(FieldPath.fromDotSeparatedString(field), value);
135+
}
136+
137+
@Nonnull
138+
public static Filter arrayContains(@Nonnull FieldPath fieldPath, @Nullable Object value) {
139+
return new UnaryFilter(fieldPath, Operator.ARRAY_CONTAINS, value);
140+
}
141+
142+
@Nonnull
143+
public static Filter arrayContainsAny(@Nonnull String field, @Nullable Object value) {
144+
return arrayContainsAny(FieldPath.fromDotSeparatedString(field), value);
145+
}
146+
147+
@Nonnull
148+
public static Filter arrayContainsAny(@Nonnull FieldPath fieldPath, @Nullable Object value) {
149+
return new UnaryFilter(fieldPath, Operator.ARRAY_CONTAINS_ANY, value);
150+
}
151+
152+
@Nonnull
153+
public static Filter inArray(@Nonnull String field, @Nullable Object value) {
154+
return inArray(FieldPath.fromDotSeparatedString(field), value);
155+
}
156+
157+
@Nonnull
158+
public static Filter inArray(@Nonnull FieldPath fieldPath, @Nullable Object value) {
159+
return new UnaryFilter(fieldPath, Operator.IN, value);
160+
}
161+
162+
@Nonnull
163+
public static Filter notInArray(@Nonnull String field, @Nullable Object value) {
164+
return notInArray(FieldPath.fromDotSeparatedString(field), value);
165+
}
166+
167+
@Nonnull
168+
public static Filter notInArray(@Nonnull FieldPath fieldPath, @Nullable Object value) {
169+
return new UnaryFilter(fieldPath, Operator.NOT_IN, value);
170+
}
171+
172+
@Nonnull
173+
public static Filter or(Filter... filters) {
174+
// TODO(orquery): Change this to Operator.OR once it is available.
175+
return new CompositeFilter(
176+
Arrays.asList(filters), StructuredQuery.CompositeFilter.Operator.OPERATOR_UNSPECIFIED);
177+
}
178+
179+
@Nonnull
180+
public static Filter and(Filter... filters) {
181+
return new CompositeFilter(
182+
Arrays.asList(filters), StructuredQuery.CompositeFilter.Operator.AND);
183+
}
184+
}

0 commit comments

Comments
 (0)