Skip to content

Commit 6a024dc

Browse files
committed
Add array
1 parent 38b2268 commit 6a024dc

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/model/Values.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -652,16 +652,8 @@ internal object Values {
652652
@JvmStatic fun encodeValue(date: Date): Value = encodeValue(com.google.firebase.Timestamp((date)))
653653

654654
@JvmStatic
655-
fun encodeValue(timestamp: com.google.firebase.Timestamp): Value {
656-
// Firestore backend truncates precision down to microseconds. To ensure offline mode works
657-
// the same with regards to truncation, perform the truncation immediately without waiting for
658-
// the backend to do that.
659-
val truncatedNanoseconds: Int = timestamp.nanoseconds / 1000 * 1000
660-
661-
return encodeValue(
662-
Timestamp.newBuilder().setSeconds(timestamp.seconds).setNanos(truncatedNanoseconds).build()
663-
)
664-
}
655+
fun encodeValue(timestamp: com.google.firebase.Timestamp): Value =
656+
encodeValue(timestamp(timestamp.seconds, timestamp.nanoseconds))
665657

666658
@JvmStatic
667659
fun encodeValue(value: Timestamp): Value = Value.newBuilder().setTimestampValue(value).build()
@@ -736,6 +728,12 @@ internal object Values {
736728
}
737729

738730
@JvmStatic
739-
fun timestamp(seconds: Long, nanos: Int): Timestamp =
740-
Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build()
731+
fun timestamp(seconds: Long, nanos: Int): Timestamp {
732+
// Firestore backend truncates precision down to microseconds. To ensure offline mode works
733+
// the same with regards to truncation, perform the truncation immediately without waiting for
734+
// the backend to do that.
735+
val truncatedNanoseconds: Int = nanos / 1000 * 1000
736+
737+
return Timestamp.newBuilder().setSeconds(seconds).setNanos(truncatedNanoseconds).build()
738+
}
741739
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal sealed class EvaluateResult(val value: Value?) {
1717
fun long(long: Long) = EvaluateResultValue(encodeValue(long))
1818
fun long(int: Int) = EvaluateResultValue(encodeValue(int.toLong()))
1919
fun string(string: String) = EvaluateResultValue(encodeValue(string))
20+
fun list(list: List<Value>) = EvaluateResultValue(encodeValue(list))
2021
fun timestamp(timestamp: Timestamp): EvaluateResult =
2122
EvaluateResultValue(encodeValue(timestamp))
2223
fun timestamp(seconds: Long, nanos: Int): EvaluateResult =

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ internal val evaluateSubtract = arithmeticPrimitive(Math::subtractExact, Double:
218218

219219
// === Array Functions ===
220220

221+
internal val evaluateArray = variadicNullableValueFunction(EvaluateResult.Companion::list)
222+
221223
internal val evaluateEqAny = notImplemented
222224

223225
internal val evaluateNotEqAny = notImplemented
@@ -632,6 +634,15 @@ private inline fun variadicFunction(
632634
}
633635
}
634636

637+
@JvmName("variadicNullableValueFunction")
638+
private inline fun variadicNullableValueFunction(
639+
crossinline function: (List<Value>) -> EvaluateResult
640+
): EvaluateFunction = { params ->
641+
block@{ input: MutableDocument ->
642+
catch { function(params.map { p -> p(input).value ?: return@block EvaluateResultError }) }
643+
}
644+
}
645+
635646
@JvmName("variadicStringFunction")
636647
private inline fun variadicFunction(
637648
crossinline function: (List<String>) -> EvaluateResult

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,26 @@ abstract class Expr internal constructor() {
26012601
fun lte(fieldName: String, value: Any): BooleanExpr =
26022602
BooleanExpr("lte", evaluateLte, fieldName, value)
26032603

2604+
/**
2605+
* Creates an expression that creates a Firestore array value from an input array.
2606+
*
2607+
* @param elements The input array to evaluate in the expression.
2608+
* @return A new [Expr] representing the array function.
2609+
*/
2610+
@JvmStatic
2611+
fun array(vararg elements: Expr): Expr =
2612+
FunctionExpr("array", evaluateArray, elements)
2613+
2614+
/**
2615+
* Creates an expression that creates a Firestore array value from an input array.
2616+
*
2617+
* @param elements The input array to evaluate in the expression.
2618+
* @return A new [Expr] representing the array function.
2619+
*/
2620+
@JvmStatic
2621+
fun array(elements: List<Expr>): Expr =
2622+
FunctionExpr("array", evaluateArray, elements.toTypedArray())
2623+
26042624
/**
26052625
* Creates an expression that concatenates an array with other arrays.
26062626
*

0 commit comments

Comments
 (0)