Skip to content

Commit 49b685d

Browse files
Add regression test for #18059 (#18075)
Closes #18059 Probably fixed by #17342 [skip mima] [skip docs] [skip community_build]
2 parents 8184fa8 + 050f54b commit 49b685d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

tests/pos-macros/i18059/Macro_1.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import scala.annotation.StaticAnnotation
2+
3+
class SqlName(val sqlName: String) extends StaticAnnotation
4+
5+
import scala.compiletime.*
6+
import scala.quoted.*
7+
8+
inline def sqlFieldNamesFor[T]: Vector[(String, String)] = ${
9+
sqlFieldNamesForImpl[T]
10+
}
11+
12+
private def sqlFieldNamesForImpl[T: Type](using
13+
Quotes // must be named!! like `q: Quotes`
14+
): Expr[Vector[(String, String)]] =
15+
import quotes.reflect.*
16+
val annot = TypeRepr.of[SqlName].typeSymbol
17+
val tuples: Seq[Expr[(String, String)]] = TypeRepr
18+
.of[T]
19+
.typeSymbol
20+
.primaryConstructor
21+
.paramSymss
22+
.head
23+
.collect:
24+
case sym if sym.hasAnnotation(annot) =>
25+
val fieldNameExpr = Expr(sym.name.asInstanceOf[String])
26+
val annotExpr = sym.getAnnotation(annot).get.asExprOf[SqlName]
27+
'{ ($fieldNameExpr, $annotExpr.sqlName) }
28+
val seq: Expr[Seq[(String, String)]] = Expr.ofSeq(tuples)
29+
'{ $seq.toVector }

tests/pos-macros/i18059/Test_2.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case class AppUser(
2+
id: Long,
3+
firstName: Option[String],
4+
@SqlName("last_name") lastName: String
5+
)
6+
7+
def hello: Unit =
8+
println(sqlFieldNamesFor[AppUser]) // Vector((lastName, last_name))

0 commit comments

Comments
 (0)