Skip to content

Commit

Permalink
COnvenience methods for and/or
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Dec 22, 2023
1 parent fda06c3 commit 4fbfc1d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,11 @@ class FilterBuilder internal constructor() {
}
return Filter.Or(leftList + rightList)
}

fun all(vararg filters: Filter): Filter? = filters.toList().combine { left, right -> left and right }
fun any(vararg filters: Filter): Filter? = filters.toList().combine { left, right -> left or right }

private fun Collection<Filter>.combine(over: (Filter, Filter) -> Filter): Filter? = fold<Filter, Filter?>(null) { acc, filter ->
acc?.let { over(acc, filter) } ?: filter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,48 +88,44 @@ fun Query.where(path: FieldPath, equalTo: Any?) = where {

@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
fun Query.where(field: String, lessThan: Any? = null, greaterThan: Any? = null, arrayContains: Any? = null) = where {
val filters = listOfNotNull(
lessThan?.let { field lessThan it },
greaterThan?.let { field greaterThan it },
arrayContains?.let { field contains it }
all(
*listOfNotNull(
lessThan?.let { field lessThan it },
greaterThan?.let { field greaterThan it },
arrayContains?.let { field contains it }
).toTypedArray()
)
filters.fold<Filter, Filter?>(null) { acc, filter ->
acc?.let { it and filter } ?: filter
}
}

@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
fun Query.where(path: FieldPath, lessThan: Any? = null, greaterThan: Any? = null, arrayContains: Any? = null) = where {
val filters = listOfNotNull(
lessThan?.let { path lessThan it },
greaterThan?.let { path greaterThan it },
arrayContains?.let { path contains it }
all(
*listOfNotNull(
lessThan?.let { path lessThan it },
greaterThan?.let { path greaterThan it },
arrayContains?.let { path contains it }
).toTypedArray()
)
filters.fold<Filter, Filter?>(null) { acc, filter ->
acc?.let { it and filter } ?: filter
}
}

@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
fun Query.where(field: String, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null) = where {
val filters = listOfNotNull(
inArray?.let { field `in` it },
arrayContainsAny?.let { field containsAny it },
all(
*listOfNotNull(
inArray?.let { field `in` it },
arrayContainsAny?.let { field containsAny it },
).toTypedArray()
)
filters.fold<Filter, Filter?>(null) { acc, filter ->
acc?.let { it and filter } ?: filter
}
}

@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
fun Query.where(path: FieldPath, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null) = where {
val filters = listOfNotNull(
inArray?.let { path `in` it },
arrayContainsAny?.let { path containsAny it },
all(
*listOfNotNull(
inArray?.let { path `in` it },
arrayContainsAny?.let { path containsAny it },
).toTypedArray()
)
filters.fold<Filter, Filter?>(null) { acc, filter ->
acc?.let { it and filter } ?: filter
}
}

fun Query.orderBy(field: String, direction: Direction = Direction.ASCENDING) = _orderBy(field, direction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,13 @@ class FirebaseFirestoreTest {
val andOrQuery = firestore
.collection("testFirestoreQuerying")
.where {
(
FieldPath(FirestoreTest::prop1.name) equalTo "aaa" or
(FieldPath(FirestoreTest::count.name) equalTo 2)
) and (FieldPath(FirestoreTest::list.name) contains "a")
all(
any(
FieldPath(FirestoreTest::prop1.name) equalTo "aaa",
FieldPath(FirestoreTest::count.name) equalTo 2,
)!!,
FieldPath(FirestoreTest::list.name) contains "a"
)
}
andOrQuery.assertDocuments(FirestoreTest.serializer(), testOne)
}
Expand Down

0 comments on commit 4fbfc1d

Please sign in to comment.