Skip to content

Commit

Permalink
Merge pull request #57 from fernandoSkoda/InstantStrategy
Browse files Browse the repository at this point in the history
Added Support for Instant ParsingStrategy.kt, created Test.
  • Loading branch information
alexadrien authored May 6, 2022
2 parents b2aacff + db0cffa commit 5c41d11
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 80 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
</parent>
<groupId>com.sipios</groupId>
<artifactId>spring-search</artifactId>
<version>0.2.4</version>
<version>0.2.5</version>
<name>spring-search</name>
<description>API for generating spring boot database queries</description>

<properties>
<java.version>1.8</java.version>
<kotlin.version>1.4.10</kotlin.version>
<kotlin.version>1.6.20</kotlin.version>
<antl4.version>4.9</antl4.version>
<grammar.package>com.sipios.springsearch.grammar</grammar.package>
<grammar.directory>com/sipios/springsearch/grammar</grammar.directory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sipios.springsearch.strategies

import com.sipios.springsearch.SearchOperation
import java.time.Instant
import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.Path
import javax.persistence.criteria.Predicate
import kotlin.reflect.KClass

class InstantStrategy : ParsingStrategy {
override fun buildPredicate(
builder: CriteriaBuilder,
path: Path<*>,
fieldName: String,
ops: SearchOperation?,
value: Any?
): Predicate? {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path.get(fieldName), value as Instant)
SearchOperation.LESS_THAN -> builder.lessThan(path.get(fieldName), value as Instant)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}

override fun parse(value: String?, fieldClass: KClass<out Any>): Any? {
return Instant.parse(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sipios.springsearch.strategies
import com.sipios.springsearch.SearchOperation
import com.sipios.springsearch.anotation.SearchSpec
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
Expand Down Expand Up @@ -72,6 +73,7 @@ interface ParsingStrategy {
fieldClass == LocalDate::class -> LocalDateStrategy()
fieldClass == LocalTime::class -> LocalTimeStrategy()
fieldClass == LocalDateTime::class -> LocalDateTimeStrategy()
fieldClass == Instant::class -> InstantStrategy()
fieldClass == UUID::class -> UUIDStrategy()
else -> StringStrategy(searchSpecAnnotation)
}
Expand Down
Loading

0 comments on commit 5c41d11

Please sign in to comment.