Skip to content

Commit aa36e0e

Browse files
danielsanfrJawnnypoo
authored andcommitted
ParseQuery extensions (#929)
* Implement extensions to ParseQuery to use property's name as key instead of strings Signed-off-by: Daniel San <[email protected]> * Update README to show ParseQuery extensions Signed-off-by: Daniel San <[email protected]>
1 parent 966a740 commit aa36e0e

File tree

2 files changed

+273
-2
lines changed

2 files changed

+273
-2
lines changed

ktx/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ The `stringAttribute` is a property delegate, and we have many other specialized
7575

7676
This causes us to not have to write get/set and besides, it removed the get/put boilerplate which is a must to map our classes with the Parse collections.
7777

78+
## ParseQuery extensions
79+
80+
Using Property Delegates will allow you to use a more secure way of creating queries.
81+
82+
If is needed to rename some property of a ParseObject, it is only necessary to use the IDE refactoring tool, that your queries will be automatically updated, which is not the case if hard coded strings are used.
83+
84+
```kotlin
85+
@ParseClassName("Cat")
86+
class Cat : ParseObject() {
87+
88+
var age by intAttribute()
89+
90+
}
91+
92+
val query = ParseQuery.getQuery(Cat::class.java)
93+
// Use this syntax
94+
query.whereEqualTo(Cat::name, 1)
95+
// instead of
96+
query.whereEqualTo("age", 1)
97+
// or
98+
query.whereEqualTo(Cat::age.name, 1)
99+
```
100+
78101
## Contributing
79102
When contributing to the `ktx` module, please first consider if the extension function you are wanting to add would potentially be better suited in the main `parse` module. If it is something specific to Kotlin users or only useful in a Kotlin project, feel free to make a PR adding it to this module. Otherwise, consider adding the addition to the `parse` module itself, so that it is still usable in Java.
80103

@@ -84,4 +107,4 @@ When contributing to the `ktx` module, please first consider if the extension fu
84107

85108
This source code is licensed under the BSD-style license found in the
86109
LICENSE file in the root directory of this source tree. An additional grant
87-
of patent rights can be found in the PATENTS file in the same directory.
110+
of patent rights can be found in the PATENTS file in the same directory.

ktx/src/main/java/com/parse/ktx/ParseQuery.kt

Lines changed: 249 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
package com.parse.ktx
44

55
import com.parse.ParseException
6+
import com.parse.ParseGeoPoint
67
import com.parse.ParseObject
8+
import com.parse.ParsePolygon
79
import com.parse.ParseQuery
10+
import kotlin.reflect.KProperty
811

912
/**
1013
* Parse hard limits [ParseQuery.find] to [ParseQuery.MAX_LIMIT] objects. This will fetch absolutely all the
@@ -32,4 +35,249 @@ inline fun <T: ParseObject> ParseQuery<T>.findAll(): List<T> {
3235
}
3336
throw ex
3437
}
35-
}
38+
}
39+
40+
/**
41+
* @see ParseQuery.addDescendingOrder
42+
*/
43+
inline fun <T : ParseObject> ParseQuery<T>.addDescendingOrder(key: KProperty<Any?>): ParseQuery<T> {
44+
return addDescendingOrder(key.name)
45+
}
46+
47+
/**
48+
* @see ParseQuery.include
49+
*/
50+
fun <T : ParseObject> ParseQuery<T>.include(vararg properties: KProperty<Any?>): ParseQuery<T> {
51+
return include(properties.joinToString(".") { it.name })
52+
}
53+
54+
/**
55+
* @see ParseQuery.orderByAscending
56+
*/
57+
inline fun <T : ParseObject> ParseQuery<T>.orderByAscending(key: KProperty<Any?>): ParseQuery<T> {
58+
return orderByAscending(key.name)
59+
}
60+
61+
/**
62+
* @see ParseQuery.orderByDescending
63+
*/
64+
inline fun <T : ParseObject> ParseQuery<T>.orderByDescending(key: KProperty<Any?>): ParseQuery<T> {
65+
return orderByDescending(key.name)
66+
}
67+
68+
/**
69+
* @see ParseQuery.selectKeys
70+
*/
71+
fun <T : ParseObject> ParseQuery<T>.selectKeys(keys: Collection<KProperty<Any?>>): ParseQuery<T> {
72+
return selectKeys(keys.map { it.name })
73+
}
74+
75+
/**
76+
* @see ParseQuery.whereContainedIn
77+
*/
78+
inline fun <T : ParseObject> ParseQuery<T>.whereContainedIn(key: KProperty<Any?>, values: Collection<Any?>): ParseQuery<T> {
79+
return whereContainedIn(key.name, values)
80+
}
81+
82+
/**
83+
* @see ParseQuery.whereContains
84+
*/
85+
inline fun <T : ParseObject> ParseQuery<T>.whereContains(key: KProperty<Any?>, substring: String): ParseQuery<T> {
86+
return whereContains(key.name, substring)
87+
}
88+
89+
/**
90+
* @see ParseQuery.whereContainsAll
91+
*/
92+
inline fun <T : ParseObject> ParseQuery<T>.whereContainsAll(key: KProperty<Any?>, values: Collection<ParseObject>): ParseQuery<T> {
93+
return whereContainsAll(key.name, values)
94+
}
95+
96+
/**
97+
* @see ParseQuery.whereContainsAllStartsWith
98+
*/
99+
inline fun <T : ParseObject> ParseQuery<T>.whereContainsAllStartsWith(key: KProperty<Any?>, values: Collection<String>): ParseQuery<T> {
100+
return whereContainsAllStartsWith(key.name, values)
101+
}
102+
103+
/**
104+
* @see ParseQuery.whereDoesNotExist
105+
*/
106+
inline fun <T : ParseObject> ParseQuery<T>.whereDoesNotExist(key: KProperty<Any?>): ParseQuery<T> {
107+
return whereDoesNotExist(key.name)
108+
}
109+
110+
/**
111+
* @see ParseQuery.whereDoesNotMatchKeyInQuery
112+
*/
113+
inline fun <T : ParseObject> ParseQuery<T>.whereDoesNotMatchKeyInQuery(key: KProperty<Any?>, keyInQuery: KProperty<Any?>, query: ParseQuery<ParseObject>): ParseQuery<T> {
114+
return whereDoesNotMatchKeyInQuery(key.name, keyInQuery.name, query)
115+
}
116+
117+
/**
118+
* @see ParseQuery.whereDoesNotMatchQuery
119+
*/
120+
inline fun <T : ParseObject> ParseQuery<T>.whereDoesNotMatchQuery(key: KProperty<Any?>, query: ParseQuery<out ParseObject>): ParseQuery<T> {
121+
return whereDoesNotMatchQuery(key.name, query)
122+
}
123+
124+
/**
125+
* @see ParseQuery.whereEndsWith
126+
*/
127+
inline fun <T : ParseObject> ParseQuery<T>.whereEndsWith(key: KProperty<Any?>, suffix: String): ParseQuery<T> {
128+
return whereEndsWith(key.name, suffix)
129+
}
130+
131+
/**
132+
* @see ParseQuery.whereEqualTo
133+
*/
134+
inline fun <T : ParseObject> ParseQuery<T>.whereEqualTo(key: KProperty<Any?>, value: Any?): ParseQuery<T> {
135+
return whereEqualTo(key.name, value)
136+
}
137+
138+
/**
139+
* @see ParseQuery.whereExists
140+
*/
141+
inline fun <T : ParseObject> ParseQuery<T>.whereExists(key: KProperty<Any?>): ParseQuery<T> {
142+
return whereExists(key.name)
143+
}
144+
145+
/**
146+
* @see ParseQuery.whereFullText
147+
*/
148+
inline fun <T : ParseObject> ParseQuery<T>.whereFullText(key: KProperty<Any?>, text: String): ParseQuery<T> {
149+
return whereFullText(key.name, text)
150+
}
151+
152+
/**
153+
* @see ParseQuery.whereGreaterThan
154+
*/
155+
inline fun <T : ParseObject> ParseQuery<T>.whereGreaterThan(key: KProperty<Any?>, value: Any): ParseQuery<T> {
156+
return whereGreaterThan(key.name, value)
157+
}
158+
159+
/**
160+
* @see ParseQuery.whereGreaterThanOrEqualTo
161+
*/
162+
inline fun <T : ParseObject> ParseQuery<T>.whereGreaterThanOrEqualTo(key: KProperty<Any?>, value: Any): ParseQuery<T> {
163+
return whereGreaterThanOrEqualTo(key.name, value)
164+
}
165+
166+
/**
167+
* @see ParseQuery.whereLessThan
168+
*/
169+
inline fun <T : ParseObject> ParseQuery<T>.whereLessThan(key: KProperty<Any?>, value: Any): ParseQuery<T> {
170+
return whereLessThan(key.name, value)
171+
}
172+
173+
/**
174+
* @see ParseQuery.whereLessThanOrEqualTo
175+
*/
176+
inline fun <T : ParseObject> ParseQuery<T>.whereLessThanOrEqualTo(key: KProperty<Any?>, value: Any): ParseQuery<T> {
177+
return whereLessThanOrEqualTo(key.name, value)
178+
}
179+
180+
/**
181+
* @see ParseQuery.whereMatches
182+
*/
183+
inline fun <T : ParseObject> ParseQuery<T>.whereMatches(key: KProperty<Any?>, regex: String): ParseQuery<T> {
184+
return whereMatches(key.name, regex)
185+
}
186+
187+
/**
188+
* @see ParseQuery.whereMatches
189+
*/
190+
inline fun <T : ParseObject> ParseQuery<T>.whereMatches(key: KProperty<Any?>, regex: String, modifiers: String): ParseQuery<T> {
191+
return whereMatches(key.name, regex, modifiers)
192+
}
193+
194+
/**
195+
* @see ParseQuery.whereMatchesKeyInQuery
196+
*/
197+
inline fun <T : ParseObject> ParseQuery<T>.whereMatchesKeyInQuery(key: KProperty<Any?>, keyInQuery: KProperty<Any?>, query: ParseQuery<ParseObject>): ParseQuery<T> {
198+
return whereMatchesKeyInQuery(key.name, keyInQuery.name, query)
199+
}
200+
201+
/**
202+
* @see ParseQuery.whereMatchesQuery
203+
*/
204+
inline fun <T : ParseObject> ParseQuery<T>.whereMatchesQuery(key: KProperty<Any?>, query: ParseQuery<out ParseObject>): ParseQuery<T> {
205+
return whereMatchesQuery(key.name, query)
206+
}
207+
208+
/**
209+
* @see ParseQuery.whereNear
210+
*/
211+
inline fun <T : ParseObject> ParseQuery<T>.whereNear(key: KProperty<Any?>, point: ParseGeoPoint): ParseQuery<T> {
212+
return whereNear(key.name, point)
213+
}
214+
215+
/**
216+
* @see ParseQuery.whereNotContainedIn
217+
*/
218+
inline fun <T : ParseObject> ParseQuery<T>.whereNotContainedIn(key: KProperty<Any?>, values: Collection<Any?>): ParseQuery<T> {
219+
return whereNotContainedIn(key.name, values)
220+
}
221+
222+
/**
223+
* @see ParseQuery.whereNotEqualTo
224+
*/
225+
inline fun <T : ParseObject> ParseQuery<T>.whereNotEqualTo(key: KProperty<Any?>, value: Any?): ParseQuery<T> {
226+
return whereNotEqualTo(key.name, value)
227+
}
228+
229+
/**
230+
* @see ParseQuery.wherePolygonContains
231+
*/
232+
inline fun <T : ParseObject> ParseQuery<T>.wherePolygonContains(key: KProperty<Any?>, point: ParseGeoPoint): ParseQuery<T> {
233+
return wherePolygonContains(key.name, point)
234+
}
235+
236+
/**
237+
* @see ParseQuery.whereStartsWith
238+
*/
239+
inline fun <T : ParseObject> ParseQuery<T>.whereStartsWith(key: KProperty<Any?>, prefix: String): ParseQuery<T> {
240+
return whereStartsWith(key.name, prefix)
241+
}
242+
243+
/**
244+
* @see ParseQuery.whereWithinGeoBox
245+
*/
246+
inline fun <T : ParseObject> ParseQuery<T>.whereWithinGeoBox(key: KProperty<Any?>, southwest: ParseGeoPoint, northeast: ParseGeoPoint): ParseQuery<T> {
247+
return whereWithinGeoBox(key.name, southwest, northeast)
248+
}
249+
250+
/**
251+
* @see ParseQuery.whereWithinKilometers
252+
*/
253+
inline fun <T : ParseObject> ParseQuery<T>.whereWithinKilometers(key: KProperty<Any?>, point: ParseGeoPoint, maxDistance: Double): ParseQuery<T> {
254+
return whereWithinKilometers(key.name, point, maxDistance)
255+
}
256+
257+
/**
258+
* @see ParseQuery.whereWithinMiles
259+
*/
260+
inline fun <T : ParseObject> ParseQuery<T>.whereWithinMiles(key: KProperty<Any?>, point: ParseGeoPoint, maxDistance: Double): ParseQuery<T> {
261+
return whereWithinMiles(key.name, point, maxDistance)
262+
}
263+
264+
/**
265+
* @see ParseQuery.whereWithinPolygon
266+
*/
267+
inline fun <T : ParseObject> ParseQuery<T>.whereWithinPolygon(key: KProperty<Any?>, points: List<ParseGeoPoint>): ParseQuery<T> {
268+
return whereWithinPolygon(key.name, points)
269+
}
270+
271+
/**
272+
* @see ParseQuery.whereWithinPolygon
273+
*/
274+
inline fun <T : ParseObject> ParseQuery<T>.whereWithinPolygon(key: KProperty<Any?>, polygon: ParsePolygon): ParseQuery<T> {
275+
return whereWithinPolygon(key.name, polygon)
276+
}
277+
278+
/**
279+
* @see ParseQuery.whereWithinRadians
280+
*/
281+
inline fun <T : ParseObject> ParseQuery<T>.whereWithinRadians(key: KProperty<Any?>, point: ParseGeoPoint, maxDistance: Double): ParseQuery<T> {
282+
return whereWithinRadians(key.name, point, maxDistance)
283+
}

0 commit comments

Comments
 (0)