Skip to content

Commit bdcca71

Browse files
authored
Pipeline Expressions Refactor (#1946)
* Refactor * Cleanup
1 parent ca51034 commit bdcca71

39 files changed

+212
-183
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
/** Converts user input into the Firestore Value representation. */
4343
class UserDataConverter {
44+
45+
static final Value NULL_VALUE = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
4446
private static final Logger LOGGER = Logger.getLogger(UserDataConverter.class.getName());
4547

4648
/** Controls the behavior for field deletes. */
@@ -120,8 +122,9 @@ static Value encodeValue(
120122
+ " as an argument at field '%s'.",
121123
path);
122124
return null;
125+
123126
} else if (sanitizedObject == null) {
124-
return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
127+
return NULL_VALUE;
125128
} else if (sanitizedObject instanceof String) {
126129
return Value.newBuilder().setStringValue((String) sanitizedObject).build();
127130
} else if (sanitizedObject instanceof Integer) {

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Accumulator.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
package com.google.cloud.firestore.pipeline.expressions;
1818

1919
import com.google.api.core.BetaApi;
20+
import com.google.common.collect.ImmutableList;
2021

2122
@BetaApi
22-
public interface Accumulator extends Expr {
23+
public abstract class Accumulator extends Function {
24+
25+
protected Accumulator(String name, ImmutableList<? extends Expr> params) {
26+
super(name, params);
27+
}
28+
2329
@BetaApi
2430
@Override
25-
default ExprWithAlias<Accumulator> as(String fieldName) {
31+
public ExprWithAlias<Accumulator> as(String fieldName) {
2632
return new ExprWithAlias<>(this, fieldName);
2733
}
2834
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/And.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
@BetaApi
25-
public final class And extends Function implements FilterCondition {
25+
public final class And extends FilterCondition {
2626
@InternalApi
2727
And(List<FilterCondition> conditions) {
2828
super("and", ImmutableList.copyOf(conditions));

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/ArrayContains.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.common.collect.ImmutableList;
2222

2323
@BetaApi
24-
public final class ArrayContains extends Function implements FilterCondition {
24+
public final class ArrayContains extends FilterCondition {
2525
@InternalApi
2626
ArrayContains(Expr array, Expr element) {
2727
super(

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/ArrayContainsAll.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
@BetaApi
25-
public final class ArrayContainsAll extends Function implements FilterCondition {
25+
public final class ArrayContainsAll extends FilterCondition {
2626
@InternalApi
2727
ArrayContainsAll(Expr array, List<Expr> elements) {
2828
super("array_contains_all", ImmutableList.of(array, new ListOfExprs(elements)));

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/ArrayContainsAny.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
@BetaApi
25-
public final class ArrayContainsAny extends Function implements FilterCondition {
25+
public final class ArrayContainsAny extends FilterCondition {
2626
@InternalApi
2727
ArrayContainsAny(Expr array, List<Expr> elements) {
2828
super("array_contains_any", ImmutableList.of(array, new ListOfExprs(elements)));

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Avg.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.common.collect.ImmutableList;
2222

2323
@BetaApi
24-
public final class Avg extends Function implements Accumulator {
24+
public final class Avg extends Accumulator {
2525
@InternalApi
2626
Avg(Expr value, boolean distinct) {
2727
super("avg", ImmutableList.of(value));

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Constant.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
import com.google.cloud.firestore.DocumentReference;
2626
import com.google.cloud.firestore.FieldValue;
2727
import com.google.cloud.firestore.GeoPoint;
28+
import com.google.common.collect.ImmutableMap;
2829
import com.google.firestore.v1.Value;
2930
import java.util.Arrays;
3031
import java.util.Date;
3132
import java.util.Map;
3233

3334
@BetaApi
34-
public final class Constant implements Expr {
35+
public final class Constant extends Expr {
36+
37+
static final Constant NULL = new Constant(null);
38+
3539
private final Object value;
3640

3741
Constant(Object value) {
@@ -85,16 +89,14 @@ public static Constant of(Value value) {
8589

8690
@InternalApi
8791
public static Constant nullValue() {
88-
return new Constant(null);
92+
return NULL;
8993
}
9094

9195
@BetaApi
9296
static Constant of(Object value) {
9397
if (value == null) {
94-
return new Constant(null);
95-
}
96-
97-
if (value instanceof String) {
98+
return NULL;
99+
} else if (value instanceof String) {
98100
return of((String) value);
99101
} else if (value instanceof Number) {
100102
return of((Number) value);
@@ -112,23 +114,25 @@ static Constant of(Object value) {
112114
return of((DocumentReference) value);
113115
} else if (value instanceof Value) {
114116
return of((Value) value);
117+
} else if (value instanceof Constant) {
118+
return (Constant) value;
115119
} else {
116120
throw new IllegalArgumentException("Unknown type: " + value);
117121
}
118122
}
119123

120124
@BetaApi
121-
public static <T> Constant of(Iterable<T> value) {
125+
public static Constant of(Iterable<?> value) {
122126
return new Constant(value);
123127
}
124128

125129
@BetaApi
126-
public static <T> Constant of(T[] value) {
130+
public static Constant of(Object[] value) {
127131
return new Constant(Arrays.asList(value.clone())); // Convert array to list
128132
}
129133

130134
@BetaApi
131-
public static <T> Constant of(Map<String, T> value) {
135+
public static Constant of(Map<String, ?> value) {
132136
return new Constant(value);
133137
}
134138

@@ -137,8 +141,8 @@ public static Constant vector(double[] value) {
137141
return new Constant(FieldValue.vector(value));
138142
}
139143

140-
@InternalApi
141-
public Value toProto() {
144+
@Override
145+
Value toProto() {
142146
return encodeValue(value);
143147
}
144148
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Count.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import javax.annotation.Nonnull;
2323

2424
@BetaApi
25-
public final class Count extends Function implements Accumulator {
25+
public final class Count extends Accumulator {
2626
@InternalApi
2727
Count(@Nonnull Expr value) {
2828
super("count", ImmutableList.of(value));

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/CountIf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.common.collect.ImmutableList;
2222

2323
@BetaApi
24-
public final class CountIf extends Function implements Accumulator {
24+
public final class CountIf extends Accumulator {
2525
@InternalApi
2626
CountIf(Expr value, boolean distinct) {
2727
super("countif", (value != null) ? ImmutableList.of(value) : ImmutableList.of());

0 commit comments

Comments
 (0)