Skip to content
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

Add LTE/GTE support #67

Closed
wants to merge 13 commits into from
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,14 @@ Request : `/cars?search=color!Red`

3. Using the greater than operator `>`
Request : `/cars?search=creationyear>2017`
> Note: You can use the `>:` operator as well.

![greater than operator example](./docs/images/greater-than-example.gif)

4. Using the less than operator `<`
Request : `/cars?search=price<100000`
Request : `/cars?search=price<100000`
> Note: You can use the `<:` operator as well.

![less than operator example](./docs/images/less-than-example.gif)

5. Using the starts with operator `*`
Expand Down
8 changes: 8 additions & 0 deletions src/main/antlr4/Query.g4
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ value
op
: EQ
| GT
| GTE
| LT
| LTE
| NOT_EQ
;

Expand Down Expand Up @@ -137,11 +139,17 @@ GT
: '>'
;

GTE
: '>:'
;

LT
: '<'
;

LTE
: '<:'
;

EQ
: ':'
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/sipios/springsearch/SearchCriteria.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SearchCriteria // Change EQUALS into ENDS_WITH, CONTAINS, STARTS_WITH base
var operation: SearchOperation?

init {
var op = SearchOperation.getSimpleOperation(operation[0])
var op = SearchOperation.getSimpleOperation(operation)
if (op != null) {
// Change EQUALS into ENDS_WITH, CONTAINS, STARTS_WITH based on the presence of * in the value
val startsWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX)
Expand Down
16 changes: 9 additions & 7 deletions src/main/kotlin/com/sipios/springsearch/SearchOperation.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.sipios.springsearch

enum class SearchOperation {
EQUALS, NOT_EQUALS, GREATER_THAN, LESS_THAN, STARTS_WITH, ENDS_WITH, CONTAINS, DOESNT_START_WITH, DOESNT_END_WITH, DOESNT_CONTAIN;
EQUALS, NOT_EQUALS, GREATER_THAN, LESS_THAN, STARTS_WITH, ENDS_WITH, CONTAINS, DOESNT_START_WITH, DOESNT_END_WITH, DOESNT_CONTAIN, GREATER_THAN_EQUALS, LESS_THAN_EQUALS;

companion object {
val SIMPLE_OPERATION_SET = arrayOf(":", "!", ">", "<", "~")
val SIMPLE_OPERATION_SET = arrayOf(":", "!", ">", "<", "~", ">:", "<:")
val ZERO_OR_MORE_REGEX = "*"
val OR_OPERATOR = "OR"
val AND_OPERATOR = "AND"
Expand All @@ -17,12 +17,14 @@ enum class SearchOperation {
* @param input operation as string
* @return The matching operation
*/
fun getSimpleOperation(input: Char): SearchOperation? {
fun getSimpleOperation(input: String): SearchOperation? {
return when (input) {
':' -> EQUALS
'!' -> NOT_EQUALS
'>' -> GREATER_THAN
'<' -> LESS_THAN
":" -> EQUALS
"!" -> NOT_EQUALS
">" -> GREATER_THAN
"<" -> LESS_THAN
">:" -> GREATER_THAN_EQUALS
"<:" -> LESS_THAN_EQUALS
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class DateStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as Date)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as Date)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as Date)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as Date)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class DoubleStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as Double)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as Double)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as Double)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as Double)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class DurationStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as Duration)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as Duration)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as Duration)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as Duration)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class FloatStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as Float)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as Float)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as Float)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as Float)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class InstantStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as Instant)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as Instant)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as Instant)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as Instant)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class IntStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as Int)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as Int)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as Int)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as Int)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class LocalDateStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as LocalDate)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as LocalDate)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as LocalDate)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as LocalDate)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class LocalDateTimeStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as LocalDateTime)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as LocalDateTime)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as LocalDateTime)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as LocalDateTime)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class LocalTimeStrategy : ParsingStrategy {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path[fieldName], value as LocalTime)
SearchOperation.LESS_THAN -> builder.lessThan(path[fieldName], value as LocalTime)
SearchOperation.GREATER_THAN_EQUALS -> builder.greaterThanOrEqualTo(path[fieldName], value as LocalTime)
SearchOperation.LESS_THAN_EQUALS -> builder.lessThanOrEqualTo(path[fieldName], value as LocalTime)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}
Expand Down
Loading