Skip to content

Commit e7ec758

Browse files
committed
More expression work
1 parent ef3a8f2 commit e7ec758

File tree

1 file changed

+111
-5
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline

1 file changed

+111
-5
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/expressions.kt

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,8 +2014,45 @@ abstract class Expr internal constructor() {
20142014
fun cond(condition: BooleanExpr, thenValue: Any, elseValue: Any): Expr =
20152015
FunctionExpr("cond", condition, thenValue, elseValue)
20162016

2017-
/** @return A new [Expr] representing the exists operation. */
2018-
@JvmStatic fun exists(expr: Expr): BooleanExpr = BooleanExpr("exists", expr)
2017+
/**
2018+
* Creates an expression that checks if a field exists.
2019+
*
2020+
* @param value An expression evaluates to the name of the field to check.
2021+
* @return A new [Expr] representing the exists check.
2022+
*/
2023+
@JvmStatic fun exists(value: Expr): BooleanExpr = BooleanExpr("exists", value)
2024+
2025+
/**
2026+
* Creates an expression that checks if a field exists.
2027+
*
2028+
* @param fieldName The field name to check.
2029+
* @return A new [Expr] representing the exists check.
2030+
*/
2031+
@JvmStatic fun exists(fieldName: String): BooleanExpr = BooleanExpr("exists", fieldName)
2032+
2033+
/**
2034+
* Creates an expression that returns the [catchExpr] argument if there is an
2035+
* error, else return the result of the [tryExpr] argument evaluation.
2036+
*
2037+
* @param tryExpr The try expression.
2038+
* @param catchExpr The catch expression that will be evaluated and
2039+
* returned if the [tryExpr] produces an error.
2040+
* @return A new [Expr] representing the ifError operation.
2041+
*/
2042+
@JvmStatic
2043+
fun ifError(tryExpr: Expr, catchExpr: Expr): Expr = FunctionExpr("if_error", tryExpr, catchExpr)
2044+
2045+
/**
2046+
* Creates an expression that returns the [catchValue] argument if there is an
2047+
* error, else return the result of the [tryExpr] argument evaluation.
2048+
*
2049+
* @param tryExpr The try expression.
2050+
* @param catchValue The value that will be returned if the [tryExpr] produces an error.
2051+
* @return A new [Expr] representing the ifError operation.
2052+
*/
2053+
@JvmStatic
2054+
fun ifError(tryExpr: Expr, catchValue: Any): Expr =
2055+
FunctionExpr("if_error", tryExpr, catchValue)
20192056

20202057
/**
20212058
* Creates an expression that returns the document ID from a path.
@@ -2207,13 +2244,43 @@ abstract class Expr internal constructor() {
22072244
fun mod(other: Any) = Companion.mod(this, other)
22082245

22092246
/**
2247+
* Creates an expression that checks if this expression, when evaluated, is equal to any of the
2248+
* provided [values].
2249+
*
2250+
* @param values The values to check against.
2251+
* @return A new [BooleanExpr] representing the 'IN' comparison.
22102252
*/
22112253
fun eqAny(values: List<Any>) = Companion.eqAny(this, values)
22122254

22132255
/**
2256+
* Creates an expression that checks if this expression, when evaluated, is equal to any of the
2257+
* elements of [arrayExpression].
2258+
*
2259+
* @param arrayExpression An expression that evaluates to an array, whose elements to check for
2260+
* equality to the input.
2261+
* @return A new [BooleanExpr] representing the 'IN' comparison.
2262+
*/
2263+
fun eqAny(arrayExpression: Expr) = Companion.eqAny(this, arrayExpression)
2264+
2265+
/**
2266+
* Creates an expression that checks if this expression, when evaluated, is not equal to all the
2267+
* provided [values].
2268+
*
2269+
* @param values The values to check against.
2270+
* @return A new [BooleanExpr] representing the 'NOT IN' comparison.
22142271
*/
22152272
fun notEqAny(values: List<Any>) = Companion.notEqAny(this, values)
22162273

2274+
/**
2275+
* Creates an expression that checks if this expression, when evaluated, is not equal to all the
2276+
* elements of [arrayExpression].
2277+
*
2278+
* @param arrayExpression An expression that evaluates to an array, whose elements to check for
2279+
* equality to the input.
2280+
* @return A new [BooleanExpr] representing the 'NOT IN' comparison.
2281+
*/
2282+
fun notEqAny(arrayExpression: Expr) = Companion.notEqAny(this, arrayExpression)
2283+
22172284
/**
22182285
*/
22192286
fun isNan() = Companion.isNan(this)
@@ -2759,9 +2826,32 @@ abstract class Expr internal constructor() {
27592826
fun lte(value: Any): BooleanExpr = Companion.lte(this, value)
27602827

27612828
/**
2829+
* Creates an expression that checks if this expression evaluates to a name of the field that
2830+
* exists.
2831+
*
2832+
* @return A new [Expr] representing the exists check.
27622833
*/
27632834
fun exists(): BooleanExpr = Companion.exists(this)
27642835

2836+
/**
2837+
* Creates an expression that returns the [catchExpr] argument if there is an
2838+
* error, else return the result of this expression.
2839+
*
2840+
* @param catchExpr The catch expression that will be evaluated and
2841+
* returned if the this expression produces an error.
2842+
* @return A new [Expr] representing the ifError operation.
2843+
*/
2844+
fun ifError(catchExpr: Expr): Expr = Companion.ifError(this, catchExpr)
2845+
2846+
/**
2847+
* Creates an expression that returns the [catchValue] argument if there is an
2848+
* error, else return the result of this expression.
2849+
*
2850+
* @param catchValue The value that will be returned if this expression produces an error.
2851+
* @return A new [Expr] representing the ifError operation.
2852+
*/
2853+
fun ifError(catchValue: Any): Expr = Companion.ifError(this, catchValue)
2854+
27652855
internal abstract fun toProto(userDataReader: UserDataReader): Value
27662856
}
27672857

@@ -2838,17 +2928,24 @@ internal constructor(
28382928
private val params: Array<out Expr>,
28392929
private val options: InternalOptions = InternalOptions.EMPTY
28402930
) : Expr() {
2931+
internal constructor(name: String, param: Expr) : this(name, arrayOf(param))
28412932
internal constructor(
28422933
name: String,
28432934
param: Expr,
28442935
vararg params: Any
28452936
) : this(name, arrayOf(param, *toArrayOfExprOrConstant(params)))
2937+
internal constructor(
2938+
name: String,
2939+
param1: Expr,
2940+
param2: Expr
2941+
) : this(name, arrayOf(param1, param2))
28462942
internal constructor(
28472943
name: String,
28482944
param1: Expr,
28492945
param2: Expr,
28502946
vararg params: Any
28512947
) : this(name, arrayOf(param1, param2, *toArrayOfExprOrConstant(params)))
2948+
internal constructor(name: String, fieldName: String) : this(name, arrayOf(field(fieldName)))
28522949
internal constructor(
28532950
name: String,
28542951
fieldName: String,
@@ -2866,18 +2963,27 @@ internal constructor(
28662963
}
28672964
}
28682965

2869-
/** An interface that represents a filter condition. */
2966+
/** A class that represents a filter condition. */
28702967
open class BooleanExpr internal constructor(name: String, params: Array<out Expr>) :
28712968
FunctionExpr(name, params, InternalOptions.EMPTY) {
28722969
internal constructor(
28732970
name: String,
2874-
params: List<Any>
2875-
) : this(name, toArrayOfExprOrConstant(params))
2971+
param: Expr
2972+
) : this(name, arrayOf(param))
28762973
internal constructor(
28772974
name: String,
28782975
param: Expr,
28792976
vararg params: Any
28802977
) : this(name, arrayOf(param, *toArrayOfExprOrConstant(params)))
2978+
internal constructor(
2979+
name: String,
2980+
param1: Expr,
2981+
param2: Expr
2982+
) : this(name, arrayOf(param1, param2))
2983+
internal constructor(
2984+
name: String,
2985+
fieldName: String
2986+
) : this(name, arrayOf(field(fieldName)))
28812987
internal constructor(
28822988
name: String,
28832989
fieldName: String,

0 commit comments

Comments
 (0)