Skip to content

Commit

Permalink
codegen: Try to output code that cross-compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
nafg committed Dec 8, 2023
1 parent d0a76d3 commit 741421b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class TablesCodeGenerator extends BaseCodeGenerator {
.typeApply(rowClassType)

def mkMapping(rowClassName: String, mappingName: String, columns: List[ColumnConfig]) = {
val companion = Term.Name(rowClassName)
val rowClassType = Type.Name(rowClassName)
val terms = columns.map(_.tableFieldTerm)
val numCols = columns.length
val (tuple, factory, extractor) =
val companion = Term.Name(rowClassName)
val rowClassType = Type.Name(rowClassName)
val terms = columns.map(_.tableFieldTerm)
val numCols = columns.length
val rhs =
if (numCols == 1)
(terms.head, companion.termSelect("apply"), companion.termSelect("unapply"))
terms.head
.termSelect("mapTo")
.termApplyType(rowClassType)
else if (numCols <= 22)
(
Term.Tuple(terms),
Term.Eta(companion.termSelect("apply")).termSelect("tupled"),
companion.termSelect("unapply")
)
Term.Tuple(terms)
.termSelect("mapTo")
.termApplyType(rowClassType)
else {
@tailrec
def group22[A](values: List[A])(group: List[A] => A): A =
Expand Down Expand Up @@ -64,13 +64,13 @@ class TablesCodeGenerator extends BaseCodeGenerator {
term"Some".termApply(res)
)

(group22[Term](terms)(Term.Tuple(_)), fac, extractor)
group22[Term](terms)(Term.Tuple(_))
.termSelect("<>")
.termApply(fac, extractor)
}

defDef(mappingName, declaredType = Some(mappingType(rowClassType)))()(
tuple
.termSelect("<>")
.termApply(factory, extractor)
rhs
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object TableModules {
override def keyColumnName = "id"
val name = column[String]("name")
def mapping: slick.lifted.MappedProjection[ColorsRow] =
name.<>(ColorsRow.apply, ColorsRow.unapply)
name.mapTo[ColorsRow]
}
}
object People extends EntityTableModule[Long, PeopleRow]("people") {
Expand Down
7 changes: 3 additions & 4 deletions slick-additions-codegen/src/test/resources/plain/Tables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import slick.jdbc.H2Profile.api._
object Tables {
class Colors(_tableTag: Tag)
extends Table[ColorsRow](_tableTag, Some("PUBLIC"), "colors") {
val id = column[Long]("id")
val name = column[String]("name")
def * : slick.lifted.ProvenShape[ColorsRow] =
(id, name).<>((ColorsRow.apply _).tupled, ColorsRow.unapply)
val id = column[Long]("id")
val name = column[String]("name")
def * : slick.lifted.ProvenShape[ColorsRow] = (id, name).mapTo[ColorsRow]
}
lazy val Colors = TableQuery[Colors]
class People(_tableTag: Tag)
Expand Down

0 comments on commit 741421b

Please sign in to comment.