File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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))
You can’t perform that action at this time.
0 commit comments