Skip to content

Commit

Permalink
Support isBefore & isAfter for Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Jan 4, 2025
1 parent 905862c commit eedd2f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .scalafix.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ DisableSyntax.regex = [
pattern = "import cats\\.implicits"
message = "Use granular imports"
}
]
]

OrganizeImports.targetDialect = Scala3
7 changes: 7 additions & 0 deletions oolong-core/src/main/scala/oolong/AstParser.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package oolong

import java.time.Instant
import java.util.regex.Pattern
import scala.annotation.tailrec
import scala.language.postfixOps
Expand Down Expand Up @@ -162,6 +163,12 @@ private[oolong] class DefaultAstParser(using quotes: Quotes) extends AstParser {
case '{ ($x: Option[_]).nonEmpty } =>
QExpr.Exists(parse(x), QExpr.Constant(true))

case '{ ($x: Instant).isBefore($s) } =>
QExpr.Lt(parse(x), parse(s))

case '{ ($x: Instant).isAfter($s) } =>
QExpr.Gt(parse(x), parse(s))

case PropSelector(name, path) if name == paramName =>
QExpr.Prop(path.mkString("."))

Expand Down
31 changes: 31 additions & 0 deletions oolong-mongo/src/test/scala/oolong/mongo/QuerySpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package oolong.mongo

import java.time.Instant
import java.time.LocalDate
import java.time.ZoneOffset
import java.util.regex.Pattern
Expand Down Expand Up @@ -1167,6 +1168,36 @@ class QuerySpec extends AnyFunSuite {
)
}

test("Instant.ifBefore is supported") {
case class InstantTest(field: Instant)
val q = query[InstantTest](_.field.isBefore(lift(Instant.parse("2020-01-01T00:00:00Z"))))
val repr = renderQuery[InstantTest](_.field.isBefore(lift(Instant.parse("2020-01-01T00:00:00Z"))))

test(
q,
repr,
BsonDocument(
"field" -> BsonDocument("$lt" -> BsonDateTime(1577836800000L))
),
ignoreRender = true
)
}

test("Instant.isAfter is supported") {
case class InstantTest(field: Instant)
val q = query[InstantTest](_.field.isAfter(lift(Instant.parse("2020-01-01T00:00:00Z"))))
val repr = renderQuery[InstantTest](_.field.isAfter(lift(Instant.parse("2020-01-01T00:00:00Z"))))

test(
q,
repr,
BsonDocument(
"field" -> BsonDocument("$gt" -> BsonDateTime(1577836800000L))
),
ignoreRender = true
)
}

private inline def test(
query: BsonDocument,
repr: String,
Expand Down

0 comments on commit eedd2f3

Please sign in to comment.