Skip to content

Add regression test for #18059 #18075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions tests/pos-macros/i18059/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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))