Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline Expressions Refactor #1946

Open
wants to merge 2 commits into
base: wuandy/JavaPplPP
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

/** Converts user input into the Firestore Value representation. */
class UserDataConverter {

static final Value NULL_VALUE = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
private static final Logger LOGGER = Logger.getLogger(UserDataConverter.class.getName());

/** Controls the behavior for field deletes. */
Expand Down Expand Up @@ -120,8 +122,9 @@ static Value encodeValue(
+ " as an argument at field '%s'.",
path);
return null;

} else if (sanitizedObject == null) {
return Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
return NULL_VALUE;
} else if (sanitizedObject instanceof String) {
return Value.newBuilder().setStringValue((String) sanitizedObject).build();
} else if (sanitizedObject instanceof Integer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
package com.google.cloud.firestore.pipeline.expressions;

import com.google.api.core.BetaApi;
import com.google.common.collect.ImmutableList;

@BetaApi
public interface Accumulator extends Expr {
public abstract class Accumulator extends Function {

protected Accumulator(String name, ImmutableList<? extends Expr> params) {
super(name, params);
}

@BetaApi
@Override
default ExprWithAlias<Accumulator> as(String fieldName) {
public ExprWithAlias<Accumulator> as(String fieldName) {
return new ExprWithAlias<>(this, fieldName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

@BetaApi
public final class And extends Function implements FilterCondition {
public final class And extends FilterCondition {
@InternalApi
And(List<FilterCondition> conditions) {
super("and", ImmutableList.copyOf(conditions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.collect.ImmutableList;

@BetaApi
public final class ArrayContains extends Function implements FilterCondition {
public final class ArrayContains extends FilterCondition {
@InternalApi
ArrayContains(Expr array, Expr element) {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

@BetaApi
public final class ArrayContainsAll extends Function implements FilterCondition {
public final class ArrayContainsAll extends FilterCondition {
@InternalApi
ArrayContainsAll(Expr array, List<Expr> elements) {
super("array_contains_all", ImmutableList.of(array, new ListOfExprs(elements)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

@BetaApi
public final class ArrayContainsAny extends Function implements FilterCondition {
public final class ArrayContainsAny extends FilterCondition {
@InternalApi
ArrayContainsAny(Expr array, List<Expr> elements) {
super("array_contains_any", ImmutableList.of(array, new ListOfExprs(elements)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.collect.ImmutableList;

@BetaApi
public final class Avg extends Function implements Accumulator {
public final class Avg extends Accumulator {
@InternalApi
Avg(Expr value, boolean distinct) {
super("avg", ImmutableList.of(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.FieldValue;
import com.google.cloud.firestore.GeoPoint;
import com.google.common.collect.ImmutableMap;
import com.google.firestore.v1.Value;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@BetaApi
public final class Constant implements Expr {
public final class Constant extends Expr {

static final Constant NULL = new Constant(null);

private final Object value;

Constant(Object value) {
Expand Down Expand Up @@ -85,16 +89,14 @@ public static Constant of(Value value) {

@InternalApi
public static Constant nullValue() {
return new Constant(null);
return NULL;
}

@BetaApi
static Constant of(Object value) {
if (value == null) {
return new Constant(null);
}

if (value instanceof String) {
return NULL;
} else if (value instanceof String) {
return of((String) value);
} else if (value instanceof Number) {
return of((Number) value);
Expand All @@ -112,23 +114,25 @@ static Constant of(Object value) {
return of((DocumentReference) value);
} else if (value instanceof Value) {
return of((Value) value);
} else if (value instanceof Constant) {
return (Constant) value;
} else {
throw new IllegalArgumentException("Unknown type: " + value);
}
}

@BetaApi
public static <T> Constant of(Iterable<T> value) {
public static Constant of(Iterable<?> value) {
return new Constant(value);
}

@BetaApi
public static <T> Constant of(T[] value) {
public static Constant of(Object[] value) {
return new Constant(Arrays.asList(value.clone())); // Convert array to list
}

@BetaApi
public static <T> Constant of(Map<String, T> value) {
public static Constant of(Map<String, ?> value) {
return new Constant(value);
}

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

@InternalApi
public Value toProto() {
@Override
Value toProto() {
return encodeValue(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import javax.annotation.Nonnull;

@BetaApi
public final class Count extends Function implements Accumulator {
public final class Count extends Accumulator {
@InternalApi
Count(@Nonnull Expr value) {
super("count", ImmutableList.of(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.collect.ImmutableList;

@BetaApi
public final class CountIf extends Function implements Accumulator {
public final class CountIf extends Accumulator {
@InternalApi
CountIf(Expr value, boolean distinct) {
super("countif", (value != null) ? ImmutableList.of(value) : ImmutableList.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.collect.ImmutableList;

@BetaApi
public final class EndsWith extends Function implements FilterCondition {
public final class EndsWith extends FilterCondition {
@InternalApi
EndsWith(Expr expr, Expr postfix) {
super("ends_with", ImmutableList.of(expr, postfix));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.collect.ImmutableList;

@BetaApi
public final class Eq extends Function implements FilterCondition {
public final class Eq extends FilterCondition {
@InternalApi
Eq(Expr left, Expr right) {
super("eq", ImmutableList.of(left, right == null ? Constant.nullValue() : right));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.google.common.collect.ImmutableList;

@BetaApi
public final class Exists extends Function implements FilterCondition {
public final class Exists extends FilterCondition {
@InternalApi
Exists(Expr expr) {
super("exists", ImmutableList.of(expr));
Expand Down
Loading
Loading