Skip to content

Commit

Permalink
Allow customizing name of primary key type based on rows (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg authored Jul 13, 2024
1 parent b6db739 commit 156f927
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion typo/src/scala/typo/Naming.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class Naming(val pkg: sc.QIdent) {
protected def tpe(name: db.RelationName, suffix: String): sc.QIdent =
pkg / name.schema.map(sc.Ident.apply).toList / sc.Ident(Naming.titleCase(name.name)).appended(suffix)

def idName(source: Source): sc.QIdent = relation(source, "Id")
def idName(source: Source, cols: List[db.Col]): sc.QIdent = {
((), cols)._1 // allow implementors to use this
relation(source, "Id")
}
def repoName(source: Source): sc.QIdent = relation(source, "Repo")
def repoImplName(source: Source): sc.QIdent = relation(source, "RepoImpl")
def repoMockName(source: Source): sc.QIdent = relation(source, "RepoMock")
Expand Down
6 changes: 4 additions & 2 deletions typo/src/scala/typo/internal/ComputedTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ case class ComputedTable(

val maybeId: Option[IdComputed] =
dbTable.primaryKey.flatMap { pk =>
val tpe = sc.Type.Qualified(naming.idName(source))
pk.colNames match {
case NonEmptyList(colName, Nil) =>
val dbCol = dbColsByName(colName)
Expand All @@ -60,8 +59,10 @@ case class ComputedTable(
Some(IdComputed.UnaryUserSpecified(col, underlying))
else if (!options.enablePrimaryKeyType.include(dbTable.name))
Some(IdComputed.UnaryNoIdType(col, underlying))
else
else {
val tpe = sc.Type.Qualified(naming.idName(source, List(col.dbCol)))
Some(IdComputed.UnaryNormal(col, tpe))
}
}

case colNames =>
Expand All @@ -74,6 +75,7 @@ case class ComputedTable(
dbCol = dbColsByName(colName)
)
}
val tpe = sc.Type.Qualified(naming.idName(source, cols.toList.map(_.dbCol)))
Some(IdComputed.Composite(cols, tpe, paramName = sc.Ident("compositeId")))
}
}
Expand Down

0 comments on commit 156f927

Please sign in to comment.