Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions oolong-bson/src/main/scala/oolong/bson/utils/utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ object AsTerm {
object TupleLambda {
def unapply(using quotes: Quotes)(expr: Expr[Any]): Option[(String, String)] =
import quotes.reflect.*

object ArrowToLiteral {
def unapply(body: Term): Option[(String, String)] =
body match
// x -> y : Scala 3.9 or earlier
case Apply(
TypeApply(Select(Apply(_, List(Select(_, first))), "->"), _),
List(Literal(StringConstant(second)))
) =>
Some(first -> second)
// Starting with Scala 3.10, inline `->` expands to a typed Tuple2.apply.
// A direct `(field, "name")` is an untyped Apply and remains unsupported.
case Typed(
Apply(
TypeApply(Select(receiver, "apply"), _),
List(Select(_, first), Literal(StringConstant(second)))
),
_
) if receiver.symbol == Symbol.requiredModule("scala.Tuple2") =>
Some(first -> second)
case _ => None
}

expr match
case AsTerm(
Block(
Expand All @@ -41,12 +64,7 @@ object TupleLambda {
"$anonfun",
_,
_,
Some(
Apply(
TypeApply(Select(Apply(_, List(Select(_, first))), "->"), _),
List(Literal(StringConstant(second)))
)
)
Some(ArrowToLiteral(first, second))
)
),
_
Expand Down
Loading