-
-
Notifications
You must be signed in to change notification settings - Fork 735
New query condition to match all strings that starts with some other given strings #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0902fee
9a53ca2
bf8be89
c1b2e9d
d57dad3
caaf453
7c8a423
3402658
f5c650c
d1624ca
2bbac59
1d04ff4
6ab5c59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
*/ | ||
package com.parse; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
|
@@ -1694,6 +1696,31 @@ public ParseQuery<T> whereContainsAll(String key, Collection<?> values) { | |
return this; | ||
} | ||
|
||
/** | ||
* Add a constraint to the query that requires a particular key's value match another | ||
* {@code ParseQuery}. | ||
* <p/> | ||
* This only works on keys whose values are {@link ParseObject}s or lists of {@link ParseObject}s. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this javadoc is wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought so also when I copied the comment from Thanks to comment this 👍 Someone could write a good documentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think you can remove/reword both to their real meaning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments updated. |
||
* Add a constraint to the query that requires a particular key's value to contain each one of | ||
* the provided list of values entirely or just starting with given strings. | ||
* | ||
* @param key | ||
* The key to check. This key's value must be an array. | ||
* @param values | ||
* The values that will match. | ||
* @return this, so you can chain this call. | ||
*/ | ||
public ParseQuery<T> whereContainsAllStartsWith(String key, Collection<String> values) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any suggestions for a different name? This seems obscure at first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the beginning I was thinking about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It took me a while to understand what this does. Personally I think the cleanest and simplest wording is to keep
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be a good name, but I think it's good to have the Let's see what other prefer. |
||
checkIfRunning(); | ||
|
||
ArrayList<Pattern> regexValues = new ArrayList<>(); | ||
for (String value : values) { | ||
regexValues.add(Pattern.compile(buildStartsWithRegex(value))); | ||
} | ||
|
||
return whereContainsAll(key, regexValues); | ||
} | ||
|
||
/** | ||
* Add a constraint to the query that requires a particular key's value match another | ||
* {@code ParseQuery}. | ||
|
@@ -1947,7 +1974,7 @@ public ParseQuery<T> whereContains(String key, String substring) { | |
* @return this, so you can chain this call. | ||
*/ | ||
public ParseQuery<T> whereStartsWith(String key, String prefix) { | ||
String regex = "^" + Pattern.quote(prefix); | ||
String regex = buildStartsWithRegex(prefix); | ||
whereMatches(key, regex); | ||
return this; | ||
} | ||
|
@@ -2150,4 +2177,15 @@ public ParseQuery<T> setTrace(boolean shouldTrace) { | |
builder.setTracingEnabled(shouldTrace); | ||
return this; | ||
} | ||
|
||
/** | ||
* Helper method to convert a string to regex for start word matching. | ||
* | ||
* @param prefix String to use as prefix in regex. | ||
* @return The string converted as regex for start word matching. | ||
*/ | ||
@NonNull | ||
private String buildStartsWithRegex(String prefix) { | ||
return "^" + Pattern.quote(prefix); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to avoid
Pattern
s and move this logic toParseQuery
just like all other $ constraints?I mean using
ParseQuery.addCondition(key, “$regex”, string)
. That way we keep consistencies and leave encoder untouched.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that could be better to avoid update the ParseEncoder. I didn't researched a lot.
Now, I think could be better to update the
constraintsAllStartsWith
to:This might auto compile as expected. If people prefer to revert the ParseEncoder changes, I'll update the PR 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense. Also since whereContainsAll is documented to work only with
ParseObject
s, you could call directlybuilder.addCondition(key, "$all", startsWithConstraints)
and return thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the function to use KeyConstraints.
I keep using
whereContsinaAll
because was not restricted toParseObject
s. The docs were wrong.