Skip to content

Commit ad10e79

Browse files
committed
Support isBefore & isAfter for Instant
1 parent 905862c commit ad10e79

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.scalafix.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ DisableSyntax.regex = [
2929
pattern = "import cats\\.implicits"
3030
message = "Use granular imports"
3131
}
32-
]
32+
]
33+
34+
OrganizeImports.targetDialect = Scala3

oolong-core/src/main/scala/oolong/AstParser.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package oolong
22

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

166+
case '{ ($x: Instant).isBefore($s)} =>
167+
QExpr.Lt(parse(x), parse(s))
168+
169+
case '{ ($x: Instant).isAfter($s)} =>
170+
QExpr.Gt(parse(x), parse(s))
171+
165172
case PropSelector(name, path) if name == paramName =>
166173
QExpr.Prop(path.mkString("."))
167174

oolong-mongo/src/test/scala/oolong/mongo/QuerySpec.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package oolong.mongo
22

3+
import java.time.Instant
34
import java.time.LocalDate
45
import java.time.ZoneOffset
56
import java.util.regex.Pattern
@@ -1167,6 +1168,36 @@ class QuerySpec extends AnyFunSuite {
11671168
)
11681169
}
11691170

1171+
test("Instant.ifBefore is supported") {
1172+
case class InstantTest(field: Instant)
1173+
val q = query[InstantTest](_.field.isBefore(lift(Instant.parse("2020-01-01T00:00:00Z"))))
1174+
val repr = renderQuery[InstantTest](_.field.isBefore(lift(Instant.parse("2020-01-01T00:00:00Z"))))
1175+
1176+
test(
1177+
q,
1178+
repr,
1179+
BsonDocument(
1180+
"field" -> BsonDocument("$lt" -> BsonDateTime(1577836800000L))
1181+
),
1182+
ignoreRender = true
1183+
)
1184+
}
1185+
1186+
test("Instant.isAfter is supported") {
1187+
case class InstantTest(field: Instant)
1188+
val q = query[InstantTest](_.field.isAfter(lift(Instant.parse("2020-01-01T00:00:00Z"))))
1189+
val repr = renderQuery[InstantTest](_.field.isAfter(lift(Instant.parse("2020-01-01T00:00:00Z"))))
1190+
1191+
test(
1192+
q,
1193+
repr,
1194+
BsonDocument(
1195+
"field" -> BsonDocument("$gt" -> BsonDateTime(1577836800000L))
1196+
),
1197+
ignoreRender = true
1198+
)
1199+
}
1200+
11701201
private inline def test(
11711202
query: BsonDocument,
11721203
repr: String,

0 commit comments

Comments
 (0)