Skip to content

Commit

Permalink
Merge pull request #85 from lucidsoftware/scalafmt
Browse files Browse the repository at this point in the history
format: Use scalafmt to automate format and check format
  • Loading branch information
tmccombs authored Nov 15, 2023
2 parents 3b1d297 + b06e071 commit 6fd0fc1
Show file tree
Hide file tree
Showing 28 changed files with 1,157 additions and 429 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Build
run: sbt package
- name: Test
run: sbt test
run: sbt test scalafmtCheckAll
- uses: actions/upload-artifact@v3
with:
name: jars
Expand Down
18 changes: 18 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version = 3.7.14
runner.dialect = scala213source3
align = none
align.openParenCallSite = false
align.openParenDefnSite = false
continuationIndent.defnSite = 2
danglingParentheses.preset = true
docstrings.style = Asterisk
maxColumn = 120
importSelectors = singleLine
rewrite.redundantBraces.stringInterpolation = true
rewrite.rules = [
RedundantParens,
PreferCurlyFors,
SortImports,
]
runner.fatalWarnings = true
newlines.afterCurlyLambdaParams = keep
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
http://lucidsoftware.github.io/relate/

[![Build Status](https://travis-ci.com/lucidsoftware/relate.svg)](https://travis-ci.com/lucidsoftware/relate)
[![Maven Version](https://img.shields.io/maven-central/v/com.lucidchart/relate_2.12.svg)](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.lucidchart%22%20AND%20a%3A%22relate_2.12%22)
[![Maven Version](https://img.shields.io/maven-central/v/com.lucidchart/relate_2.13.svg)](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.lucidchart%22%20AND%20a%3A%22relate_2.13%22)
[![Join the chat at https://gitter.im/lucidsoftware/relate](https://badges.gitter.im/lucidsoftware/relate.svg)](https://gitter.im/lucidsoftware/relate?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Relate is a lightweight, blazingly fast database access layer for Scala that abstracts the idiosyncricies of the JDBC while keeping complete control over the SQL.
Expand Down
18 changes: 6 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import com.lucidchart.sbtcross.ProjectAggregateArgument.toArgument

lazy val macros = project.in(file("macros")).cross.dependsOn(relate)
lazy val `macros2.11` = macros("2.11.12")
lazy val `macros2.12` = macros("2.12.11")
lazy val `macros2.13` = macros("2.13.2")
lazy val macrosAggregate = macros.aggregate(`macros2.11`, `macros2.12`, `macros2.13`).settings(
lazy val `macros2.13` = macros("2.13.12")
lazy val macrosAggregate = macros.aggregate( `macros2.13`).settings(
publish / skip := true
)

lazy val relate = project.in(file("relate")).cross
lazy val `relate2.11` = relate("2.11.12")
lazy val `relate2.12` = relate("2.12.11")
lazy val `relate2.13` = relate("2.13.2")
lazy val relateAggregate = relate.aggregate(`relate2.11`, `relate2.12`, `relate2.13`).settings(
lazy val `relate2.13` = relate("2.13.12")
lazy val relateAggregate = relate.aggregate(`relate2.13`).settings(
publish / skip := true
)

lazy val postgres = project.in(file("postgres")).cross.dependsOn(relate)
lazy val `postgres2.11` = postgres("2.11.12")
lazy val `postgres2.12` = postgres("2.12.11")
lazy val `postgres2.13` = postgres("2.13.2")
lazy val postgresAggregate = postgres.aggregate(`postgres2.11`, `postgres2.12`, `postgres2.13`).settings(
lazy val `postgres2.13` = postgres("2.13.12")
lazy val postgresAggregate = postgres.aggregate(`postgres2.13`).settings(
publish / skip := true
)

Expand Down
5 changes: 0 additions & 5 deletions macros/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ libraryDependencies ++= Seq(
"org.specs2" %% "specs2-mock" % "4.6.0" % Test,
)

libraryDependencies ++= (CrossVersion.binaryScalaVersion(scalaVersion.value) match {
case "2.11" | "2.12" => Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full))
case _ => Seq.empty
})

moduleName := "relate-macros"

scalacOptions += "-language:experimental.macros"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ class RowParserImpl(val c: Context) {

val opts: AnnotOpts = c.prefix.tree match {
case q"new Record(..$params)" =>
val paramTrees: Map[String, Tree] = params.map {
case q"$optNameAst -> $optValueAst" =>
val optName = optNameAst match {
case Literal(Constant(optName: String)) => optName
case name => c.abort(name.pos, "Keys must be literal strings")
}

if (!validOptions.contains(optName)) {
c.abort(optNameAst.pos, s"$optName is an invalid option. Valid options: ${validOptions.mkString(", ")}")
}

optName -> optValueAst
val paramTrees: Map[String, Tree] = params.map { case q"$optNameAst -> $optValueAst" =>
val optName = optNameAst match {
case Literal(Constant(optName: String)) => optName
case name => c.abort(name.pos, "Keys must be literal strings")
}

if (!validOptions.contains(optName)) {
c.abort(optNameAst.pos, s"$optName is an invalid option. Valid options: ${validOptions.mkString(", ")}")
}

optName -> optValueAst
}.toMap

if (paramTrees.contains("colMapping") && paramTrees.contains("snakeCase")) {
Expand All @@ -46,15 +45,17 @@ class RowParserImpl(val c: Context) {

paramTrees.foldLeft(AnnotOpts(false, Map.empty)) { case (opts, (optName, optValueAst)) =>
optName match {
case "colMapping" => optValueAst match {
case q"Map[..$tpts](..$params)" =>
opts.copy(remapping = getRemapping(params))
}
case "snakeCase" => optValueAst match {
case q"true" => opts.copy(snakeCase = true)
case q"false" => opts.copy(snakeCase = false)
case value => c.abort(value.pos, "snakeCase requires a literal true or false value")
}
case "colMapping" =>
optValueAst match {
case q"Map[..$tpts](..$params)" =>
opts.copy(remapping = getRemapping(params))
}
case "snakeCase" =>
optValueAst match {
case q"true" => opts.copy(snakeCase = true)
case q"false" => opts.copy(snakeCase = false)
case value => c.abort(value.pos, "snakeCase requires a literal true or false value")
}
}
}
case q"new Record()" => AnnotOpts(false, Map.empty)
Expand All @@ -64,7 +65,6 @@ class RowParserImpl(val c: Context) {

val result: List[Tree] = inputs match {
case target @ q"case class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" :: tail =>

val params = paramss.head

val paramNames = params.map(_.name.toString).toSet
Expand Down Expand Up @@ -134,13 +134,13 @@ class RowParserImpl(val c: Context) {
private def tupleValueString(tupleTree: Tree): String = {
val remapAst = tupleTree match {
case q"$aa($colLit).$arrow[..$tpts]($remapAst)" => remapAst
case q"$col -> $remapAst" => remapAst
case q"($col, $remapAst)" => remapAst
case q"$col -> $remapAst" => remapAst
case q"($col, $remapAst)" => remapAst
}

remapAst match {
case Literal(Constant(remap: String)) => remap
case value => c.abort(value.pos, "Remappings must be literal strings")
case value => c.abort(value.pos, "Remappings must be literal strings")
}
}

Expand Down Expand Up @@ -189,11 +189,16 @@ class RowParserImpl(val c: Context) {
}
}

private def toSnakeCase(s: String): String = s.replaceAll(
"([A-Z]+)([A-Z][a-z])", "$1_$2"
).replaceAll(
"([a-z\\d])([A-Z])", "$1_$2"
).toLowerCase
private def toSnakeCase(s: String): String = s
.replaceAll(
"([A-Z]+)([A-Z][a-z])",
"$1_$2"
)
.replaceAll(
"([a-z\\d])([A-Z])",
"$1_$2"
)
.toLowerCase

private def findCaseClassFields(ty: Type): List[(TermName, Type)] = {
ty.members.sorted.collect {
Expand All @@ -204,17 +209,17 @@ class RowParserImpl(val c: Context) {
private def expand(colLit: Tree, tree: Tree): (String, Tree) = {
val col = colLit match {
case Literal(Constant(col: String)) => col
case _ => c.abort(colLit.pos, "Column names must be literal strings")
case _ => c.abort(colLit.pos, "Column names must be literal strings")
}
col -> tree
}

private def getRemapping(params: List[Tree]): Map[String, Tree] = {
params.map {
case tree @ q"$aa($colLit).$arrow[..$tpts]($remapLit)" => expand(colLit, tree)
case tree @ q"$colLit -> $remapLit" => expand(colLit, tree)
case tree @ q"($colLit, $remapLit)" => expand(colLit, tree)
case tree => c.abort(tree.pos, "Remappings must be literal tuples")
case tree @ q"$colLit -> $remapLit" => expand(colLit, tree)
case tree @ q"($colLit, $remapLit)" => expand(colLit, tree)
case tree => c.abort(tree.pos, "Remappings must be literal tuples")
}.toMap
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ case class Big(
def m2: Int = 0
}


class RowParserTest extends Specification with Mockito {
class MockableRow extends SqlRow(null) {
final override def apply[A: ColReader](col: String): A = super.apply(col)
Expand All @@ -57,7 +56,7 @@ class RowParserTest extends Specification with Mockito {

val p = generateParser[Thing]

p.parse(row) mustEqual(Thing("hi", Some(20)))
p.parse(row) mustEqual (Thing("hi", Some(20)))
}

"generate parser w/snake_case columns" in {
Expand All @@ -67,14 +66,16 @@ class RowParserTest extends Specification with Mockito {

val p = generateSnakeParser[Thing]

p.parse(row) mustEqual(Thing("gregg", Some(20)))
p.parse(row) mustEqual (Thing("gregg", Some(20)))
}

"remap column names" in {
val p = generateParser[User](Map(
"firstName" -> "fname",
"lastName" -> "lname"
))
val p = generateParser[User](
Map(
"firstName" -> "fname",
"lastName" -> "lname"
)
)

val row = mock[MockableRow]
row.stringOption("fname") returns Some("gregg")
Expand All @@ -84,10 +85,12 @@ class RowParserTest extends Specification with Mockito {
}

"remap column names w/normal tuple syntax" in {
val p = generateParser[User](Map(
("firstName", "fname"),
("lastName", "lname")
))
val p = generateParser[User](
Map(
("firstName", "fname"),
("lastName", "lname")
)
)

val row = mock[MockableRow]
row.stringOption("fname") returns Some("gregg")
Expand All @@ -97,9 +100,11 @@ class RowParserTest extends Specification with Mockito {
}

"remap some column names" in {
val p = generateParser[User](Map(
"firstName" -> "fname"
))
val p = generateParser[User](
Map(
"firstName" -> "fname"
)
)

val row = mock[MockableRow]
row.stringOption("fname") returns Some("gregg")
Expand All @@ -110,18 +115,39 @@ class RowParserTest extends Specification with Mockito {

"generate parser for a case class > 22 fields" in {
val row = mock[MockableRow]
for (i <- (1 to 9)) { row.intOption(s"f${i}") returns Some(i) }
for (i <- (10 to 19)) { row.intOption(s"z${i}") returns Some(i) }
for (i <- (20 to 25)) { row.intOption(s"a${i}") returns Some(i) }

for (i <- 1 to 9) { row.intOption(s"f${i}") returns Some(i) }
for (i <- 10 to 19) { row.intOption(s"z${i}") returns Some(i) }
for (i <- 20 to 25) { row.intOption(s"a${i}") returns Some(i) }

val p = generateParser[Big]

p.parse(row) mustEqual(Big(
1, Some(2), 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, Some(24), 25)
)
p.parse(row) mustEqual (Big(
1,
Some(2),
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
Some(24),
25
))
}

"fail to compile with non-literals" in {
Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ addSbtPlugin("com.lucidchart" % "sbt-cross" % "4.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.2.1")

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.18")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
3 changes: 0 additions & 3 deletions relate/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ libraryDependencies ++= Seq(
)

libraryDependencies ++= (CrossVersion.binaryScalaVersion(scalaVersion.value) match {
case "2.10" => Seq("com.typesafe.play" %% "anorm" % "2.4.0" % Benchmark)
case "2.11" => Seq("com.typesafe.play" %% "anorm" % "2.5.2" % Benchmark)
case "2.12" => Seq("com.typesafe.play" %% "anorm" % "2.6.0-M1" % Benchmark)
case "2.13" => Seq("org.playframework.anorm" %% "anorm" % "2.6.7" % Benchmark)
})

Expand Down
Loading

0 comments on commit 6fd0fc1

Please sign in to comment.