Skip to content

Commit 4fbfc1d

Browse files
committed
COnvenience methods for and/or
1 parent fda06c3 commit 4fbfc1d

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/Filter.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,11 @@ class FilterBuilder internal constructor() {
144144
}
145145
return Filter.Or(leftList + rightList)
146146
}
147+
148+
fun all(vararg filters: Filter): Filter? = filters.toList().combine { left, right -> left and right }
149+
fun any(vararg filters: Filter): Filter? = filters.toList().combine { left, right -> left or right }
150+
151+
private fun Collection<Filter>.combine(over: (Filter, Filter) -> Filter): Filter? = fold<Filter, Filter?>(null) { acc, filter ->
152+
acc?.let { over(acc, filter) } ?: filter
153+
}
147154
}

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,48 +88,44 @@ fun Query.where(path: FieldPath, equalTo: Any?) = where {
8888

8989
@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
9090
fun Query.where(field: String, lessThan: Any? = null, greaterThan: Any? = null, arrayContains: Any? = null) = where {
91-
val filters = listOfNotNull(
92-
lessThan?.let { field lessThan it },
93-
greaterThan?.let { field greaterThan it },
94-
arrayContains?.let { field contains it }
91+
all(
92+
*listOfNotNull(
93+
lessThan?.let { field lessThan it },
94+
greaterThan?.let { field greaterThan it },
95+
arrayContains?.let { field contains it }
96+
).toTypedArray()
9597
)
96-
filters.fold<Filter, Filter?>(null) { acc, filter ->
97-
acc?.let { it and filter } ?: filter
98-
}
9998
}
10099

101100
@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
102101
fun Query.where(path: FieldPath, lessThan: Any? = null, greaterThan: Any? = null, arrayContains: Any? = null) = where {
103-
val filters = listOfNotNull(
104-
lessThan?.let { path lessThan it },
105-
greaterThan?.let { path greaterThan it },
106-
arrayContains?.let { path contains it }
102+
all(
103+
*listOfNotNull(
104+
lessThan?.let { path lessThan it },
105+
greaterThan?.let { path greaterThan it },
106+
arrayContains?.let { path contains it }
107+
).toTypedArray()
107108
)
108-
filters.fold<Filter, Filter?>(null) { acc, filter ->
109-
acc?.let { it and filter } ?: filter
110-
}
111109
}
112110

113111
@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
114112
fun Query.where(field: String, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null) = where {
115-
val filters = listOfNotNull(
116-
inArray?.let { field `in` it },
117-
arrayContainsAny?.let { field containsAny it },
113+
all(
114+
*listOfNotNull(
115+
inArray?.let { field `in` it },
116+
arrayContainsAny?.let { field containsAny it },
117+
).toTypedArray()
118118
)
119-
filters.fold<Filter, Filter?>(null) { acc, filter ->
120-
acc?.let { it and filter } ?: filter
121-
}
122119
}
123120

124121
@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { }", "dev.gitlive.firebase.firestore"))
125122
fun Query.where(path: FieldPath, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null) = where {
126-
val filters = listOfNotNull(
127-
inArray?.let { path `in` it },
128-
arrayContainsAny?.let { path containsAny it },
123+
all(
124+
*listOfNotNull(
125+
inArray?.let { path `in` it },
126+
arrayContainsAny?.let { path containsAny it },
127+
).toTypedArray()
129128
)
130-
filters.fold<Filter, Filter?>(null) { acc, filter ->
131-
acc?.let { it and filter } ?: filter
132-
}
133129
}
134130

135131
fun Query.orderBy(field: String, direction: Direction = Direction.ASCENDING) = _orderBy(field, direction)

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,13 @@ class FirebaseFirestoreTest {
751751
val andOrQuery = firestore
752752
.collection("testFirestoreQuerying")
753753
.where {
754-
(
755-
FieldPath(FirestoreTest::prop1.name) equalTo "aaa" or
756-
(FieldPath(FirestoreTest::count.name) equalTo 2)
757-
) and (FieldPath(FirestoreTest::list.name) contains "a")
754+
all(
755+
any(
756+
FieldPath(FirestoreTest::prop1.name) equalTo "aaa",
757+
FieldPath(FirestoreTest::count.name) equalTo 2,
758+
)!!,
759+
FieldPath(FirestoreTest::list.name) contains "a"
760+
)
758761
}
759762
andOrQuery.assertDocuments(FirestoreTest.serializer(), testOne)
760763
}

0 commit comments

Comments
 (0)