Skip to content

Commit

Permalink
Backport "Add regression test for #18059" to LTS (#19045)
Browse files Browse the repository at this point in the history
Backports #18075 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
Kordyjan committed Dec 8, 2023
2 parents 575487e + 88454a0 commit 4b5201c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/pos-macros/i18059/Macro_1.scala
@@ -0,0 +1,29 @@
import scala.annotation.StaticAnnotation

class SqlName(val sqlName: String) extends StaticAnnotation

import scala.compiletime.*
import scala.quoted.*

inline def sqlFieldNamesFor[T]: Vector[(String, String)] = ${
sqlFieldNamesForImpl[T]
}

private def sqlFieldNamesForImpl[T: Type](using
Quotes // must be named!! like `q: Quotes`
): Expr[Vector[(String, String)]] =
import quotes.reflect.*
val annot = TypeRepr.of[SqlName].typeSymbol
val tuples: Seq[Expr[(String, String)]] = TypeRepr
.of[T]
.typeSymbol
.primaryConstructor
.paramSymss
.head
.collect:
case sym if sym.hasAnnotation(annot) =>
val fieldNameExpr = Expr(sym.name.asInstanceOf[String])
val annotExpr = sym.getAnnotation(annot).get.asExprOf[SqlName]
'{ ($fieldNameExpr, $annotExpr.sqlName) }
val seq: Expr[Seq[(String, String)]] = Expr.ofSeq(tuples)
'{ $seq.toVector }
8 changes: 8 additions & 0 deletions tests/pos-macros/i18059/Test_2.scala
@@ -0,0 +1,8 @@
case class AppUser(
id: Long,
firstName: Option[String],
@SqlName("last_name") lastName: String
)

def hello: Unit =
println(sqlFieldNamesFor[AppUser]) // Vector((lastName, last_name))

0 comments on commit 4b5201c

Please sign in to comment.