From eedd2f3851561bd579f5c31d4ad8c06e81dde84d Mon Sep 17 00:00:00 2001 From: Daniel Slapman Date: Sat, 4 Jan 2025 23:08:26 +0100 Subject: [PATCH] Support `isBefore` & `isAfter` for `Instant` --- .scalafix.conf | 4 ++- .../src/main/scala/oolong/AstParser.scala | 7 +++++ .../test/scala/oolong/mongo/QuerySpec.scala | 31 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/.scalafix.conf b/.scalafix.conf index 6caff1c..0f2eea8 100644 --- a/.scalafix.conf +++ b/.scalafix.conf @@ -29,4 +29,6 @@ DisableSyntax.regex = [ pattern = "import cats\\.implicits" message = "Use granular imports" } -] \ No newline at end of file +] + +OrganizeImports.targetDialect = Scala3 \ No newline at end of file diff --git a/oolong-core/src/main/scala/oolong/AstParser.scala b/oolong-core/src/main/scala/oolong/AstParser.scala index fe3dab2..7f9c999 100644 --- a/oolong-core/src/main/scala/oolong/AstParser.scala +++ b/oolong-core/src/main/scala/oolong/AstParser.scala @@ -1,5 +1,6 @@ package oolong +import java.time.Instant import java.util.regex.Pattern import scala.annotation.tailrec import scala.language.postfixOps @@ -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(".")) diff --git a/oolong-mongo/src/test/scala/oolong/mongo/QuerySpec.scala b/oolong-mongo/src/test/scala/oolong/mongo/QuerySpec.scala index 87d6d20..8254bf1 100644 --- a/oolong-mongo/src/test/scala/oolong/mongo/QuerySpec.scala +++ b/oolong-mongo/src/test/scala/oolong/mongo/QuerySpec.scala @@ -1,5 +1,6 @@ package oolong.mongo +import java.time.Instant import java.time.LocalDate import java.time.ZoneOffset import java.util.regex.Pattern @@ -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,